Group BY 同时获取 SUM of Bit 列

Group BY while getting SUM of Bit columns

我有一个 SQL 查询 returns 他们输入的每个就业条目,而不是他们选择是否允许联系雇主,以及与该人相关的 ID。这是 table 的样子:

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

如果在JOINS之后数据丢失,可能的原因是tbl_Controlstbl_Default_Controlstbl_User_Profile丢失,相当于[=15]的数据=].

你能试试这个查询吗?我注释掉了 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]