sqlserver行转列/列转行
1.sqlserver行转列
行转列使用pivot。
2.sqlserver列转行
列转行使用列转行函数unpivot。
示例脚本如下所示
with temp as(
select t1.DingHaoPici,
count(*) AS aNum,
sum(t1.fxwanzheng) as fxwanzheng,
sum(case when efa.avgDianZu between 0.3 and 0.4 then t1.fxwanzheng else 0 end) as [0.3-0.4],
sum(case when efa.avgDianZu between 0.4 and 0.5 then t1.fxwanzheng else 0 end) as [0.4-0.5],
sum(case when efa.avgDianZu between 0.5 and 0.6 then t1.fxwanzheng else 0 end) as [0.5-0.6],
sum(case when efa.avgDianZu between 0.6 and 0.7 then t1.fxwanzheng else 0 end) as [0.6-0.7],
sum(case when efa.avgDianZu between 0.7 and 0.8 then t1.fxwanzheng else 0 end) as [0.7-0.8],
sum(case when efa.avgDianZu between 0.8 and 0.9 then t1.fxwanzheng else 0 end) as [0.8-0.9],
sum(case when efa.avgDianZu between 0.9 and 1.0 then t1.fxwanzheng else 0 end) as [0.9-1.0],
sum(case when efa.avgDianZu between 1.0 and 1.1 then t1.fxwanzheng else 0 end) as [1.0-1.1],
sum(case when efa.avgDianZu between 1.1 and 1.2 then t1.fxwanzheng else 0 end) as [1.1-1.2],
sum(case when efa.avgDianZu between 1.2 and 1.3 then t1.fxwanzheng else 0 end) as [1.2-1.3],
sum(case when efa.avgDianZu between 1.3 and 1.4 then t1.fxwanzheng else 0 end) as [1.3-1.4],
sum(case when efa.avgDianZu between 1.4 and 1.5 then t1.fxwanzheng else 0 end) as [1.4-1.5],
sum(case when efa.avgDianZu between 1.5 and 1.6 then t1.fxwanzheng else 0 end) as [1.5-1.6],
sum(case when efa.avgDianZu between 1.6 and 1.7 then t1.fxwanzheng else 0 end) as [1.6-1.7],
sum(case when efa.avgDianZu between 1.7 and 1.8 then t1.fxwanzheng else 0 end) as [1.7-1.8],
sum(case when efa.avgDianZu between 1.8 and 1.9 then t1.fxwanzheng else 0 end) as [1.8-1.9],
sum(case when efa.avgDianZu between 1.9 and 2.0 then t1.fxwanzheng else 0 end) as [1.9-2.0],
sum(case when efa.avgDianZu between 2.0 and 2.1 then t1.fxwanzheng else 0 end) as [2.0-2.1],
sum(case when efa.avgDianZu between 2.1 and 2.2 then t1.fxwanzheng else 0 end) as [2.1-2.2],
sum(case when efa.avgDianZu between 2.2 and 2.3 then t1.fxwanzheng else 0 end) as [2.2-2.3],
sum(case when efa.avgDianZu between 2.3 and 2.4 then t1.fxwanzheng else 0 end) as [2.3-2.4],
sum(case when efa.avgDianZu between 2.4 and 2.5 then t1.fxwanzheng else 0 end) as [2.4-2.5],
sum(case when efa.avgDianZu between 2.5 and 2.6 then t1.fxwanzheng else 0 end) as [2.5-2.6],
sum(case when efa.avgDianZu between 2.6 and 2.7 then t1.fxwanzheng else 0 end) as [2.6-2.7],
sum(case when efa.avgDianZu between 2.7 and 2.8 then t1.fxwanzheng else 0 end) as [2.7-2.8],
sum(case when efa.avgDianZu between 2.8 and 2.9 then t1.fxwanzheng else 0 end) as [2.8-2.9],
sum(case when efa.avgDianZu between 2.9 and 3.0 then t1.fxwanzheng else 0 end) as [2.9-3.0],
sum(case when efa.avgDianZu between 3.0 and 3.1 then t1.fxwanzheng else 0 end) as [3.0-3.1],
sum(case when efa.avgDianZu between 3.1 and 3.2 then t1.fxwanzheng else 0 end) as [3.1-3.2],
sum(case when efa.avgDianZu between 3.2 and 3.3 then t1.fxwanzheng else 0 end) as [3.2-3.3],
sum(case when efa.avgDianZu between 3.3 and 3.4 then t1.fxwanzheng else 0 end) as [3.3-3.4],
sum(case when efa.avgDianZu between 3.4 and 3.5 then t1.fxwanzheng else 0 end) as [3.4-3.5],
sum(case when efa.avgDianZu between 3.5 and 3.6 then t1.fxwanzheng else 0 end) as [3.5-3.6]
from
VF_TongJi_All1 t1
left join ETL_FX_AVG efa ON t1.daoCodeTime =efa.daoCodeTime AND t1.zuhao =efa .zuhao
where 1=1
-- and t1.xqOpeDate >= '2023-01-01'
and efa.daoCodeTime is not null
AND efa.avgDianZu IS NOT NULL
-- AND t1.DingHaoPici='22C23BTC'
AND t1.DingHaoPici='${pici}'
group by t1.DingHaoPici
)
select DingHaoPici,aNum,fxwanzheng,qujian,dNumm,CONVERT(decimal(18,2),dNumm*1.0/fxwanzheng) AS LV
from temp
unpivot (dNumm for qujian in([0.3-0.4],[0.4-0.5],[0.5-0.6],[0.6-0.7],[0.7-0.8],[0.8-0.9],[0.9-1.0],[1.0-1.1],
[1.1-1.2],[1.2-1.3],[1.3-1.4],[1.4-1.5],[1.5-1.6],[1.6-1.7],[1.7-1.8],[1.8-1.9],[1.9-2.0],[2.0-2.1],[2.2-2.3],
[2.3-2.4],[2.4-2.5],[2.6-2.7],[2.7-2.8],[2.8-2.9],[2.9-3.0],[3.0-3.1] ,[3.1-3.2],[3.2-3.3],[3.3-3.4],[3.4-3.5],[3.5-3.6]
))
s
where dNumm>0
order by DingHaoPici
;