A report I'm working on is based on the following SELECT statement; it was good enough at the outset but now I'm being asked to subtotal every part ordered (by part number) and show a grand total.SELECTarmast.fcinvoice as INVNUM,CONVERT(CHAR(10), armast.finvdate, 101) AS INVDATE, aritem.fpartno AS PARTNO, aritem.fshipqty as SHIPQTY, aritem.fprice AS UNITPRICE,SUM(aritem.fprice * aritem.fshipqty) AS INVTOTAL FROMaritem LEFT OUTER JOIN armast ON aritem.fcinvoice=armast.fcinvoice WHEREarmast.finvdate BETWEEN ('2007-08-01') AND ('2008-08-13')AND armast.fcustno in ('082130') AND aritem.fpartno in ('RedPart', 'WhitePart', 'GreenPart') GROUP BYaritem.fpartno, aritem.fprice, aritem.fshipqty, armast.fcinvoice, armast.finvdate(with this result)[CODE]INVNUM INVDATE PARTNO SHIPQTY UNITPRICE INVTOTAL------ ------- ------ ------- --------- --------75000 7/18/2008 RedPart 22000 0.31 682070125 3/5/2008 RedPart 5000 0.34 170066232 10/23/2007 WhitePart 7000 0.085 59570125 3/5/2008 GreenPart 5000 0.274 1370[/CODE]So I did some looking into ROLLUP and thought maybe it could help. It DOES give me the Total Ordered that I wanted, but it also returns a LOT of rows of repetitive data:SELECT armast.fcinvoice AS INVNUM, CONVERT(CHAR(10), armast.finvdate, 101) AS INVDATE, CASE WHEN (GROUPING(aritem.fpartno) = 1) THEN 'Total Ordered' ELSE aritem.fpartno END AS PARTNO, SUM(aritem.fshipqty) AS SHIPQTY, aritem.fprice as UNITPRICE, SUM(aritem.fprice * aritem.fshipqty) AS INVTOTAL FROM aritem left outer join armast on aritem.fcinvoice = armast.fcinvoiceWHERE armast.finvdate BETWEEN '2007-08-01' AND '2008-08-13' AND armast.fcustno in ('082130') AND aritem.fpartno in ('RedPart', 'WhitePart', 'GreenPart')GROUP BY aritem.fpartno, aritem.fprice, armast.finvdate, armast.fcinvoice WITH ROLLUP(which returns this:)INVNUM INVDATE PARTNO SHIPQTY UNITPRICE INVTOTAL------ ------- ------ ------- --------- --------75000 7/18/2008 RedPart 22000 0.31 6820NULL 7/18/2008 RedPart 22000 0.31 6820NULL NULL RedPart 22000 0.31 682070125 3/5/2008 RedPart 5000 0.34 1700NULL 3/5/2008 RedPart 5000 0.34 1700NULL NULL RedPart 5000 0.34 1700NULL NULL RedPart 27000 NULL 852066232 10/23/2007 WhitePart 7000 0.085 595NULL 10/23/2007 WhitePart 7000 0.085 595NULL NULL WhitePart 7000 0.085 595NULL NULL WhitePart 7000 NULL 59570125 3/5/2008 GreenPart 5000 0.274 1370NULL 3/5/2008 GreenPart 5000 0.274 1370NULL NULL GreenPart 5000 0.274 1370NULL NULL GreenPart 5000 NULL 1370NULL NULL Total Ordered 39000 NULL 10485
Now, I'm pretty sure I can programmatically discard the useless rows through some combination of DataView and DataGrid tinkering, but before I go that route, thought I'd check here and see if maybe I'm using ROLLUP correctly.If possible, I want to trim the return to something like this:INVNUM INVDATE PARTNO SHIPQTY UNITPRICE INVTOTAL------ ------- ------ ------- --------- --------75000 7/18/2008 RedPart 22000 0.31 682070125 3/5/2008 RedPart 5000 0.34 1700NULL NULL RedPart 27000 NULL 852066232 10/23/2007 WhitePart 7000 0.085 595NULL NULL WhitePart 7000 NULL 59570125 3/5/2008 GreenPart 5000 0.274 1370NULL NULL GreenPart 5000 NULL 1370NULL NULL Total Ordered 39000 NULL 10485
I was messing around with it earlier doing a CASE statement like the one for aritem.fpartno only on aritem.fprice and I got an error (converting varchar to numeric, even though the field is numeric?) so I suspect I'm doing something just a little wrong here. Does anyone have any ideas? I'd sure appreciate a shove in the right direction.