jana
asked on
Extract the TOP 1 of each item within the Table in Ms Sql
We have a sales table with where an invoice can have 2 type of transactions: ‘Invoice’ and ‘PreQtrInvoice’. The table consists of a document number representing the invoice number for the sales and a type for the Items sold.
We are trying to display only the first ‘PreQtrInvoice’of an Item within each document number (like TOP 1 sorting by ‘PreQtrInvoice’). If the item has no ‘PreQtrInvoice’ within its document number, then include in the list the first Invoice type
The data is:
Data:
The expected result is:
Upto now we see 2 separate script. One to extract the TOP 1 of ‘PreQtrInvoice’ and another to display items with ‘‘PreQtrInvoice’ and display the first one of their document number.
We are trying to display only the first ‘PreQtrInvoice’of an Item within each document number (like TOP 1 sorting by ‘PreQtrInvoice’). If the item has no ‘PreQtrInvoice’ within its document number, then include in the list the first Invoice type
The data is:
Data:
DocNumber Product DocType StockQy
Doc1 Prod-A PreQtrInvoice 1
Doc1 Prod-A Invoice 3
Doc1 Prod-B PreQtrInvoice 5
Doc1 Prod-B PreQtrInvoice 5
Doc1 Prod-B Invoice 1
Doc1 Prod-C Invoice 1
Doc2 Prod-D Invoice 2
Doc2 Prod-E Invoice 2
Doc3 Prod-F PreQtrInvoice 1
Doc3 Prod-G Invoice 7
Doc3 Prod-F Invoice 3
The expected result is:
DocNumber Product DocType StockQy
Doc1 Prod-A PreQtrInvoice 1
Doc1 Prod-B PreQtrInvoice 5
Doc1 Prod-C Invoice 1
Doc2 Prod-D Invoice 2
Doc2 Prod-E Invoice 2
Doc3 Prod-F PreQtrInvoice 1
Doc3 Prod-G Invoice 7
Upto now we see 2 separate script. One to extract the TOP 1 of ‘PreQtrInvoice’ and another to display items with ‘‘PreQtrInvoice’ and display the first one of their document number.
See attach sample using row counter to sort invoices. Note: SQL 2008 syntax used for populating table.
Invoice-Sample-SQL.sql
Invoice-Sample-SQL.sql
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
gohord, we're getting a syntax error at the first "('Doc1',".
BriCrowe, it worked excellently. How can we display only the result? When adding your portion, it does a full table display, then the result.
Here is the last portion of the script after adding your lines:
BriCrowe, it worked excellently. How can we display only the result? When adding your portion, it does a full table display, then the result.
Here is the last portion of the script after adding your lines:
-- Display Table
SELECT * from @Sales
;WITH cteInvoice (DocNumber, Product, DocType, StockQy, RowNumber)
AS
(
SELECT DocNumber, Product, DocType, StockQy,
ROW_NUMBER() OVER (PARTITION BY Product ORDER BY DocType DESC) AS RowNumber
FROM @Sales
)
SELECT * FROM cteInvoice
WHERE RowNumber = 1
ORDER BY DocNumber, Product asc
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes! Thanx worked!
Can you explain a bit on the use of
and
in order to get the result?
Can you explain a bit on the use of
ROW_NUMBER() OVER (PARTITION BY Product ORDER BY DocType DESC) AS RowNumber
and
WHERE RowNumber = 1
in order to get the result?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
PortletPaul thanks for picking up my slack :-)
no problem BriCrowe happy to lend a hand.
ASKER
Thanx Lots! You guys are Great!
ASKER
Thanx!
ASKER
Open in new window
Below is the results of the above marking the lines the script we are working trying to accomplish:
Open in new window