是否有可能将'结果'结果连成一列?

时间:2016-08-17 21:52:43

标签: sql-server

在下面的SQL中,我遇到了将结果连接到一列时出现问题的问题。我希望B.DAYNUM字段以下列方式显示结果:太阳,周一,周二,周三等。计划的日期将每周变化。任何建议都将不胜感激。

SELECT A.SHORTNM AS 'SCHEDULE GROUP'
,RTRIM(RIGHT(CONVERT(VARCHAR(10),A.STARTDTM,110),10)) AS STARTDATE
,A.SHORTNM AS 'PATTERN TEMPLATE' 
,A.LONGDSC AS 'PATTERN DESCRIPTION'
,A.DAYCNT AS 'DAYS IN PATTERN'
,C.LABELNM AS 'SHIFT'
,LTRIM(RIGHT(CONVERT(VARCHAR(20),E.STARTDTM,100),7)) AS SHIFTSTART
,LTRIM(RIGHT(CONVERT(VARCHAR(20),G.STARTDTM,100),7)) AS BREAKSTART
,LTRIM(RIGHT(CONVERT(VARCHAR(20),H.ENDDTM,100),7)) AS BREAKEND
,CASE WHEN F1.ORDERNUM = 3 THEN 
    LTRIM(RIGHT(CONVERT(VARCHAR(20),F1.ENDDTM,100),7)) 
    ELSE LTRIM(RIGHT(CONVERT(VARCHAR(20),F.ENDDTM,100),7)) END AS SHIFTEND1
,CASE WHEN B.DAYNUM = 1 THEN 'SUN'
    WHEN B.DAYNUM = 2 THEN 'MON'
    WHEN B.DAYNUM = 3 THEN 'TUES'
    WHEN B.DAYNUM = 4 THEN 'WED'
    WHEN B.DAYNUM = 5 THEN 'THURS'
    WHEN B.DAYNUM = 6 THEN 'FRI'
    WHEN B.DAYNUM = 7 THEN 'SAT'
    ELSE '' END AS 'SCHED DAYS-WK 1'
,CASE WHEN B.DAYNUM = 8 THEN 'SUN'
    WHEN B.DAYNUM = 9 THEN 'MON'
    WHEN B.DAYNUM = 10 THEN 'TUES'
    WHEN B.DAYNUM = 11 THEN 'WED'
    WHEN B.DAYNUM = 12 THEN 'THURS'
    WHEN B.DAYNUM = 13 THEN 'FRI'
    WHEN B.DAYNUM = 14 THEN 'SAT'
    ELSE '' END AS 'SCHED DAYS-WK 2'
,CASE WHEN B.DAYNUM = 15 THEN 'SUN'
    WHEN B.DAYNUM = 16 THEN 'MON'
    WHEN B.DAYNUM = 17 THEN 'TUES'
    WHEN B.DAYNUM = 18 THEN 'WED'
    WHEN B.DAYNUM = 19 THEN 'THURS'
    WHEN B.DAYNUM = 20 THEN 'FRI'
    WHEN B.DAYNUM = 21 THEN 'SAT'
    ELSE '' END AS "Sched Days-Week 3"
,CASE WHEN B.DAYNUM = 22 THEN 'SUN'
    WHEN B.DAYNUM = 23 THEN 'MON'
    WHEN B.DAYNUM = 24 THEN 'TUES'
    WHEN B.DAYNUM = 25 THEN 'WED'
    WHEN B.DAYNUM = 26 THEN 'THURS'
    WHEN B.DAYNUM = 27 THEN 'FRI'
    WHEN B.DAYNUM = 28 THEN 'SAT'
    ELSE '' END AS "Sched Days-Week 4"
,CASE WHEN B.DAYNUM = 29 THEN 'SUN'
    WHEN B.DAYNUM = 30 THEN 'MON'
    WHEN B.DAYNUM = 31 THEN 'TUES'
    WHEN B.DAYNUM = 32 THEN 'WED'
    WHEN B.DAYNUM = 33 THEN 'THURS'
    WHEN B.DAYNUM = 34 THEN 'FRI'
    WHEN B.DAYNUM = 35 THEN 'SAT'
    ELSE '' END AS "Sched Days-Week 5"
,CASE WHEN B.DAYNUM = 36 THEN 'SUN'
    WHEN B.DAYNUM = 37 THEN 'MON'
    WHEN B.DAYNUM = 38 THEN 'TUES'
    WHEN B.DAYNUM = 39 THEN 'WED'
    WHEN B.DAYNUM = 40 THEN 'THURS'
    WHEN B.DAYNUM = 41 THEN 'FRI'
    WHEN B.DAYNUM = 42 THEN 'SAT'
    ELSE '' END AS "Sched Days-Week 6"
,CASE WHEN B.DAYNUM = 43 THEN 'SUN'
    WHEN B.DAYNUM = 44 THEN 'MON'
    WHEN B.DAYNUM = 45 THEN 'TUES'
    WHEN B.DAYNUM = 46 THEN 'WED'
    WHEN B.DAYNUM = 47 THEN 'THURS'
    WHEN B.DAYNUM = 48 THEN 'FRI'
    WHEN B.DAYNUM = 49 THEN 'SAT'
    ELSE '' END AS "Sched Days-Week 7"
,CASE WHEN B.DAYNUM = 50 THEN 'SUN'
    WHEN B.DAYNUM = 51 THEN 'MON'
    WHEN B.DAYNUM = 52 THEN 'TUES'
    WHEN B.DAYNUM = 53 THEN 'WED'
    WHEN B.DAYNUM = 54 THEN 'THURS'
    WHEN B.DAYNUM = 55 THEN 'FRI'
    WHEN B.DAYNUM = 56 THEN 'SAT'
    ELSE '' END AS "Sched Days-Week 8"
FROM SCHEDULEPATTRN A 
JOIN SCHEDPATTRNDAY B ON A.SCHEDULEPATTRNID = B.SCHEDULEPATTRNID
JOIN SHIFTCODE C ON B.SHIFTCODEID = C.SHIFTCODEID
LEFT JOIN SHIFTSEGMENT E ON C.SHIFTCODEID = E.SHIFTCODEID AND E.ORDERNUM = 1
LEFT JOIN SHIFTSEGMENT F ON C.SHIFTCODEID = F.SHIFTCODEID AND F.SHIFTSEGMNTTYPID = 1 AND F.ORDERNUM = 1
LEFT JOIN SHIFTSEGMENT F1 ON C.SHIFTCODEID = F1.SHIFTCODEID AND F1.SHIFTSEGMNTTYPID = 1 AND F1.ORDERNUM = 3
LEFT JOIN SHIFTSEGMENT G ON C.SHIFTCODEID = G.SHIFTCODEID AND G.SHIFTSEGMNTTYPID = 4 AND G.ORDERNUM = 2
LEFT JOIN SHIFTSEGMENT H ON C.SHIFTCODEID = H.SHIFTCODEID AND H.SHIFTSEGMNTTYPID = 4 AND H.ORDERNUM = 2
JOIN TIMEPERIODTYPE I ON A.TIMEPERIODTYPEID = I.TIMEPERIODTYPEID
AND A.TEMPLATESW = 1
AND A.SHORTNM NOT LIKE 'z%'
ORDER BY A.SHORTNM,B.DAYNUM

0 个答案:

没有答案