I'm trying to make a query i can use for an alert, it generates sales for the past 7 days.
This query works fine:
SELECT
CASE WHEN GROUPING(T0.[CardCode]) = 0
THEN CAST (T0.[CardCode] AS CHAR(8))
ELSE 'ALL'
END AS Customer#,
SUM(T0.[Max1099]) AS "Total Sales",
SUM(T0.[GrosProfit]) AS "Gross Profit"
FROM OINV T0
WHERE T0.[DocDate] >= DATEADD(dd,DATEDIFF(dd,0,GETDATE())-7,0) AND T0.[Max1099] > 0
GROUP BY T0.[CardCode] WITH ROLLUP
And it gives me this:
# Customer#* Total Sales* Gross Profit*
1 C2235 8,285.87 4,165.77
2 C2236 10,191.39 4,197.95
3 C2253 570.56 311.17
4 C3008 18,756.76 5,720.21
5 ALL 37,804.58 14,395.10
Which is great. Gives me a total at the end, and substitutes "ALL" for the customer number. Lovely.
Problem #1: I REALLY want it to give the Customer Name NEXT TO the Customer Number. But when I try to add it, i have to add it to the GROUP BY as well. Which changes the query to this:
SELECT
CASE WHEN GROUPING(T0.[CardCode]) = 0
THEN CAST (T0.[CardCode] AS CHAR(8))
ELSE 'ALL'
END AS Customer#,
CardName as "Cust Name",
SUM(T0.[Max1099]) AS "Total Sales",
SUM(T0.[GrosProfit]) AS "Gross Profit"
FROM OINV T0
WHERE T0.[DocDate] >= DATEADD(dd,DATEDIFF(dd,0,GETDATE())-7,0) AND T0.[Max1099] > 0
GROUP BY T0.[CardCode], T0.[CardName] WITH ROLLUP
And changes my output to THIS:
# Customer# Cust Name Total Sales Gross Profit
1 C2235 Acme Products 8,285.87 4,165.77
2 C2235 (blanks blanks) 8,285.87 4,165.77
3 C2236 Some Other Products 10,191.39 4,197.95
4 C2236 (blanks blanks blanks) 10,191.39 4,197.95
5 C2253 Third Customer Name 570.56 311.17
6 C2253 (blanks blanks blanks) 570.56 311.17
7 C3008 Fourth Customer Name 18,756.76 5,720.21
8 C3008 (blanks blanks blanks) 18,756.76 5,720.21
9 ALL 37,804.58 14,395.10
( I have replaced actual customer names, of course, and replaces actual blanks with the word 'blanks' so it would be more legible.)
I can't figure out a way to simply list the customer name next to the number. Instead , it gives me a summary for the CardCode and a summary for the CardName.
I've tried combining the two into one field, on the fly, but haven't been successful.
Problem #2 - extra credit!
If i really want this done right, i should also have a query that pulls the same data from ORIN (Credit Memos) and do a UNION ALL, but when i do this, is simply rejects me at the word "UNION"
any and all help appreciated, and to test this, you can just cut and past the query into SAP, it will run right there, no mods needed.
oops. I had to change the "Not Equal" symbol to just "greater than" for "Max1099" because it was just dropping the symbol...
Edited by: Dante Amodeo on Jan 18, 2012 6:30 PM