0

私は SSMS 2008 R2 を使用しており、このクエリからの出力を 1 行 / full_name に要約しようとしています。どうすればいいですか?これが私のクエリです:

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 値である可能性があることがわかります。そのため、すべての回答を単純に合計することはできません。

4

1 に答える 1

1

あまり知られていない事実: 実際には を使用して、結果セットからMAX()null 以外を選択できます。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

次に、 と を使用MAXGROUP BYて、各値タイプをリストする A の項目ごとに 1 行を取得できます。

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

于 2012-05-02T20:32:52.713 に答える