SQL-Case /无法对包含聚合或子查询的表达式执行聚合函数

时间:2015-03-09 08:30:26

标签: sql sql-server

这是我的代码,我试图开始工作:

DECLARE @DATE AS SMALLDATETIME ,
    @BasTar DATE ,
    @Bittar DATE 

SET @DATE = GETDATE() - 1
SET @BasTar = GETDATE() - 1
SET @Bittar = GETDATE() - 1


SELECT  ISNULL(SUM(CASE WHEN TH.PTYPE IN ( 0, 1, 3, 4, 5 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                        THEN ( TS.DEFAULT_RETURN_PRICE )
                   END), 0) AS SATIST ,
        ISNULL(SUM(CASE WHEN TH.PTYPE IN ( 0, 1, 3, 4, 5 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                        THEN ( TS.VAT_TOTAL )
                   END), 0) AS SATISIV ,
        ISNULL(SUM(CASE WHEN TH.PTYPE IN ( 2, 6 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                        THEN ( TS.DEFAULT_RETURN_PRICE )
                   END), 0) AS IADET ,
        ISNULL(SUM(CASE WHEN TH.PTYPE IN ( 2, 6 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                        THEN ( TS.VAT_TOTAL )
                   END), 0) AS IADEIV ,
        ISNULL(SUM(CASE WHEN TH.PTYPE IN ( 0, 1, 3, 4, 5 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                             AND TH.STATUS = 0 THEN TS.AMOUNT
                   END), 0) AS SATISMIK ,
        ISNULL(SUM(CASE WHEN TH.PTYPE IN ( 2, 6 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                             AND TH.STATUS = 0 THEN TS.AMOUNT
                   END), 0) AS IADEMIK
FROM    TRANSACTION_HEADER TH WITH ( NOLOCK )
        INNER JOIN TRANSACTION_SALE TS WITH ( NOLOCK ) ON TS.FK_TRANSACTION_HEADER = TH.ID
        INNER JOIN STORE ST WITH ( NOLOCK ) ON ST.ID = TH.FK_STORE
                                               AND ST.NUM BETWEEN '3999' AND '9999'
WHERE   TH.STATUS = 0
        AND ST.DESCRIPTION NOT LIKE 'POL_%'
        AND ST.DESCRIPTION NOT LIKE 'SIS%'
        AND CONVERT(CHAR(11), TH.TRANS_DATE, 103) BETWEEN @BasTar
                                                  AND     @Bittar

因为我在case语句中使用ST.ID IN(SELECT ID FROM FLO_AA_MAGAZALAR),它为每个sum(case)语句提供了错误。

  

无法对包含聚合或子查询的表达式执行聚合函数。

1 个答案:

答案 0 :(得分:1)

我不完全确定你要做什么,而且我没有你的架构,所以我无法对此进行测试,但你可以移动SUMming在主要查询之外,这是一个使用CTE;

的示例
DECLARE @DATE AS SMALLDATETIME ,
    @BasTar DATE ,
    @Bittar DATE 

SET @DATE = GETDATE() - 1
SET @BasTar = GETDATE() - 1
SET @Bittar = GETDATE() - 1

; WITH CTE AS (
SELECT  ISNULL(CASE WHEN TH.PTYPE IN ( 0, 1, 3, 4, 5 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                        THEN ( TS.DEFAULT_RETURN_PRICE )
                   END, 0) AS SATIST ,
        ISNULL(CASE WHEN TH.PTYPE IN ( 0, 1, 3, 4, 5 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                        THEN ( TS.VAT_TOTAL )
                   END, 0) AS SATISIV ,
        ISNULL(CASE WHEN TH.PTYPE IN ( 2, 6 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                        THEN ( TS.DEFAULT_RETURN_PRICE )
                   END, 0) AS IADET ,
        ISNULL(CASE WHEN TH.PTYPE IN ( 2, 6 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                        THEN ( TS.VAT_TOTAL )
                   END, 0) AS IADEIV ,
        ISNULL(CASE WHEN TH.PTYPE IN ( 0, 1, 3, 4, 5 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                             AND TH.STATUS = 0 THEN TS.AMOUNT
                   END, 0) AS SATISMIK ,
        ISNULL(CASE WHEN TH.PTYPE IN ( 2, 6 )
                             AND ST.ID IN ( SELECT  ID
                                            FROM    FLO_AA_MAGAZALAR )
                             AND TH.STATUS = 0 THEN TS.AMOUNT
                   END, 0) AS IADEMIK
FROM    TRANSACTION_HEADER TH WITH ( NOLOCK )
        INNER JOIN TRANSACTION_SALE TS WITH ( NOLOCK ) ON TS.FK_TRANSACTION_HEADER = TH.ID
        INNER JOIN STORE ST WITH ( NOLOCK ) ON ST.ID = TH.FK_STORE
                                               AND ST.NUM BETWEEN '3999' AND '9999'
WHERE   TH.STATUS = 0
        AND ST.DESCRIPTION NOT LIKE 'POL_%'
        AND ST.DESCRIPTION NOT LIKE 'SIS%'
        AND CONVERT(CHAR(11), TH.TRANS_DATE, 103) BETWEEN @BasTar
                                                  AND     @Bittar
)
SELECT
    SUM(SATIST) AS SATIST,
    SUM(SATISIV) AS SATISIV,
    SUM(IADET) AS IADET,
    SUM(IADEIV) AS IADEIV,
    SUM(SATISMIK) AS SATISMIK,
    SUM(IADEMIK) AS IADEMIK
FROM
    CTE