SQL實現行轉列

2021-07-11 22:13:23 字數 1775 閱讀 8026

需求:用sql實現行轉列。如下圖所示:

行顯示的資料轉換成列顯示

實現行轉列的sql指令碼如下:

select

date_format(

last_day(

date_format(now(), '%y-%m-%d')

),'%y%m%d'

) as 業務日期,

max(case index_code when 'ind20101001' then prsn_total end) as 使用者註冊量,

max(case index_code when 'ind20204001' then prsn_total end) as 完成三項認證人數,

max(case index_code when 'ind20501001' then prsn_total end) as 投資人數,

max(case index_code when 'ind20501001' then count_time end) as 投資筆數,

max(case index_code when 'ind20504001' then prsn_total end) as 新客投資人數,

max(case index_code when 'ind21101001' then count_time end) as 滿標個數,

max(case index_code when 'ind20501001' then amount end) as 投資金額(元),

max(case index_code when 'ind20504001' then amount end) as 新客投資金額(元),

max(case index_code when 'ind20502001' then amount end) as 回款續投金額(元),

max(case index_code when 'ind20505001' then amount end) as 充值金額(元),

max(case index_code when 'ind20701002' then amount end) as 提現金額(元),

max(case index_code when 'ind21201001' then amount end) as 資金淨流量(元),

max(case index_code when 'ind21301001' then amount end) as 站崗資金金額(元),

max(case index_code when 'ind21101001' then amount end) as 成交額(元),

max(case index_code when 'ind20505001' then amount end) as 回款金額(元),

max(case index_code when 'ind20506001' then amount end) as 收益金額(元),

date_format(now(), '%y%m%d') as 資料時間

from dw_bds.tid01_trans_index_month

where reporting_code='tr04_oper_002'

and data_date = now()

group by data_date;

sql實現行轉列

行轉列不留空 select a as 訂單號 四川 as 位址 into result insert into result select a as 訂單號 成都 as 位址 union all select b as 訂單號 新疆 as 位址 union all select b as 訂單號 喀...

SQL語句實現行轉列查詢

表sales 查詢結果如下 1 建表 create table dbo sales id int identity 1,1 not null,year int null,jidu int null,jine int null,primary key clustered id asc with pad...

mysql 查詢行轉列 SQL語句實現行轉列查詢

表sales 查詢結果如下 1 建表 create table dbo sales id int identity 1,1 not null,year int null,jidu int null,jine int null,primary key clustered id asc with pad...