Dong Lam Trien

Dong Lam Trien

  • 830
  • 986
  • 147.3k

Error query sql server group and sum into one line ?

Jun 27 2023 2:47 AM

you see my image and sql file why not sum and group into one line ?

SELECT DISTINCT 
    TOP (100) PERCENT dbo.TABHDBHCT.MACUAHANG, dbo.TABHDBHCT.MAHDBH, dbo.TABHDBH.MABAN, dbo.TABHDBH.SOPHIEU, 
    SUM(dbo.TABHDBHCT.SOLUONG * dbo.TABHDBHCT.GIABAN) AS TIENHANG, 
    SUM(dbo.TABHDBHCT.SOLUONG * CASE WHEN [MANHOM] = 14 THEN TABHDBHCT.GIABAN ELSE 0 END) AS TTGIO, dbo.TABHDBH.GIAMPGIO, 
    SUM(dbo.TABHDBH.GIAMTGIO) AS TGIAMTGIO, SUM(dbo.TABHDBHCT.SOLUONG * dbo.TABHDBHCT.GIAMTIENCK) AS TGIAMTIENCK, 
    SUM(dbo.TABHDBHCT.SOLUONG * CASE WHEN [MANHOM] <> 14 THEN (TABHDBHCT.GIABAN - TABHDBHCT.GIAMTIENCK) ELSE 0 END) AS TTDU, 
    dbo.TABHDBH.GIAMPDU, SUM(dbo.TABHDBH.GIAMTDU) AS TGIAMTDU, 
    SUM(dbo.TABHDBHCT.SOLUONG * (dbo.TABHDBHCT.GIABAN - dbo.TABHDBHCT.GIAMTIENCK)) AS TONGTIEN
FROM         dbo.TABHDBHCT INNER JOIN
    dbo.TABHDBH ON dbo.TABHDBHCT.MAHDBH = dbo.TABHDBH.IDHDBH
WHERE     (dbo.TABHDBHCT.MAHDBH = 62) AND (dbo.TABHDBHCT.MACUAHANG = 1) AND (dbo.TABHDBHCT.DEL = 0) AND (dbo.TABHDBH.DEL = 0)
GROUP BY dbo.TABHDBHCT.MACUAHANG, dbo.TABHDBHCT.MAHDBH, dbo.TABHDBH.MABAN, dbo.TABHDBH.SOPHIEU, dbo.TABHDBHCT.SOLUONG, 
    dbo.TABHDBHCT.GIABAN, dbo.TABHDBHCT.GIAMTIENCK, dbo.TABHDBHCT.MANHOM, dbo.TABHDBH.GIAMPGIO, dbo.TABHDBH.GIAMTGIO, dbo.TABHDBH.GIAMPDU, 
    dbo.TABHDBH.GIAMTDU
ORDER BY dbo.TABHDBHCT.MAHDBH


Answers (4)