0

我收到以下查询:

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”?因为我没有想法......

4

2 回答 2

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
于 2012-10-16T09:13:25.973 回答
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 
于 2012-10-16T09:14:33.880 回答