簡體   English   中英

Oracle SQL或PLSQL將行轉換為列

[英]Oracle sql or plsql converting rows to column

我在下面的結構和示例中有一個table1。 需要將其顯示為表2。任何人都可以提供幫助。

表格1

id  Month   Days
1   january 20
2   january 19
3   january 20
4   january 21
5   january 22
6   january 23
1   February    18
2   February    17
3   February    16
4   February    15
5   February    16
6   February    18
6   February    18

表2:

id  January February
1   20  18
2   19  17
3   20  16
4   21  15
5   22  16
6   23  18
select id
     , sum(case when month = 'January' then days end) as january
     , sum(case when month = 'February' then days end) as february
  from table1
 group by id;

case語句為您的查詢提供了最佳解決方案:

select id,
      sum(
       case when 
               month = 'January' then  days 
                                 else '' end) as january, 
      sum(
        case when 
               month = 'February' then days  
                                 else ''  end) as February    
  from table1
 group by id;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM