sql 行轉列、分組、排序,
阿新 • • 發佈:2019-02-05
select * from (
select
concat(t2.supplier,'(',t1.from_addr,')') 供應商(礦點),
t3.goods_name 貨品,
count(t1.id) 磅總單數,
sum(case when t1.status='1'then 1 else 0 end) 待處理,
sum(case when t1.status='2'then 1 else 0 end) 自動配對,
sum(case when t1.status='3'then 1 else 0 end) 人工配對,
sum(case when t1.status='7'then 1 else 0 end) 手工補單,
sum(case when t1.status not in ('1','2','3','7')then 1 else 0 end) 其他,
(sum(case when t1.status='2'then 1 else 0 end) +
sum(case when t1.status='3'then 1 else 0 end))
/count(t1.id) 接單率
from
poundbill.poundbill_${code} t1
inner join
poundbill.manu_supplier_mine t2
on
t1.supplier=t2.supplier
inner join
poundbill.goods_loss_def t3
on
t3.goods_category = t1.category
inner join
poundbill.manu_code_def t4
on
t3.pk_corp = t4.pk_corp
where
t1.sum_date >= '${beginDate}'
and
t1.sum_date <= '${endDate}'
group by
t4.name ,t2.supplier,t3.goods_name,t1.from_addr
) A
order by
A.接單率
select
concat(t2.supplier,'(',t1.from_addr,')') 供應商(礦點),
t3.goods_name 貨品,
count(t1.id) 磅總單數,
sum(case when t1.status='1'then 1 else 0 end) 待處理,
sum(case when t1.status='2'then 1 else 0 end) 自動配對,
sum(case when t1.status='3'then 1 else 0 end) 人工配對,
sum(case when t1.status='7'then 1 else 0 end) 手工補單,
sum(case when t1.status not in ('1','2','3','7')then 1 else 0 end) 其他,
(sum(case when t1.status='2'then 1 else 0 end) +
sum(case when t1.status='3'then 1 else 0 end))
/count(t1.id) 接單率
from
poundbill.poundbill_${code} t1
inner join
poundbill.manu_supplier_mine t2
on
t1.supplier=t2.supplier
inner join
poundbill.goods_loss_def t3
on
t3.goods_category = t1.category
inner join
poundbill.manu_code_def t4
on
t3.pk_corp = t4.pk_corp
where
t1.sum_date >= '${beginDate}'
and
t1.sum_date <= '${endDate}'
group by
t4.name ,t2.supplier,t3.goods_name,t1.from_addr
) A
order by
A.接單率