应该很简单,但不能使它工作。还看了其他类似的答案。此代码生成报告。有三个主要类别:一般调整,财务费用和坏账。我想要完成的任务......
如果这三列全部都是零,则删除该行。就是这样。
DECLARE @startdate DATETIME
DECLARE @lastdate DATETIME
SET @startdate = '20170701'
SET @lastdate = '20170731'
CREATE TABLE #a
(
ClientName VARCHAR(50),
JobName VARCHAR(50),
JobPartner VARCHAR(40),
[Date] DATETIME,
GenAdj MONEY,
FC MONEY,
BadDebt MONEY,
nomid INT
)
SELECT
C.searchname AS 'CLIENT NAME',
n.date AS 'DATE',
SUM(CASE WHEN NOMTYPEID=3 AND NOMSUBTYPEID=2 THEN TOTAL else 0 END) AS 'GENERAL ADJUSTMENTS',
SUM(CASE WHEN NOMTYPEID=3 AND NOMSUBTYPEID=1 THEN TOTAL else 0 END) AS 'FINANCE CHARGES',
SUM(CASE WHEN NOMTYPEID=3 AND NOMSUBTYPEID=0 THEN TOTAL else 0 END) AS 'BAD DEBTS',
sl.serviceline AS 'DEPARTMENT',
p.name AS 'PARTNER NAME'
FROM
tblNominal N
JOIN
tblNominalAccs A ON N.ContNominalID = A.NominalID
JOIN
tblclient C on N.clientid = C.clientid
LEFT JOIN
tbljob j ON n.jobid = j.JobID
LEFT JOIN
tblpartner p ON j.PartnerID = p.PartnerID
LEFT JOIN
tbljobtype jt ON j.jobtypeid = jt.jobtypeid
LEFT JOIN
tblserviceline sl ON j.servicelineid = sl.servicelineid
WHERE
Date >= @startdate AND DATE <= @LASTDATE
GROUP BY
C.searchname, n.date, sl.serviceline, p.name
ORDER BY
'CLIENT NAME' asc, 'FINANCE CHARGES' asc
DROP TABLE #a
我想我可能会过度思考这个问题。如何删除全零的行?
感谢您的阅读。
答案 0 :(得分:6)
您需要在HAVING
后GROUP BY
之前使用ORDER BY
条款:
HAVING SUM(CASE WHEN NOMTYPEID=3 AND NOMSUBTYPEID=2 THEN TOTAL else 0 END) > 0
OR SUM(CASE WHEN NOMTYPEID=3 AND NOMSUBTYPEID=1 THEN TOTAL else 0 END) > 0
OR SUM(CASE WHEN NOMTYPEID=3 AND NOMSUBTYPEID=0 THEN TOTAL else 0 END) > 0
HAVING
有点像小组结果的WHERE
。