1.準備表和測試資料
create
table trandata (
id int
primary
key auto_increment,
flow_merc_id varchar(255),
tran_type varchar(255),
tran_amount int
);create
table trantype (
id int
primary
key auto_increment,
tran_type varchar(255)
);insert
into trantype (tran_type) values ('消費');
insert
into trantype (tran_type) values ('消費撤銷');
insert
into trantype (tran_type) values ('預授權完成');
insert
into trantype (tran_type) values ('預授權完成撤銷');
insert
into trantype (tran_type) values ('退貨');
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1000","消費",5);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1000","消費撤銷",1);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1000","消費",2);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1000","預授權完成",3);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1001","消費",4);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1001","消費",6);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1001","預授權完成撤銷",7);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1001","消費",9);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1002","消費",7);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1002","退貨",7);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1000","消費",2);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1003","消費",3);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1003","預授權完成",4);
insert
into trandata (flow_merc_id,tran_type,tran_amount) values ("1003","消費",5);
trantype表內容:
trandata表內容:
2.問題描述
問題一:
將相同flow_merc_id,相同tran_type的記錄合併,並算出交易筆數,交易金額的和。
select flow_merc_id as 商戶編號, tran_type as 交易型別, count(tran_type) as 交易筆數, sum(tran_amount) as 交易金額
問題二:
統計出每個商戶的各種交易型別的交易筆數,交易金額。
select
flow_merc_id as 商戶編號,
count(tran_type) as 交易筆數,
sum(tran_amount) as 交易金額,
sum(case tran_type when
'消費'
then
1else
0end ) 消費筆數,
sum(case tran_type when
'消費'
then tran_amount else
0end ) 消費,
sum(case tran_type when
'消費撤銷'
then
1else
0end ) 消費撤銷筆數,
sum(case tran_type when
'消費撤銷'
then tran_amount else
0end ) 消費撤銷,
sum(case tran_type when
'預授權完成'
then
1else
0end ) 預授權完成筆數,
sum(case tran_type when
'預授權完成'
then tran_amount else
0end ) 預授權完成,
sum(case tran_type when
'預授權完成撤銷'
then
1else
0end )預授權完成撤銷筆數,
sum(case tran_type when
'預授權完成撤銷'
then tran_amount else
0end )預授權完成撤銷,
sum(case tran_type when
'退貨'
then
1else
0end ) 退貨筆數,
sum(case tran_type when
'退貨'
then tran_amount else
0end ) 退貨
補充: case … when …then … else 也可以用if函式替換:
if(expr1,expr2,expr3) --> if(tran_type='消費',1,0)
sql行列轉換
問題 如果上述兩表互相換一下 即表結構和資料為 姓名 語文 數學 物理 張三 74 83 93 李四 74 84 94 想變成 得到如下結果 姓名 課程 分數 李四 語文 74 李四 數學 84 李四 物理 94 張三 語文 74 張三 數學 83 張三 物理 93 create table tb ...
SQL 行列轉換
資料列轉換成行。其中一列需要轉換成行,因為列的值不確定所以用動態執行sql sql物件拼寫出要轉換行的列的值。declare sql varchar 1000 select sql isnull sql advertise name from tbl advertise master select ...
sql行列轉換
1 靜態的行轉列 sql select start dt,max case type name when 總的參與人數 then value end as 總的參與人數 max case type name when 會員參與人數 then value end as 會員參與人數 max case ...