获取位列的SUM时分组BY

时间:2015-01-21 13:36:48

标签: sql-server casting group-by sum case

我有一个SQL查询,它返回他们输入的每个工作条目,而不是他们选择是否允许与雇主联系,也包括与该人员相关的ID。这就是表格的外观:

full_name               number_of_dnc_ver   user_profile_id
FATest FATesterlast     No                  260054
FATest FATesterlast     Yes                 260054
FATest FATesterlast     No                  260059
FATest FATesterlast     Yes                 260059
FATest FATesterlast     No                  260064
FATest FATesterlast     Yes                 260064
Domenic Wiley           Yes                 260220
Harold Rojas            Yes                 261550
Harold Rojas            Yes                 262186
Harold Rojas            Yes                 262287
testvvi0236 testvvi0236 No                  263692
Harold Rojas            Yes                 263878
Harold Rojas            Yes                 263893

我试图获得的结果是GROUP BY user_profile_id(相同名称但不同的ID),强制转换" number_of_dnc_ver"作为一个位列并获得总和。因此对于具有260054的FATest FATesterlast,结果将是名称,1和ID。

这是我到目前为止所知道的并且我已经关闭了:

SELECT [tbl_User_Profile].[first_name] + ' ' + [tbl_User_Profile].[last_name] AS full_name,
       SUM(CASE WHEN CONVERT(varchar(MAX),DecryptByKey([tbl_Results].[default_value_binary])) = 'YES' THEN 1 ELSE 0 END) AS number_of_dnc_ver,
       tbl_User_Profile].[user_profile_id]
FROM tbl_Results
INNER JOIN tbl_Controls ON tbl_Results.control_id = tbl_Controls.control_id
INNER JOIN tbl_Default_Controls ON tbl_Controls.default_control_id = tbl_Default_Controls.default_control_id
INNER JOIN tbl_User_Profile ON tbl_User_Profile.user_profile_id = tbl_Results.user_profile_id
WHERE (tbl_User_Profile.application_id = 3707
       AND tbl_Controls.control_name LIKE 'rbl_ContactEmployer_%')
GROUP BY [tbl_User_Profile].[user_profile_id],
         [tbl_User_Profile].[first_name] + ' ' + [tbl_User_Profile].[last_name]

这与我想要的方式很接近,但显然它不包括一些名字。

full_name               number_of_dnc_ver user_profile_id 
Domenic Wiley           1                 260220 
Ha Ro                   2                 262287 
testvvi0236 testvvi0236 0                 263692
Harold Rojas            1                 263902 
FATest FATesterlast     1                 260054 
FATest FATesterlast     1                 260059 
Harold Rojas            1                 263878 
Harold Rojas            1                 261550
Harold Rojas            1                 263893 
Harold Rojas            1                 263907
FATest FATesterlast     1                 260064 
Harold Rojas            1                 262186

1 个答案:

答案 0 :(得分:0)

如果数据在JOINS之后丢失,可能的原因是tbl_Controlstbl_Default_Controlstbl_User_Profile丢失,则来自tbl_Results的等效数据

您可以尝试此查询吗?我评论了tbl_Default_Controls,因为它未在where条件/ Select中使用

SELECT      P.[first_name] + ' ' + P.[last_name] AS full_name,
            SUM(CASE WHEN CONVERT(varchar(MAX),DecryptByKey(R.[default_value_binary])) = 'YES' THEN 1 ELSE 0 END) AS number_of_dnc_ver,
            P.[user_profile_id]
FROM        tbl_Results R
INNER JOIN  tbl_Controls C ON R.control_id = C.control_id
--INNER JOIN    tbl_Default_Controls DC ON C.default_control_id = DC.default_control_id
INNER JOIN  tbl_User_Profile P ON P.user_profile_id = R.user_profile_id
WHERE       (P.application_id = 3707 AND C.control_name LIKE 'rbl_ContactEmployer_%')
GROUP BY    P.[user_profile_id],
            P.[first_name] + ' ' + P.[last_name]