2012-05-02 4 views
0

Я использую SSMS 2008 R2, и я пытаюсь сконденсировать вывод из этого запроса в одну строку/полное_имя. Как мне это сделать? Вот мой запрос:Уникальная группировка записей Tsql

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, поэтому я не могу просто SUM все ответы.

+0

Вы ищете для 'FOR XML PATH' трюк с [значений CONCATENATE] (http://stackoverflow.com/a/7654396/569436)? –

ответ

1

Малоизвестный факт: вы можете использовать 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 и GROUP BY, чтобы получить одну строку для каждого элемента в А перечисляя каждый тип значения.

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 

Посмотри на sqlfiddle здесь: http://sqlfiddle.com/#!3/aba0a/1

Смежные вопросы