SQL Sub Query将结果拆分为两行

时间:2014-07-17 09:19:31

标签: sql join subquery case multiple-results

我有一个SQL查询,它使用子查询来计算主查询中未使用的表的一些结果。我有一个问题,个别结果(2.1和2.2)的结果被放在两行,所以2.1结果在那里,2.2在一行上是0,反之亦然。我希望它像下面的预期结果一样。

    SELECT DISTINCT

    DATEPART(week, SS.timearrived) 
        AS [Week No],

    SS.timearrived
        AS [Date],

    RTRIM(SS.vname)
        AS [V Name],    

    SS.vyid
        AS [Vy ID],

    ((SELECT CASE WHEN
                 (select COUNT(*)
    FROM  IME 
    WHERE IME.topos = SS.visid  and XCHE.sname IN ('2.1','2.2')
                      AND movek != 'Y' AND movek != 'S'
    GROUP BY IME.topos) IS NULL
    THEN '0'
    else (select COUNT(*)
    FROM  IME 
    WHERE IME.topos = SS.visid  and XCHE.sname IN ('2.1','2.2')
                      AND movek != 'Y' AND movek != 'S'
    GROUP BY IME.topos)
    END )

     + (SELECT CASE WHEN
                 (select COUNT(*)
    FROM  IME 
    WHERE IME.fmpos = SS.visid  and XCHE.sname IN ('2.1','2.2')
    GROUP BY IME.fmpos) IS NULL
    THEN '0'
    else (select COUNT(*)
    FROM  IME 
    WHERE IME.fmpos = SS.visid and XCHE.sname IN ('2.1','2.2')
    GROUP BY IME.fmpos)
    END)
    )/(
    convert(float,datediff(mi, swork, ework))/60 )

        AS [GBP T2],

    ((SELECT CASE WHEN
                 (select COUNT(*)
    FROM  IME 
    WHERE IME.topos = SS.visid  and XCHE.sname IN ('2.1')
                      AND movek != 'Y' AND movek != 'S'
    GROUP BY IME.topos) IS NULL
    THEN '0'
    else (select COUNT(*)
    FROM  IME 
    WHERE IME.topos = SS.visid  and XCHE.sname IN ('2.1')
                      AND movek != 'Y' AND movek != 'S'
    GROUP BY IME.topos)
    END )

     + (SELECT CASE WHEN
                 (select COUNT(*)
    FROM  IME 
    WHERE IME.fmpos = SS.visid  and XCHE.sname IN ('2.1')
    GROUP BY IME.fmpos) IS NULL
    THEN '0'
    else (select COUNT(*)
    FROM  IME 
    WHERE IME.fmpos = SS.visid and XCHE.sname IN ('2.1')
    GROUP BY IME.fmpos)
    END)
    )/(
    convert(float,datediff(mi, swork, ework))/60 )

        AS [GCP 2.1],

    ((SELECT CASE WHEN
                 (select COUNT(*)
    FROM  IME 
    WHERE IME.topos = SS.visid  and XCHE.sname IN ('2.2')
                      AND movek != 'Y' AND movek != 'S'
    GROUP BY IME.topos) IS NULL
    THEN '0'
    else (select COUNT(*)
    FROM  IME 
    WHERE IME.topos = SS.visid  and XCHE.sname IN ('2.2')
                      AND movek != 'Y' AND movek != 'S'
    GROUP BY IME.topos)
    END )

     + (SELECT CASE WHEN
                 (select COUNT(*)
    FROM  IME 
    WHERE IME.fmpos = SS.visid  and XCHE.sname IN ('2.2')
    GROUP BY IME.fmpos) IS NULL
    THEN '0'
    else (select COUNT(*)
    FROM  IME 
    WHERE IME.fmpos = SS.visid and XCHE.sname IN ('2.2')
    GROUP BY IME.fmpos)
    END)
    )/(
    convert(float,datediff(mi, swork, ework))/60 )

        AS [GCP 2.2]

    FROM    SS
    LEFT OUTER JOIN VCME ON SS.visid = vgk 
    LEFT OUTER JOIN VVVD ON SS.visid = fs01
    LEFT OUTER JOIN VCS ON VVVD.vvdgk = VCS.vvdgk
    LEFT OUTER JOIN VCSD ON VCS.gk = VCSD.csgk
    INNER JOIN XCHE on VCS.cgk = XCHE.gk

当前结果

    Week No     Date                        V Name      Vy ID       GBP T2      GCP 2.1     GCP 2.2
    14          2013-03-31 00:01:00.000     FORD        30580       18.72       0           18.72
    14          2013-03-31 00:01:00.000     FORD        30580       18.72       18.72       0

预期结果

    Week No     Date                        V Name      Vy ID       GBP T2      GCP 2.1     GCP 2.2
    14          2013-03-31 00:01:00.000     FORD        30580       18.72       18.72       18.72

1 个答案:

答案 0 :(得分:1)

在您的最终查询中,使用此作为最外层查询来获得您想要的结果。

SELECT    `Week No` ,      Date  ,   `V Name`, `Vy ID`,  `GBP T2`,
MAX(`GCP 2.1`), MAX(`GCP 2.2`) 
FROM
( -------------------  REST OF THE INNER QUERY HERE  --------------)
GROUP BY  `Week No` ,      Date  ,   `V Name`, `Vy ID`,  `GBP T2`;