我有一个表格,其数据如下:
case_id person_id provider_group_ID PROVIDER_GROUP_FUNCTION case_date
12421 1111 SLS_D2 BPSC_Service 2014-10-07
12422 1111 SLS_D2 BPSC Service 2014-10-07
12423 1111 SLS_D2 BPSC Service 2014-10-07
我还有一个名为TeleComData的表,如:
PersonID provider_group_id provider_group_function case_date NUM_CALLS
1111 SLS_D2 BPSC_Service 2014-10-07 8
1111 BPSC BPSC_Service 2014-10-07 0
我想要的是
CaseCount PROVIDER_GROUP_ID PROVIDER_GROUP_FUNCTION case_date num_calls
3 SLS_D2 BPSC_Service 2014-10-07 8
0 BPSC BPSC_Service 2014-10-07 0
但截至目前,我得到以下内容
CaseCount PROVIDER_GROUP_ID PROVIDER_GROUP_FUNCTION case_date num_calls
3 SLS_D2 BPSC_Service 2014-10-07 8
3 BPSC BPSC_Service 2014-10-07 0
我在两个表之间做了一个左外连接,而不是根据特定的PROVIDER_GROUP_ID对Cases进行计数,它会复制它。 我似乎无法弄清楚我的代码是怎么回事。这是我到目前为止的作品:
SELECT COUNT(DISTINCT CASE_ID) FROM
dbo.DW_TELECOM_DATA B
LEFT OUTER JOIN Clear.T_CLEAR_SERVICE A ON
A.Case_date = B.Case_date
AND A.PERSON_ID = B.PERSON_ID
答案 0 :(得分:2)
试试这个。在ON
Left Outer Join
条件
CREATE TABLE #e
(
case_id INT,
person_id INT,
provider_group_ID VARCHAR(100),
PROVIDER_GROUP_FUNCTION VARCHAR(100),
case_date DATE
)
INSERT #e
VALUES (12421,1111,'SLS_D2','BPSC_Service','2014-10-07'),
(12422,1111,'SLS_D2','BPSC Service','2014-10-07'),
(12423,1111,'SLS_D2','BPSC Service','2014-10-07')
CREATE TABLE #f
(
PersonID INT,
provider_group_ID VARCHAR(100),
PROVIDER_GROUP_FUNCTION VARCHAR(100),
case_date DATE,
NUM_CALLS INT
)
INSERT #f
VALUES (1111,'SLS_D2','BPSC_Service','2014-10-07',8),
(1111,'BPSC','PSC_Service','2014-10-07',0)
SELECT COUNT(CASE_ID) CaseCount, b.provider_group_ID, b.PROVIDER_GROUP_FUNCTION,
b.case_date,b.NUM_CALLS
FROM #f B
LEFT OUTER JOIN #e A
ON A.CASE_DATE = B.CASE_DATE
AND A.PERSON_ID = B.PERSONID
AND A.PROVIDER_GROUP_ID = B.PROVIDER_GROUP_ID
group by b.provider_group_ID,
b.PROVIDER_GROUP_FUNCTION,b.case_date,b.NUM_CALLS
或
SELECT DISTINCT Count(CASE_ID)OVER (partition BY b.provider_group_ID) CaseCount,
b.provider_group_ID,
b.PROVIDER_GROUP_FUNCTION,
b.case_date,b.NUM_CALLS
FROM #f B
LEFT OUTER JOIN #e A
ON A.CASE_DATE = B.CASE_DATE
AND A.PERSON_ID = B.PERSONID
AND A.PROVIDER_GROUP_ID = B.PROVIDER_GROUP_ID
答案 1 :(得分:0)
SELECT B.PROVIDER_GROUP_ID, B.PROVIDER_GROUP_FUNCTION, B.case_date
, COUNT(DISTINCT A.CASE_ID)
FROM dbo.DW_TELECOM_DATA B
LEFT OUTER JOIN Clear.T_CLEAR_SERVICE A
ON A.Case_date = B.Case_date
AND A.PERSON_ID = B.PERSON_ID
AND A.provider_group_ID = A.provider_group_ID
group by B.PROVIDER_GROUP_ID, B.PROVIDER_GROUP_FUNCTION, B.case_date