减少记录收益

时间:2016-01-15 17:48:08

标签: sql sql-server tsql

我的查询大部分都正常运行,但根据屏幕截图here和下面的示例,我遇到了以下问题:

1)Expr1列返回我通过查询请求的正确总数,但正如您所见,通过COMPANY重复。使用DISTINCT没有帮助,我的子查询是我如何检索由COMPANY描绘的总数,但它仍然显示重复的值。

2)关于如何在每个公司获得一行Expr1的总量的建议是我正在拍摄的。谢谢。

这是我的代码:

    SELECT Activity.ACTIVITY_TYPE, 
           Activity.TRANSACTION_DATE, 
           Activity.PRODUCT_CODE, 
           Activity.DESCRIPTION, 
           Name.ID, 
           Name.MEMBER_TYPE, 
           Name.STATUS,             
           Name.COMPANY, 
           Demo_Chapter.CH_COL_REGION, 
           Demo_Ch_Membership.CM_CURRENT_INIT, 
           Demo_Chapter.CH_CHAP_NUMBER,
           (SELECT DISTINCT COUNT(a2.PRODUCT_CODE) AS Expr1
           FROM Activity AS a2 INNER JOIN
           Name AS n2 ON a2.ID = n2.ID
           WHERE (n2.COMPANY = Name.COMPANY)
           AND (a2.PRODUCT_CODE = 'aaol') 
           AND (a2.TRANSACTION_DATE BETWEEN CONVERT(DATETIME, '2015-08-01 00:00:00', 102)
           AND CONVERT(DATETIME, '2016-07-31 00:00:00', 102))
           GROUP BY n2.COMPANY) AS Expr1
           FROM Name INNER JOIN
            Demo_Individual ON Name.ID = Demo_Individual.ID 
           INNER JOIN
           Activity ON Name.ID = Activity.ID 
           INNER JOIN
           Demo_Chapter ON Name.CO_ID = Demo_Chapter.ID 
           INNER JOIN
           Demo_Ch_Membership ON Name.CO_ID = Demo_Ch_Membership.ID
           WHERE (Name.MEMBER_TYPE = 'COL') 
           AND (Name.STATUS IN ('A', 'ABR')) 
           AND (Activity.ACTIVITY_TYPE IN ('FIDELITY', 'FIDELITYOL')) 
           AND (Activity.TRANSACTION_DATE BETWEEN CONVERT(DATETIME, '2015-08-01 00:00:00', 102) AND CONVERT(DATETIME, '2016-07-31 00:00:00', 102))
           AND (Activity.PRODUCT_CODE = 'aaol')

以下是输出样本(注意重复信息):

CH_CHAP_NUMBER   COMPANY        CH_Col_REGION    CM_CURRENT_INIT    Expr1
--------------------------------------------------------------------------
    024         Omega Chapter      REG_5              160            121
    024         Omega Chapter      REG_5              160            121
    161        Zeta Rho Chapter    REG_3              282            162
    161        Zeta Rho Chapter    REG_3              282            162
    161        Zeta Rho Chapter    REG_3              282            162
    etc

所需的输出如下:

CH_CHAP_NUMBER   COMPANY        CH_Col_REGION    CM_CURRENT_INIT    Expr1
--------------------------------------------------------------------------
    024         Omega Chapter      REG_5              160            121
    161        Zeta Rho Chapter    REG_3              282            162
    etc  

更广泛的完成输出:

