Oracle的 簡單行轉列

來源:互聯網
上載者:User

應個報表要求做個

時間             雙色球,  勝負彩。。。。。

200905     銷量       銷量

200906     銷量       銷量

200907     銷量       銷量

200908     銷量       銷量

 

一個SQL 陳述式得到結果是:

f_yearmonth, lot_name,money

200905   雙色球    50

200905   勝負彩   100

200906   雙色球   60

200906   勝負彩    70

 

 

Select f_yearmonth,
Max(decode(f_lotname,'雙色球',f_money,0)) As "雙色球",
Max(decode(f_lotname,'福彩3D',f_money,0)) as "福彩3D",
Max(decode(f_lotname,'勝負彩',f_money,0)) as "勝負彩",
Max(decode(f_lotname,'時時彩',f_money,0)) as "時時彩",
Max(decode(f_lotname,'時時樂',f_money,0)) as "時時樂",
 From
 t_tmp_test_A
 Group By f_yearmonth
 Order By f_yearmonth

 

關鍵語句:

 Max(decode(f_lotname,'雙色球',f_money,0)) As "雙色球",

 Group By f_yearmonth

 

應為 按時間重組只有一條,並且只有一條資料:  200905   雙色球    50 

那麼 用MAX取一條資料 就是50

decode 意思說 如果是‘雙色球’ 取對應的f_money  並且放在 命名為 "雙色球"的 列上。

 

很顯然 這列是固定!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.