2014-06-21 66 views
0

我有使用聯合輸出向下結果的向下查詢,我想將輸出重新格式化爲下一個格式。如何更改查詢輸出

SELECT TO_CHAR(F.DATE, 'YYYY') YEAR, 
F.FIELD, 
    ROUND((ROUND(SUM(F.INJECTION_ VOLUME)/120, 0)/1000), 2) INJECTION 
    FROM INJECTION F 
    GROUP BY TO_CHAR(F.DATE, 'YYYY'), 
    F.F_CD 
    HAVING TO_CHAR(F.DATE, 'YYYY') = '2013' 
    UNION 
    SELECT TO_CHAR(S.DATE, 'YYYY') YEAR, 
    S.FIELD, 
    ROUND((ROUND(SUM(S.PROD_VOLUME)/120, 0)/1000), 2) PRODUCTION 
    FROM PRODUCTION P 
    GROUP BY TO_CHAR(S.DATE, 'YYYY'), 
    S.FIELD 
    HAVING TO_CHAR(S.DATE, 'YYYY') = '2013' 
    ORDER BY 1 

輸出

YEAR  FIELD  INJECTION 
2013  FARM  122.35 
2013  FARM  123.73 
2013  DARM   59.99 

所需的輸出

YEAR  FIELD  PRODUCTION  INJECTION 
2013  FARM  122.35   123.75 
2013  DARM  59.99  

回答

0

我只是用你的代碼(有一些修改)爲t1和t2,並加入了兩個表,使結果進來的行(而不是結合在一起):

select t1.YEAR, t1.FIELD, t2.PRODUCTION, t1.INJECTION 
from (
    SELECT TO_CHAR(F.DATE, 'YYYY') YEAR, F.FIELD, ROUND((ROUND(SUM(F.INJECTION_ VOLUME)/120, 0)/1000), 2) INJECTION 
     FROM INJECTION F 
     WHERE TO_CHAR(F.DATE, 'YYYY') = '2013' 
     GROUP BY TO_CHAR(F.DATE, 'YYYY'),F.F_CD 
    ) as t1 
    full outer join (
     SELECT TO_CHAR(S.DATE, 'YYYY') YEAR, S.FIELD, ROUND((ROUND(SUM(S.PROD_VOLUME)/120, 0)/1000), 2) PRODUCTION 
      FROM PRODUCTION S 
      WHERE TO_CHAR(S.DATE, 'YYYY') = '2013' 
      GROUP BY TO_CHAR(S.DATE, 'YYYY'),S.FIELD 
     ) as t2 on t1.YEAR = t2.YEAR and t1.FIELD = t2.FIELD 

請注意,對於最後一個值,我們將在結果集中包含「NULL」。要將null更改爲零(或任何其他),可以在MS SQL Server中使用isnull(field,0),或者在MySQL中使用ifnull(field,0)。

+0

是對齊的別名。謝謝。 – user3747520

+0

你是什麼意思? t1和t2只是名字。他們可以是任何東西。 – Alisa

+0

不是t的,是第二張桌子的S還是P ...... – user3747520

0

試試這個

SELECT T.Year,T.FIELD,T.INJECTION,S.PRODUCTION 
From 
(
    SELECT TO_CHAR(F.DATE, 'YYYY') YEAR, 
      F.FIELD, 
      ROUND((ROUND(SUM(F.INJECTION_ VOLUME)/120, 0)/1000), 2) INJECTION 
    FROM INJECTION F 
    GROUP BY TO_CHAR(F.DATE, 'YYYY'),F.F_CD 
    HAVING TO_CHAR(F.DATE, 'YYYY') = '2013' 
) T Join 
(
    SELECT TO_CHAR(S.DATE, 'YYYY') YEAR, 
      S.FIELD, 
      ROUND((ROUND(SUM(S.PROD_VOLUME)/120, 0)/1000), 2) PRODUCTION 
      FROM PRODUCTION P 
    GROUP BY TO_CHAR(S.DATE, 'YYYY'),S.FIELD 
    HAVING TO_CHAR(S.DATE, 'YYYY') = '2013' 
) T1 ON T.Year = T1.Year And T.FIELD = S.FIELD 
+0

是對齊的別名。謝謝。 – user3747520

0

還有就是列轉換爲行特定的技術。你可以使用它。 我可以概述以下原則。您可以重寫一次運行。 如果需要,可以將group by子句添加到最外層的select語句中。 以上答案工作,如果表只有兩個,並有具體數據。以下是一種通用技術。

實施例:

SELECT YEAR, FIELD, 
    CASE WHEN TBL = 'PRODUCTION' 
     THEN INJECTION 
     END PRODUCTION, 
    CASE WHEN TBL = 'INJECTION' 
     THEN INJECTION 
     END INJECTION 
    (
    SELECT TO_CHAR(F.DATE, 'YYYY') YEAR, 
    F.FIELD, 
    ROUND((ROUND(SUM(F.INJECTION_ VOLUME)/120, 0)/1000), 2) INJECTION, 
    'INJECTION' TBL 
    FROM INJECTION F 
    GROUP BY TO_CHAR(F.DATE, 'YYYY'), 
    F.F_CD 
    HAVING TO_CHAR(F.DATE, 'YYYY') = '2013' 
    UNION 
    SELECT TO_CHAR(S.DATE, 'YYYY') YEAR, 
    S.FIELD, 
    ROUND((ROUND(SUM(S.PROD_VOLUME)/120, 0)/1000), 2) , 
    'PRODUCTION' TBL 
    FROM PRODUCTION P. 
    GROUP BY TO_CHAR(S.DATE, 'YYYY'), 
    S.FIELD 
    HAVING TO_CHAR(S.DATE, 'YYYY') = '2013' 
    ORDER BY 1  
    ) 

我可以在查詢適當的修改。以下代碼給出了一條線索。通過編寫合適的邏輯,可以生成期望的結果。

SELECT YEAR, FIELD, 
MAX (
     CASE WHEN FIELD = 'FARM' 
      THEN INJECTION 
      END 
) PRODUCTION, 
MAX (  CASE WHEN FIELD <> 'FARM' 
      THEN INJECTION 
      END 
) INJECTION 
     (
     SELECT TO_CHAR(F.DATE, 'YYYY') YEAR, 
     F.FIELD, 
     ROUND((ROUND(SUM(F.INJECTION_ VOLUME)/120, 0)/1000), 2) INJECTION 
     FROM INJECTION F 
     GROUP BY TO_CHAR(F.DATE, 'YYYY'), 
     F.F_CD 
     HAVING TO_CHAR(F.DATE, 'YYYY') = '2013' 
     UNION 
     SELECT TO_CHAR(S.DATE, 'YYYY') YEAR, 
     S.FIELD, 
     ROUND((ROUND(SUM(S.PROD_VOLUME)/120, 0)/1000), 2) 
     FROM PRODUCTION P. 
     GROUP BY TO_CHAR(S.DATE, 'YYYY'), 
     S.FIELD 
     HAVING TO_CHAR(S.DATE, 'YYYY') = '2013' 
     ORDER BY 1  
     ) 
GROUP BY YEAR, FIELD 
+0

是對齊的別名。謝謝。 – user3747520