ACTIVITY_TYPE TRANSACTION_DATE        PRODUCT_CODE                    MEMBER_TYPE STATUS COMPANY                                                                          CH_COL_REGION                  CM_CURRENT_INIT CH_CHAP_NUMBER Expr1
------------- ----------------------- ------------------------------- ----------- ------ -------------------------------------------------------------------------------- ------------------------------ --------------- -------------- -----------
FIDELITYOL    2015-09-01 13:18:00.000 AAOL                            COL         A      Omega Chapter                                                                    REG_5                          160             024            121
FIDELITYOL    2015-09-01 11:47:00.000 AAOL                            COL         A      Omega Chapter                                                                    REG_5                          160             024            121
FIDELITYOL    2015-09-01 21:24:00.000 AAOL                            COL         A      Zeta Rho Chapter                                                                 REG_3                          282             161            162
FIDELITYOL    2015-09-07 15:39:00.000 AAOL                            COL         A      Zeta Rho Chapter                                                                 REG_3                          282             161            162
FIDELITYOL    2015-08-31 12:23:00.000 AAOL                            COL         A      Zeta Rho Chapter                                                                 REG_3                          282             161            162
FIDELITYOL    2015-12-07 00:00:00.000 AAOL                            COL         A      Gamma Psi Chapter                                                                REG_5                          84              095            56
FIDELITYOL    2015-09-07 16:01:00.000 AAOL                            COL         A      Beta Sigma Chapter                                                               REG_8                          172             066            6
FIDELITYOL    2015-09-08 09:14:00.000 AAOL                            COL         A      Beta Eta Chapter                                                                 REG_5                          127             055            85
FIDELITYOL    2015-09-07 22:44:00.000 AAOL                            COL         A      Epsilon Nu Chapter                                                               REG_7                          189             133            129
FIDELITYOL    2015-08-31 20:50:00.000 AAOL                            COL         A      Epsilon Pi Chapter                                                               REG_2                          149             136            68
FIDELITYOL    2015-09-01 08:03:00.000 AAOL                            COL         A      Zeta Rho Chapter                                                                 REG_3                          282             161            162
FIDELITYOL    2015-08-31 22:19:00.000 AAOL                            COL         A      Epsilon Pi Chapter                                                               REG_2                          149             136            68
FIDELITYOL    2015-09-05 16:29:00.000 AAOL                            COL         A      Zeta Rho Chapter                                                                 REG_3                          282             161            162
FIDELITYOL    2015-09-07 22:25:00.000 AAOL                            COL         A      Beta Sigma Chapter                                                               REG_8                          172             066            6
FIDELITYOL    2015-09-01 20:58:00.000 AAOL                            COL         A      Beta Sigma Chapter                                                               REG_8                          172             066            6
FIDELITYOL    2015-09-07 13:25:00.000 AAOL                            COL         A      Zeta Rho Chapter                                                                 REG_3                          282             161            162
FIDELITYOL    2015-09-07 16:14:00.000 AAOL                            COL         A      Zeta Rho Chapter                                                                 REG_3                          282             161            162
FIDELITYOL    2015-09-04 23:34:00.000 AAOL                            COL         A      Zeta Rho Chapter                                                                 REG_3                          282             161            162
FIDELITYOL    2015-09-07 14:27:00.000 AAOL                            COL         A      Zeta Rho Chapter                                                                 REG_3                          282             161            162

2 个答案:

答案 0 :(得分:0)

你能看出这是否有效,在不知道你的数据是什么的情况下很难说清楚:

DECLARE @transDateStart DATETIME='20150801';
DECLARE @transDateEnd DATETIME='20160731';
DECLARE @productCode VARCHAR(10) = 'aaol';

WITH CompanyActivityCount
     AS
        (SELECT DISTINCT       COUNT(a2.PRODUCT_CODE) AS Expr1, 
                       n2.COMPANY
         FROM Activity AS a2
         INNER JOIN
         Name AS n2
         ON a2.ID = n2.ID
         WHERE a2.PRODUCT_CODE = @productCode
               AND a2.TRANSACTION_DATE BETWEEN @transDateStart AND @transDateEnd
         GROUP BY n2.COMPANY
        )
     SELECT DISTINCT    Activity.ACTIVITY_TYPE, 
                    Activity.TRANSACTION_DATE, 
                    Activity.PRODUCT_CODE, 
                    Activity.DESCRIPTION, 
                    Name.ID, 
                    Name.MEMBER_TYPE, 
                    Name.STATUS, 
                    Name.COMPANY, 
                    Demo_Chapter.CH_COL_REGION, 
                    Demo_Ch_Membership.CM_CURRENT_INIT, 
                    Demo_Chapter.CH_CHAP_NUMBER, 
                    CompanyActivityCount.Expr1
     FROM Name
     INNER JOIN
     Demo_Individual
     ON Name.ID = Demo_Individual.ID
     INNER JOIN
     Activity
     ON Name.ID = Activity.ID
     INNER JOIN
     Demo_Chapter
     ON Name.CO_ID = Demo_Chapter.ID
     INNER JOIN
     Demo_Ch_Membership
     ON Name.CO_ID = Demo_Ch_Membership.ID
     LEFT OUTER JOIN
     CompanyActivityCount
     ON CompanyActivityCount.COMPANY = Name.COMPANY
     WHERE Name.MEMBER_TYPE = 'COL'
           AND Name.STATUS IN ('A', 'ABR')
           AND Activity.ACTIVITY_TYPE IN ('FIDELITY', 'FIDELITYOL')
           AND Activity.TRANSACTION_DATE BETWEEN @transDateStart AND @transDateEnd
           AND Activity.PRODUCT_CODE = @productCode;

由于没有嵌套的子查询,它应该会更好一些。

答案 1 :(得分:-2)

只需在选择

中使用DISTINCT即可