【问题标题】:Show SQL case statement as different columns将 SQL 案例语句显示为不同的列
【发布时间】:2012-09-09 20:11:45
【问题描述】:

是否可以将 case 语句中的每个 WHEN 输出到其自己的列中?我试过了,但没有运气。下面是我最接近我想要的。

我目前有一个查询如下:

select BH.BATCH                 as batch_number, 
       BS.TBS_DESCRIPTION       as batch_description,
       BH.NUM_VOUCHERS          as "NUM_VOUCHERS(BATCH)",
       case when VS.TVS_CODE = 1 then count(*) else 0 end as in_stock,
       case when VS.TVS_CODE = 3 then count(*) else 0 end as terminate,
       case when VS.TVS_CODE = 5 then count(*) else 0 end as in_progress,
       case when VS.TVS_CODE = 6 then count(*) else 0 end as used,
       case when VS.TVS_CODE = 8 then count(*) else 0 end as deactivate
from BATCH_HIST   bh,
     BATCH_STATUS bs,
     VOUCH_HIST   vh,
     VOUCH_STATUS vs
where substr(BH.ALLOCAT_DATE, 1, 6) = 201207
and   to_char(BH.STATUS) = BS.TBS_CODE
and   BH.BATCH = VH.BATCH
and   to_char(VH.STATUS) = VS.TVS_CODE
group by BH.BATCH,
         BS.TBS_DESCRIPTION,
         BH.NUM_VOUCHERS,
         VS.TVS_CODE
order by 1,2,3;

这给了我以下输出,每个 STATUS 作为不同的记录:

BATCH_NUMBER  | BATCH_DESCRIPTION  | NUM_VOUCHERS(BATCH)  | IN_STOCK  | TERMINATE  | IN_PROGRESS  | USED  | DEACTIVATE
------------------------------------------------------------------------------------------------------------------------------
4413565       |  Allocate          |  100                 |  67       |  0         |  0           |  0    |  0
4413565       |  Allocate          |  100                 |  0        |  0         |  0           |  33   |  0
4413566       |  Allocate          |  100                 |  63       |  0         |  0           |  0    |  0
4413566       |  Allocate          |  100                 |  0        |  0         |  0           |  37   |  0

我希望将其组合在一起,以便每个 BATCH_NUMBER 有一条记录:

BATCH_NUMBER  | BATCH_DESCRIPTION  | NUM_VOUCHERS(BATCH)  | IN_STOCK  | TERMINATE  | IN_PROGRESS  | USED  | DEACTIVATE
------------------------------------------------------------------------------------------------------------------------------
4413565       |  Allocate          |  100                 |  67       |  0         |  0           |  33   |  0
4413566       |  Allocate          |  100                 |  63       |  0         |  0           |  37   |  0

【问题讨论】:

    标签: sql oracle case


    【解决方案1】:

    表达式,如您所写,需要 group by,但如果您对匹配项求和,则可以从 group by 中消除 tvs_code:

    select BH.BATCH                 as batch_number, 
           BS.TBS_DESCRIPTION       as batch_description,
           BH.NUM_VOUCHERS          as "NUM_VOUCHERS(BATCH)",
           sum (case when VS.TVS_CODE = 1 then 1 else 0 end) as in_stock,
           sum (case when VS.TVS_CODE = 3 then 1 else 0 end) as terminate,
           sum (case when VS.TVS_CODE = 5 then 1 else 0 end) as in_progress,
           sum (case when VS.TVS_CODE = 6 then 1 else 0 end) as used,
           sum (case when VS.TVS_CODE = 8 then 1 else 0 end) as deactivate
    from BATCH_HIST   bh,
         BATCH_STATUS bs,
         VOUCH_HIST   vh,
         VOUCH_STATUS vs
    where substr(BH.ALLOCAT_DATE, 1, 6) = 201207
    and   to_char(BH.STATUS) = BS.TBS_CODE
    and   BH.BATCH = VH.BATCH
    and   to_char(VH.STATUS) = VS.TVS_CODE
    group by BH.BATCH,
             BS.TBS_DESCRIPTION,
             BH.NUM_VOUCHERS
    order by 1,2,3;
    

    【讨论】:

      【解决方案2】:

      这包含了您当前的查询。也许这可以帮助你。

      SELECT  subQuery.BATCH_NUMBER,
              subQuery.BATCH_DESCRIPTION,
              subquery."NUM_VOUCHERS(BATCH)",
              MAX(subquery.in_stock) AS in_stock,
              MAX(subquery.terminate) AS terminate,
              MAX(subquery.in_progress) AS in_progress,
              MAX(subquery.used) AS used,
              MAX(subquery.deactivate) AS deactivate
      FROM
      (
          select BH.BATCH                 as batch_number, 
                 BS.TBS_DESCRIPTION       as batch_description,
                 BH.NUM_VOUCHERS          as "NUM_VOUCHERS(BATCH)",
                 case when VS.TVS_CODE = 1 then count(*) else 0 end as in_stock,
                 case when VS.TVS_CODE = 3 then count(*) else 0 end as terminate,
                 case when VS.TVS_CODE = 5 then count(*) else 0 end as in_progress,
                 case when VS.TVS_CODE = 6 then count(*) else 0 end as used,
                 case when VS.TVS_CODE = 8 then count(*) else 0 end as deactivate
          from BATCH_HIST   bh,
               BATCH_STATUS bs,
               VOUCH_HIST   vh,
               VOUCH_STATUS vs
          where substr(BH.ALLOCAT_DATE, 1, 6) = 201207
          and   to_char(BH.STATUS) = BS.TBS_CODE
          and   BH.BATCH = VH.BATCH
          and   to_char(VH.STATUS) = VS.TVS_CODE
          group by BH.BATCH,
                   BS.TBS_DESCRIPTION,
                   BH.NUM_VOUCHERS,
                   VS.TVS_CODE
      ) AS subQuery
      GROUP BY subQuery.BATCH_NUMBER,
              subQuery.BATCH_DESCRIPTION,
              subquery."NUM_VOUCHERS(BATCH)"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 2014-06-19
        • 1970-01-01
        • 1970-01-01
        • 2020-10-31
        相关资源
        最近更新 更多