我有一张表可以返回如下记录:
Name Total_Case_Count User_Case_Count P_Count Rej_Count PPP_Count Other_Count
ABC 20 10 03
ABC 20 10 05
ABC 20 10 02
XYZ 20 10 05
XYZ 20 10 02
XYZ 20 10 01
XYZ 20 10 02
但我需要这样的结果:
Name Total_Case_Count User_Case_Count P_Count Rej_Count PPP_Count Other_Count
ABC 20 10 03 05 02
XYZ 20 10 05 02 01 02
表示用户数和&的总和其他计数将在同一行。我正在使用此查询:
select distinct result.NAME, result.USER_ACCOUNT_ID,Total_Case_Count,User_Case_COUNT,Pending_Case_Count,
Rejected_Case_Count,Pending_For_Payment_Case_Count,Active_Case_Count FROM
(
select row_number() over (order by C.CASE_ID asc) as row_index,
UA.USER_ACCOUNT_ID,
UA.FIRST_NAME ||' '||UA.LAST_NAME AS NAME,
COUNT(*) OVER () Total_Case_Count,
COUNT(*) OVER (PARTITION BY UA.USER_ACCOUNT_ID) User_Case_COUNT,
CASE
WHEN C.CASE_STATUS_ID = 2 THEN COUNT(*) OVER (PARTITION BY C.CASE_STATUS_ID,UA.USER_ACCOUNT_ID) end as Pending_Case_Count,
CASE
WHEN C.CASE_STATUS_ID = 4 THEN COUNT(*) OVER (PARTITION BY C.CASE_STATUS_ID,UA.USER_ACCOUNT_ID) end as Rejected_Case_Count,
CASE
WHEN C.CASE_STATUS_ID = 6 THEN COUNT(*) OVER (PARTITION BY C.CASE_STATUS_ID,UA.USER_ACCOUNT_ID) end as Pending_For_Payment_Case_Count,
CASE
WHEN C.CASE_STATUS_ID In (1,3,5,7,8,9) THEN COUNT(*) OVER (PARTITION BY C.CASE_STATUS_ID,UA.USER_ACCOUNT_ID) end as Active_Case_Count
FROM CASE C
INNER JOIN CASE_STATUS CS ON CS.CASE_STATUS_ID = C.CASE_STATUS_ID
INNER JOIN SSO.USER_ACCOUNTS UA ON UA.USER_ACCOUNT_ID = C.CREATED_BY
inner join sso.User_In_Types uit on uit.USER_ACCOUNT_ID = UA.USER_ACCOUNT_ID
inner join SSO.USER_TYPES ut on UT.USER_TYPE_ID = UiT.USER_TYPE_ID
where UT.APPLICATION_ID = 12 and UT.USER_TYPE_ID = 2170
) result
ORDER BY NAME
任何建议都非常感谢..
答案 0 :(得分:1)
您可以在外部select
中使用聚合:
select result.NAME, result.USER_ACCOUNT_ID,MAX(Total_Case_Count),MAX(User_Case_COUNT),MAX(Pending_Case_Count),
MAX(Rejected_Case_Count),MAX(Pending_For_Payment_Case_Count),MAX(Active_Case_Count) FROM
(
select row_number() over (order by C.CASE_ID asc) as row_index,
UA.USER_ACCOUNT_ID,
UA.FIRST_NAME ||' '||UA.LAST_NAME AS NAME,
COUNT(*) OVER () Total_Case_Count,
COUNT(*) OVER (PARTITION BY UA.USER_ACCOUNT_ID) User_Case_COUNT,
CASE
WHEN C.CASE_STATUS_ID = 2 THEN COUNT(*) OVER (PARTITION BY C.CASE_STATUS_ID,UA.USER_ACCOUNT_ID) end as Pending_Case_Count,
CASE
WHEN C.CASE_STATUS_ID = 4 THEN COUNT(*) OVER (PARTITION BY C.CASE_STATUS_ID,UA.USER_ACCOUNT_ID) end as Rejected_Case_Count,
CASE
WHEN C.CASE_STATUS_ID = 6 THEN COUNT(*) OVER (PARTITION BY C.CASE_STATUS_ID,UA.USER_ACCOUNT_ID) end as Pending_For_Payment_Case_Count,
CASE
WHEN C.CASE_STATUS_ID In (1,3,5,7,8,9) THEN COUNT(*) OVER (PARTITION BY C.CASE_STATUS_ID,UA.USER_ACCOUNT_ID) end as Active_Case_Count
FROM CASE C
INNER JOIN CASE_STATUS CS ON CS.CASE_STATUS_ID = C.CASE_STATUS_ID
INNER JOIN SSO.USER_ACCOUNTS UA ON UA.USER_ACCOUNT_ID = C.CREATED_BY
inner join sso.User_In_Types uit on uit.USER_ACCOUNT_ID = UA.USER_ACCOUNT_ID
inner join SSO.USER_TYPES ut on UT.USER_TYPE_ID = UiT.USER_TYPE_ID
where UT.APPLICATION_ID = 12 and UT.USER_TYPE_ID = 2170
) result
GROUP BY result.NAME, result.USER_ACCOUNT_ID
ORDER BY NAME
答案 1 :(得分:1)
我不确定我是否完全理解您的问题,但您不能只使用group by
吗?
SELECT Name
,Total_Case_Count
,SUM(User_Case_Count)
,SUM(P_Count)
,SUM(Rej_Count)
,SUM(PPP_Count)
,SUM(Other_Count)
FROM (Your table Here)
GROUP BY Name
,Total_Case_Count
答案 2 :(得分:1)
我想你需要简单的Group by
和Aggregate
SELECT Name,
Total_Case_Count,
User_Case_Count,
Max(P_Count),
Max(Rej_Count),
Max(PPP_Count),
Max(Other_Count)
FROM Yourtable
GROUP BY Name,
Total_Case_Count,
User_Case_Count