SQ Server : how to find the fifth order for each customer - sql-server-2014-express

How to find the fifth order for each customer and return title_order or null if the customer doesn't have the fifth order
Tables are
customer with columns Id, firstname, lastname...
order with columns order_id, title_order, id_custmer, date...
It can be done only with a query or do I need to create a function
Thanks in advance

You can use OUTER APPLY with OFFSET-FETCH:
select c.firstname, oa.title_order
from customer c
outer apply(select title_order from order o
where o.id_custmer = c.Id
order by date
offset 4 ROW FETCH next 1 ROW only)oa

Related

Get name from only date and ID matched with people list

I have rawdata with only ID's and dates of transactions. Then I have a list of people; their ID, their hire date and their name.
What I want is a column in the rawdata that gives me the persons name. The problem is, that the ID is re-used if the person quits, and another one starts.
Since I have the transaction date, I figure it should be possible to check:
IF the transaction matches the period they were hired, then get the right name.
Is this possible, and then how?
Rawdata:
Column A = Transaction date
Column B = ID
Column C = Here I want their name
People list:
Column A = ID
Column B = Name
Column C = Hire date
Example (Excel Online - can be edited):
https://1drv.ms/x/s!AjWVkb2UBexjhzAKMy-YiE5EoKwc
Screenshot:
Make sure your reference table is ordered in chronological order oldest to newest:
=INDEX(F:F,AGGREGATE(14,6,ROW($G$8:INDEX(G:G,MATCH(1E+99,E:E)))/(($E$8:INDEX(E:E,MATCH(1E+99,E:E))=B8)*($G$8:INDEX(G:G,MATCH(1E+99,E:E))<A8)),1))
I had to name your tables People and Raw_Data to use structured table references in the formula. Additonally, I added some data and changed a few dates.
=INDEX(People[Agent Name], AGGREGATE(15,6, (ROW(People[Personal ID])-ROW(People[[#Headers],[Agent Name]]))/((People[Personal ID]=[#[Personal ID]])*(People[Hire date]=AGGREGATE(14, 6, (People[Hire date])/((People[Personal ID]=[#[Personal ID]])*(People[Hire date]<=[#[Transaction date]])), 1))), 1))

DAX query to see if there's another row in a table with same id and same date - 1 year

I've got a table with orders. It contains the following relevant columns:
OrderId (Key)
CustomerId
Date (Hierarchy)
I want to create a new column in the same table: OrderedSameMonthLastYear
The value should be true if there's at least one other order from the same customer the same month one year ago.
I've tried a couple different queries but I don't really know enough DAX to accomplish this.
Thanks!
You can use the EARLIER() function to access the previous row context (which is all the rows in the table in this case) and do the comparison between columns, and then use COUNTROWS() to count the number of filtered rows.
OrderedSameMonthLastYear =
IF(
COUNTROWS(
FILTER(
Orders,
Orders[CustomerId] = EARLIER(Orders[CustomerId]) &&
Orders[Date].[Year] = EARLIER(Orders[Date].[Year]) - 1 &&
Orders[Date].[Month] = EARLIER(Orders[Date].[Month])
)
) > 0,
TRUE,
FALSE
)
The result will be as below:

How can I fetch 'distinct' data keeping row ID in the lookup view?

Assume I have an entity with the following structure:
ID (it would be the GUID of the record, I'll use number in this example for easier read)
Name
Product (lookup to product)
Price List (custom lookup to standard price list)
I now need to build a lookup view in this fashion:
(Sample data)
ID Name Product PriceList
-----------------------------------
1 Rec1 P1 PL1
2 Rec1 P1 PL2
3 Rec1 P1 PL3
4 Rec2 P2 PL1
5 Rec2 P2 PL2
(Desired result in lookup view)
ID Name
-----------------------------------
1 Rec1
4 Rec2
To explain the requirement in plain english, it is:
In the lookup view I only want one row per Name: Product and Price List don't matter.
Here's what I'd do if I could use straight T-SQL:
;WITH cte AS
(
SELECT ROW_NUMBER()
OVER (PARTITION BY Name ORDER BY Name ASC) AS RowNo, ID, Name
FROM FilteredEntity
)
SELECT ID, Name FROM cte WHERE RowNo = 1
But I can't figure out how to achieve the same result set in a FetchXML query.
The only way I can think of to do this would go like this:
In JS on your form, determine the unique names (Rec1 and Rec2 from your example), and store the ids of two records that reference Rec1 and Rec2 (let's call these ids Id1 and Id2)
Generate a fetch query that returns only those two records. Your fetch query would specifically say show me records where id is Id1 or Id2.
Filter the lookup using addCustomFilter and addPreSearch (link).
I can see two approaches here, neither of which are pure FetchXML queries.
Use distinct, (I'm not 100% on this but worth giving a go). This will then hopefully get you a record for every name, and you just grab the first record of each in code.
<attribute name='name' distinct='true'/>
Perform the filtering client side using Linq.

Doctrine2 subquery

I'm trying to write a subquery in doctrine2, to sort a table ordered by date column of another table.
(let's say I'm querying table A, which has an id column, and B has an a_id and a date, in the subquery b.a_id = a.id)
I'm using query builder, and the addSelect method, but since I can't use LIMIT in my query I get this error:
SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more
than 1 row
This error is true, but how could I limit this query to 1 row, if LIMIT is not allowed in doctrine2 and when I try to do it by querybuilder (I mean the subquery) and I'm using setMaxResults, and then getDQl it is still not working.
->addSelect('(SELECT b.date FROM B b WHERE b.conversation = a.id ORDER BY b.date DESC)')
Is there any solution for my problem?
Thanks
Make the query return exactly one row. Try SELECT MAX(b.date) FROM B b WHERE b.conversation = a.id)

Cassandra BETWEEN & ORDER BY operations

I wanted to perform SQL operations such as BETWEEN, ORDER BY with ASC/DSC order on Cassandra-0.7.8.
As I know, Cassandra-0.7.8 does not have direct support to these operations. Kindly let me know is there a way to accomplish these by tweaking on secondary index?
Below is my Data model design.
Emp(KS){
User(CF):{
bsanderson(RowKey): { eno, name, dept, dob, email }
prothfuss(RowKey): { eno, name, dept, dob, email }
}
}
Queries:
- Select * from emp where dept='IT' ORDER BY dob ASC.
- Select * from emp where eno BETWEEN ? AND ? ORDER BY dob ASC.
Thanks in advance.
Regards,
Thamizhananl
Select * from emp where dept='IT' ORDER BY dob ASC.
You can select rows where the 'dept' column has a certain value, by using the built-in secondary indexes. However, the rows will be returned in the order determined by the partitioner (RandomPartitioner or OrderPreservingPartitioner). To order by arbitrary values such as DOB, you would need to sort at the client.
Or, you could support this query directly by having a row for each dept, and a column for each employee, keyed (and therefore sorted) by DOB. But be careful of shared birthdays! And you'd still need subsequent queries to retrieve other data (the results of your SELECT *) for the employees selected, unless you denormalise so that the desired data is stored in the index too.
Select * from emp where eno BETWEEN ? AND ? ORDER BY dob ASC.
The secondary index querying in Cassandra requires at least one equality term, so I think you can do dept='IT' AND eno >=X AND eno <=y, but not just a BETWEEN-style query.
You could do this by creating your own index row, with a column for each employee, keyed on the employee number, with an appropriate comparator so all the columns are automatically sorted in employee-number order. You could then do a range query on that row to get a list of matching employees - but you would need further queries to retrieve other data for each employee (dob etc), unless you denormalise so that the desired data is stored in the index too. You would still need to do the dob ordering at the client.
As I know the columns will be sorted by comparator when you create column family and you can use clustring key for sorting on your opinion
and row in column family will be sorted by partitioner
I suggest you read this paper
Cassandra The Definitive Guide Chapter 6

Resources