Tsql唯一记录分组

Tsql唯一记录分组,tsql,group-by,ssms,Tsql,Group By,Ssms,我使用的是SSMS 2008 R2,我试图将此查询的输出压缩为一行/全名。我该怎么做?我的问题是: select top 500 CASE when pv.conversion_id_no is not null then pv.conversion_id_no else prv.id_no end as id_number, eiev.full_name, eiev.dob, ep.start_date as yvdoa, ep.end_date as yvdod, e4pv.actual_d

我使用的是SSMS 2008 R2,我试图将此查询的输出压缩为一行/全名。我该怎么做?我的问题是:

select top 500
CASE when pv.conversion_id_no is not null then pv.conversion_id_no else prv.id_no end as id_number,
eiev.full_name,
eiev.dob,
ep.start_date as yvdoa,
ep.end_date as yvdod,
e4pv.actual_date,
test_header_x.OUTCM_FLWUP_SRVY_PERIOD,
l.description as Call_Location,
case when tqav.question_caption like 'Custody at Follow-up%' then answer_caption end as Custody,
case when tqav.answer_caption like 'Residential Treatment Center%' then answer_is_selected end as RTC,
case when tqav.answer_caption like 'Psychiatric Hospital%' then answer_is_selected end as PsychHosp,
case when tqav.answer_caption like 'Juvenile Detention/Corrections or Adult Jail%' then answer_is_selected end as Corrections,
case when tqav.question_caption like 'Has the youth been attending school?%' then answer_caption end as AttendingSchool,
case when tqav.question_caption like 'Why is the youth not attending school%' then answer_caption end as SchoolStatus,
case when tqav.question_caption like 'Has youth been in trouble with the law/legal system%' then answer_caption end as TroubleLaw

from dbo.rpt_events_4people_view as e4pv 
    JOIN dbo.rpt_test_questions_answers_view as tqav on e4pv.event_log_id = tqav.event_log_id
    left JOIN dbo.enrollment_info_expanded_view as eiev on eiev.people_id = e4pv.people_id
    JOIN dbo.rpt_episod_of_care_program_view as ep on eiev.people_id = ep.people_id  and ep.program_info_id = eiev.program_info
    JOIN dbo.people_reports_view as prv on e4pv.people_id = prv.people_id
    JOIN evolv_cs.dbo.test_details_answers_expanded_view as tdaev on tdaev.event_log_id = e4pv.event_log_id
    JOIN evolv_cs.dbo.user_defined_lut as l ON tdaev.picklist_value = l.user_defined_lut_id
    left outer JOIN dbo.people_rv as pv on eiev.people_id = pv.people_id
    left JOIN [evolv_reports].[dbo].[test_header_rv] With (NoLock) ON  e4pv.[event_log_id] = [test_header_rv].[event_log_id]  
    left JOIN evolv_cs.dbo.Test_header_x With (NoLock) ON  test_header_x.test_header_id = test_header_rv.test_header_id

where e4pv.event_name = 'Outcome Follow-up Survey' 
    and e4pv.actual_date between '10/01/2011' and '03/31/2012'
    and ep.program_name in ('In-Home Services - Intercept')
    and (tqav.question_caption like 'Custody at Follow-up%' or
        tqav.answer_caption like 'Residential Treatment Center%' or
        tqav.answer_caption like 'Psychiatric Hospital%' or
        tqav.answer_caption like 'Juvenile Detention/Corrections or Adult Jail%' or
        tqav.question_caption like 'Has the youth been attending school?%' or
        tqav.question_caption like 'Why is the youth not attending school%' or
        tqav.question_caption like 'Has youth been in trouble with the law/legal system%' )

    and ((eiev.profile_name like '%Lic.# NH%' and ep.start_date >= '02/01/2011') 
            or (eiev.profile_name like '%Lic.# MA%' and ep.start_date >= '02/01/2011') 
            or (eiev.profile_name like '%Lic.# DC%' and ep.start_date >= '05/01/2011')
            or (eiev.profile_name like '%Lic.# NC%' and ep.start_date >= '05/01/2011')
            or (eiev.profile_name like '%Lic.# FL%' and ep.start_date >= '07/18/2011')
            or (eiev.profile_name like '%Lic.# TX%' and ep.start_date >= '07/18/2011')
            or (eiev.profile_name like '%Lic.# AL%' and ep.start_date >= '02/01/2012')
            or (eiev.profile_name like '%Lic.# GA%' and ep.start_date >= '02/01/2012')
            or (eiev.profile_name like '%Lic.# OR%' and ep.start_date >= '03/01/2012'))

    and DATEDIFF (dd,ep.start_date, ep.end_date) >= 60
