DB2 SQL Select语句&通过...分组

时间:2014-02-11 17:00:09

标签: sql db2

我无法正确分组查询。我需要包含TELLERACTIVITY.ENTRY_DATE日期列,以便稍后按日期过滤。但是,我不希望它按TELLERACTIVITY.ENTRY_DATE日期分组,因为它给了我比我想要的更多结果。

SELECT DISTINCT ACTIVITYINFO.MEMBER_NBR AS "Member Number"
   ,ACCOUNT.ACCOUNT_NBR AS "Account Number"
   ,TELLERACTIVITY.FOCUS_TELLER_ID AS "Teller ID"
   ,TELLERACTIVITY.ENTRY_DATE AS "Date"
   ,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 1 THEN 1
               ELSE 0
          END )) AS "Branch 1"
   ,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 2 THEN 1
               ELSE 0
          END )) AS "Branch 2"
   ,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 3 THEN 1
               ELSE 0
          END )) AS "Branch 3"
   ,SUM(( CASE WHEN OPERATOR.BRANCH_NBR = 4 THEN 1
               ELSE 0
          END )) AS "Branch 4"
FROM TELLERACTIVITY
JOIN OPERATOR
    ON TELLERACTIVITY.FOCUS_TELLER_ID = OPERATOR.OPR_NBR
JOIN ACTIVITY
    ON TELLERACTIVITY.ACTIVITY_ID = ACTIVITY.ACTIVITY_ID
       AND TELLERACTIVITY.FOCUS_TELLER_ID = ACTIVITY.FOCUS_TELLER_ID
       AND TELLERACTIVITY.ENTRY_DATE = ACTIVITY.ENTRY_DATE
JOIN ACTIVITYINFO
    ON ACTIVITY.ACTIVITY_ID = ACTIVITYINFO.ACTIVITY_ID
JOIN ACCOUNT
    ON ACTIVITYINFO.MEMBER_NBR = ACCOUNT.MEMBER_NBR
       AND ACTIVITYINFO.ACCOUNT_NBR = ACCOUNT.ACCOUNT_NBR
WHERE TELLERACTIVITY.FOCUS_TELLER_ID < 6000
GROUP BY ACTIVITYINFO.MEMBER_NBR
   ,ACCOUNT.ACCOUNT_NBR
   ,OPERATOR.BRANCH_NBR
   ,TELLERACTIVITY.FOCUS_TELLER_ID
   ,TELLERACTIVITY.ENTRY_DATE
ORDER BY ACTIVITYINFO.MEMBER_NBR

0 个答案:

没有答案