如果今天的日期介于FROMDATE和EXPIRYDATE之间,我试图求和多个折扣值(在客户导出的CSV中)。
如果我将折扣值加起来,一切都很好,但是如果我要检查今天是否在FROMDATE和EXPIRYDATE之间,它会迫使我将“ FROMDATE and EXPIRYDATE”添加到GROUP BY,这会导致重复的行和SUM不起作用。
这是代码,我用**签名有问题的部分
非常感谢您的帮助!
SELECT
CUSTOMERS.CUSTNAME AS 'ERP Code',
CUSTOMERS.CUSTDES AS 'Name',
CUSTOMERS.PHONE AS 'Phone',
(SELECT TOP 1 PHONEBOOK.CELLPHONE FROM PHONEBOOK WHERE
CUSTOMERS.CUST=PHONEBOOK.CUST) AS 'Mobile',
(SELECT TOP 1 PHONEBOOK.NAME FROM PHONEBOOK WHERE CUSTOMERS.CUST=PHONEBOOK.CUST) AS 'Prop1 Contact Name',
CUSTOMERS.FAX AS 'Fax',
CUSTOMERS.ADDRESS AS 'Address',
CUSTOMERS.STATE AS 'City',
CUSTOMERS.ZIP AS 'ZIP Code',
CUSTOMERS.VATNUM + CUSTOMERS.WTAXNUM AS 'Goverment ID',
CUSTOMERSA.EMAIL AS 'Email',
**(SELECT SUM (DISCOUNTS.T$PERCENT) WHERE GETDATE() BETWEEN zoom#system.dbo.tabula_dateconvert(DISCOUNTS.FROMDATE) AND zoom#system.dbo.tabula_dateconvert(DISCOUNTS.EXPIRYDATE)) AS 'Discount',**
ISNULL(CUSTPLIST.PLIST, -1) AS 'Price Level Code',
(SELECT (PAY.PAYDES) FROM PAY WHERE CUSTOMERS.PAY=PAY.PAY) AS 'Prop2'
FROM CUSTOMERS
LEFT OUTER JOIN CUSTOMERSA
ON CUSTOMERS.CUST=CUSTOMERSA.CUST
LEFT OUTER JOIN CUSTDISCOUNT
ON CUSTOMERS.CUST=CUSTDISCOUNT.CUST
LEFT OUTER JOIN DISCOUNTS
ON CUSTDISCOUNT.DISCOUNT=DISCOUNTS.DISCOUNT
LEFT OUTER JOIN CUSTPLIST
ON CUSTOMERS.CUST=CUSTPLIST.CUST
LEFT OUTER JOIN CUSTSTATS
ON CUSTOMERS.CUSTSTAT=CUSTSTATS.CUSTSTAT
WHERE CUSTOMERS.CUST > 0
and CUSTOMERS.CUSTSTAT between -4 and -2
GROUP BY CUSTOMERS.CUSTNAME,CUSTOMERS.CUSTDES,CUSTOMERS.PHONE,CUSTOMERS.CUST,CUSTOMERS.FAX,CUSTOMERS.ADDRESS,CUSTOMERS.STATE,CUSTOMERS.ZIP,CUSTOMERS.VATNUM,CUSTOMERS.WTAXNUM,CUSTOMERSA.EMAIL,CUSTPLIST.PLIST,CUSTOMERS.PAY,**FROMDATE,EXPIRYDATE**
ORDER BY Name;
答案 0 :(得分:0)
不确定,但是CTE可能会在这里有所帮助,请尝试以下方法:
WITH CTE AS
(
SELECT CUSTOMERS.CUSTNAME AS 'ERP Code',
CUSTOMERS.CUSTDES AS 'Name',
CUSTOMERS.PHONE AS 'Phone',
(SELECT TOP 1 PHONEBOOK.CELLPHONE FROM PHONEBOOK WHERE
CUSTOMERS.CUST=PHONEBOOK.CUST) AS 'Mobile',
(SELECT TOP 1 PHONEBOOK.NAME FROM PHONEBOOK WHERE
CUSTOMERS.CUST=PHONEBOOK.CUST) AS 'Prop1 Contact Name',
CUSTOMERS.FAX AS 'Fax',
CUSTOMERS.ADDRESS AS 'Address',
CUSTOMERS.STATE AS 'City',
CUSTOMERS.ZIP AS 'ZIP Code',
CUSTOMERS.VATNUM + CUSTOMERS.WTAXNUM AS 'Goverment ID',
CUSTOMERSA.EMAIL AS 'Email',
**(SELECT SUM (DISCOUNTS.T$PERCENT) WHERE GETDATE() BETWEEN
zoom#system.dbo.tabula_dateconvert(DISCOUNTS.FROMDATE) AND
zoom#system.dbo.tabula_dateconvert(DISCOUNTS.EXPIRYDATE)) AS 'Discount',**
ISNULL(CUSTPLIST.PLIST, -1) AS 'Price Level Code',
(SELECT (PAY.PAYDES) FROM PAY WHERE CUSTOMERS.PAY=PAY.PAY) AS 'Prop2'
FROM CUSTOMERS
LEFT OUTER JOIN CUSTOMERSA
ON CUSTOMERS.CUST=CUSTOMERSA.CUST
LEFT OUTER JOIN CUSTDISCOUNT
ON CUSTOMERS.CUST=CUSTDISCOUNT.CUST
LEFT OUTER JOIN DISCOUNTS
ON CUSTDISCOUNT.DISCOUNT=DISCOUNTS.DISCOUNT
LEFT OUTER JOIN CUSTPLIST
ON CUSTOMERS.CUST=CUSTPLIST.CUST
LEFT OUTER JOIN CUSTSTATS
ON CUSTOMERS.CUSTSTAT=CUSTSTATS.CUSTSTAT
GROUP BY CUSTOMERS.CUSTNAME, CUSTOMERS.CUSTDES, CUSTOMERS.PHONE,
CUSTOMERS.CUST, CUSTOMERS.FAX, CUSTOMERS.ADDRESS, CUSTOMERS.STATE,
CUSTOMERS.ZIP, CUSTOMERS.VATNUM, CUSTOMERS.WTAXNUM,
CUSTOMERSA.EMAIL, CUSTPLIST.PLIST, CUSTOMERS.PAY
)
SELECT * FROM CTE WHERE CUSTOMERS.CUST > 0
and CUSTOMERS.CUSTSTAT between -4 and -2