Friday, February 17, 2012

Counting Orders in Sub-Select Query?

Hi,
I am summarizing data like this
SELECT dbo_Order_Line_Invoice.Cono, dbo_Order_Line_Invoice.CustId,
Year([Invoicedate]) & '/' & Format(Month([Invoicedate]),'00') AS
[Year-Month], dbo_Order_Line_Invoice.VendId,
Sum(dbo_Order_Line_Invoice.Sales) AS Sales_Total, Sum([Sales]-[Cost]) AS
GM_Total, Count(dbo_Order_Line_Invoice.LineNum) AS Line_Count
FROM dbo_Order_Line_Invoice
WHERE (((dbo_Order_Line_Invoice.InvoiceDate)>=#8/1/2004# And
(dbo_Order_Line_Invoice.InvoiceDate)<=#7/31/2005#))
GROUP BY dbo_Order_Line_Invoice.Cono, dbo_Order_Line_Invoice.CustId,
Year([Invoicedate]) & '/' & Format(Month([Invoicedate]),'00'),
dbo_Order_Line_Invoice.VendId;
But I also want to count the number of orders. I am thinking I could
use a sub query to select just the orders for the above realtions, group
on ordernumber then Count(*) but I don't know how to actually do this.
Can someone help me?
TIA,
DanThat isn't a SQL Server query. If you are using SQL Server then take a
look at the CUBE / ROLLUP operator.
David Portas
SQL Server MVP
--

No comments:

Post a Comment