SQL UNION语句

时间:2012-10-16 09:11:40

标签: sql sql-server union

我收到了以下问题:

SELECT
        ep_101.cd_hfdmapnr AS Dossiernummer,
        SUM(ep_102.id_fhmbdlv) AS Kosten

    FROM ep_101

    INNER JOIN et_101 ON ep_101.cd_hfdmapnr = et_101.cd_hfdmapnr
    INNER JOIN ep_102 ON ep_101.cd_hfdmapnr = ep_102.cd_hfdmapnr

    WHERE ep_101.cd_mapkverw = 0 
    AND ep_102.cd_bkcode = '000010' 
    AND et_101.opdrachtgever1 = '05354605'

    GROUP BY ep_101.cd_hfdmapnr

    UNION

    SELECT
        ep_101.cd_hfdmapnr AS Dossiernummer,
        SUM(ep_102.id_fhmbdlv) AS GriffieKosten

    FROM ep_101

    INNER JOIN et_101 ON ep_101.cd_hfdmapnr = et_101.cd_hfdmapnr
    INNER JOIN ep_102 ON ep_101.cd_hfdmapnr = ep_102.cd_hfdmapnr

    WHERE ep_101.cd_mapkverw = 0 
    AND ep_102.cd_bkcode = '000020' 
    AND et_101.opdrachtgever1 = '05354605'

    GROUP BY ep_101.cd_hfdmapnr

虽然结果只给回了Dossiernummer& Kosten如何重新创建它以便还显示“Griffiekosten”?因为我没有想法......

2 个答案:

答案 0 :(得分:1)

如果你真的需要在两栏中展示它:

SELECT
    ep_101.cd_hfdmapnr AS Dossiernummer,
    SUM(case when ep_102.cd_bkcode = '000010' then ep_102.id_fhmbdlv else 0 end) AS Kosten,
    SUM(case when ep_102.cd_bkcode = '000020' then ep_102.id_fhmbdlv else 0 end) AS GriffieKosten
FROM ep_101
    INNER JOIN et_101 ON ep_101.cd_hfdmapnr = et_101.cd_hfdmapnr
    INNER JOIN ep_102 ON ep_101.cd_hfdmapnr = ep_102.cd_hfdmapnr
WHERE ep_101.cd_mapkverw = 0 
    AND ep_102.cd_bkcode in ('000010', '000020')
    AND et_101.opdrachtgever1 = '05354605'
GROUP BY ep_101.cd_hfdmapnr

答案 1 :(得分:0)

他们被显示!
当你使用UNION时,列名是根据UNION中的名字来命名的,在这种情况下:Dossiernummer& Kosten。

只是一个小问题,你确定要使用UNION吗? Kosten和Griffiekosten是一样的吗?如果没有,那么UNION是错误的方式。

假设它们是不同类型的值,但你希望结果显示每个Dossiernummer,那么你正在寻找一个JOIN(在Dossiernummer上)。

有些事情:

SELECT Isnull(T1.DOSSIERNUMMER, T2.DOSSIERNUMMER) AS Dossiernummer, 
       T1.KOSTEN, 
       T2.GRIFFIEKOSTEN 
FROM   (SELECT EP_101.CD_HFDMAPNR     AS Dossiernummer, 
               Sum(EP_102.ID_FHMBDLV) AS Kosten 
        FROM   EP_101 
               INNER JOIN ET_101 
                       ON EP_101.CD_HFDMAPNR = ET_101.CD_HFDMAPNR 
               INNER JOIN EP_102 
                       ON EP_101.CD_HFDMAPNR = EP_102.CD_HFDMAPNR 
        WHERE  EP_101.CD_MAPKVERW = 0 
               AND EP_102.CD_BKCODE = '000010' 
               AND ET_101.OPDRACHTGEVER1 = '05354605' 
        GROUP  BY EP_101.CD_HFDMAPNR)T1 
       FULL OUTER JOIN (SELECT EP_101.CD_HFDMAPNR     AS Dossiernummer, 
                               Sum(EP_102.ID_FHMBDLV) AS GriffieKosten 
                        FROM   EP_101 
                               INNER JOIN ET_101 
                                       ON EP_101.CD_HFDMAPNR = 
                                          ET_101.CD_HFDMAPNR 
                               INNER JOIN EP_102 
                                       ON EP_101.CD_HFDMAPNR = 
                                          EP_102.CD_HFDMAPNR 
                        WHERE  EP_101.CD_MAPKVERW = 0 
                               AND EP_102.CD_BKCODE = '000020' 
                               AND ET_101.OPDRACHTGEVER1 = '05354605' 
                        GROUP  BY EP_101.CD_HFDMAPNR)T2 
                    ON T1.DOSSIERNUMMER = T2.DOSSIERNUMMER