现在,我的输出如下所示:

id_number   full_name   dob yvdoa   yvdod   actual_date OUTCM_FLWUP_SRVY_PERIOD Call_Location   Custody RTC PsychHosp   Corrections AttendingSchool SchoolStatus    TroubleLaw
10169613    Anderson, Mason 1996-02-06 00:00:00.000 2011-02-07 12:00:00.000 2011-09-04 23:59:00.000 2012-03-30 00:00:00.000 28094354-A0B4-428C-B38A-B90835DAF896    No Response Not available   NULL    NULL    NULL    NULL    NULL    NULL
10169613    Anderson, Mason 1996-02-06 00:00:00.000 2011-02-07 12:00:00.000 2011-09-04 23:59:00.000 2012-03-30 00:00:00.000 28094354-A0B4-428C-B38A-B90835DAF896    No Response NULL    NULL    NULL    NULL    NULL    NULL    NULL
10169613    Anderson, Mason 1996-02-06 00:00:00.000 2011-02-07 12:00:00.000 2011-09-04 23:59:00.000 2012-03-30 00:00:00.000 28094354-A0B4-428C-B38A-B90835DAF896    No Response NULL    NULL    NULL    NULL    NULL    NULL    NULL
10169613    Anderson, Mason 1996-02-06 00:00:00.000 2011-02-07 12:00:00.000 2011-09-04 23:59:00.000 2012-03-30 00:00:00.000 28094354-A0B4-428C-B38A-B90835DAF896    No Response NULL    NULL    NULL    0   NULL    NULL    NULL
10169613    Anderson, Mason 1996-02-06 00:00:00.000 2011-02-07 12:00:00.000 2011-09-04 23:59:00.000 2012-03-30 00:00:00.000 28094354-A0B4-428C-B38A-B90835DAF896    No Response NULL    NULL    0   NULL    NULL    NULL    NULL
10169613    Anderson, Mason 1996-02-06 00:00:00.000 2011-02-07 12:00:00.000 2011-09-04 23:59:00.000 2012-03-30 00:00:00.000 28094354-A0B4-428C-B38A-B90835DAF896    No Response NULL    0   NULL    NULL    NULL    NULL    NULL

因此,您将看到答案字段可以是VARCHAR值,这就是为什么我不能简单地对所有答案求和。

鲜为人知的事实:您实际上可以使用
MAX()
从结果集中选择非空的
VARCHAR

假设您具有以下架构:

TABLE A:
a_id name
1    fruit
2    names

TABLE Vals:
a_id value_type value
1    a          apple
1    b          banana
1    c          carrot
2    a          alice
2    b          bob
2    c          carol
然后,您可以使用
MAX
groupby
在每个值类型的列表中为每个项目获取一行

SELECT A.a_id,
  A.name,
  MAX(CASE WHEN V.value_type = 'a' then value end) as "a",
  MAX(CASE WHEN V.value_type = 'b' then value end) as "b",
  MAX(CASE WHEN V.value_type = 'c' then value end) as "c"
FROM A
  INNER JOIN Vals V on V.a_id = A.a_id
GROUP BY A.a_id, A.name

请在SQLFIDLE上查看:

您是否正在寻找XML路径的
技巧?