如何更改查询输出

时间:2014-06-21 06:24:54

标签: plsqldeveloper

我有使用union输出down结果的down查询,我想将输出重新格式化为下一种格式。

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       

3 个答案:

答案 0 :(得分: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)。

答案 1 :(得分: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

答案 2 :(得分: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