Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grouping in SQL

Status
Not open for further replies.

livezone

MIS
Jan 17, 2003
81
CA
Hi,
The following query returns this output

Select DATE,LOCN,Product,RETAIL, Sum(QTY) as TotalQty From MyFile
Group by DATE, LOCN, Product, RETAIL
Order By DATE, LOCN, Product, RETAIL DESC

Date LOCN Product Retail Total Qty
20030326 3702 1111 19.97 3
20030326 3702 2222 34.97 2
20030326 3702 3333 149.97 1
20030326 3703 4444 89.97 1
20030326 3703 1111 19.97 6
20030326 3703 1111 -19.97 1

The following query runs ok if there is no refund but if there is a refund as you see the last two lines, I want to convert these two lines

20030326 3703 1111 19.97 6
20030326 3703 1111 -19.97 1

to one line like that

20030326 3703 1111 19.97 5

Qty = 6 -1 = 5

How can I do this in the above SQL Statement

Thanks
 
Select DATE,LOCN,Product,abs(RETAIL),
Sum(case when RETAIL > 0 then QTY
else -QTY end) as TotalQty From MyFile
Group by DATE, LOCN, Product, ABS(RETAIL)
Order By DATE, LOCN, Product, ABS(RETAIL) DESC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top