⑴ sql 如何实现转置
你的表我没太懂 但是用改链case when 语句应该可以
SELECT  a,b,d AS c,(CASE WHEN c LIKE '核做孙人事科%' THEN d ELSE  0 END)人事科,(CASE WHEN c LIKE '总务科%' THEN d ELSE  0 END)总务科胡此,....FROM 表
⑵ sql 行列转置
看看这个吧
SQLServer中(行列转换)行转列及列转行且加平均值及汇总值
⑶ 怎样用SQL实现从表A到表B的变换,从表C到表D的变换,如图。
第一个是行列转置,
select student,
sum(decode(subject,'语缓晌文', grade,null)) "语文"蠢哪罩,
sum(decode(subject,'数学', grade,null)) "数学",
sum(decode(subject,'英语', grade,null)) "英语"
from table
group by student;
 第二个当然你行带闹数多了不行~
select distinct tmp2.a,
                (select b
                   from (SELECT a,
                                b,
                                rank() over(PARTITION BY a ORDER BY b) c
                           FROM tmp_1010) tmp1
                  where tmp1.a = tmp2.a
                    and tmp1.c = 1) as score1,
                     (select b
                   from (SELECT a,
                                b,
                                rank() over(PARTITION BY a ORDER BY b) c
                           FROM tmp_1010) tmp1
                  where tmp1.a = tmp2.a
                    and tmp1.c = 2) as score2,
                     (select b
                   from (SELECT a,
                                b,
                                rank() over(PARTITION BY a ORDER BY b) c
                           FROM tmp_1010) tmp1
                  where tmp1.a = tmp2.a
                    and tmp1.c = 3) as score3
  from tmp_1010 tmp2
⑷ 将表格中的两列数据进行转置,SQL 语句该怎么写
select人员编号,年份,月份,
max(case项目when'水电费'then金额else0end)水电费,
max(case项目磨帆when'服务费'then金额else0end)服务费,
max(case项目when'伙食费'没腔then金额else0end)伙食费
fromtable_namegroupby人员编号,年份,月份
具体情况具体分析,仅供参考。枯游衫
⑸ SQL行列转置
declare @sql varchar(8000)
declare @date varchar(20)
declare @bmbh varchar(20)
declare @ckbh varchar(20)
set @date='20110101'
set @bmbh='500103'
set @ckbh='0601'set @sql = 'select max(lsbmzd_bmmc) as bmmc,kcrkd2_wlbh as wlbh,max(lswlzd_wlmc) as wlmc,max(lswlzd_ggxh) as ggxh  ,sum(kcrkd2_sssl) as sum'
select @sql = @sql + ', max(case kcrkd1_kcywrq when ''' + kcrkd1_kcywrq + ''' then kcrkd2_sssl else 0 end) [' + kcrkd1_kcywrq + ']'
from (select distinct kcrkd1_kcywrq from  kcrkd1 
where kcrkd1_kcywrq>= substring(convert(varchar(100),dateadd(mm,-1,Convert(DateTime,@date)),112),1,6)+'26'
and kcrkd1_kcywrq<=substring(@date,1,6)+'25') as a
set @sql = @sql + ' from kcrkd1,kcrkd2,lswlzd,lsbmzd
where kcrkd1_lsbh=kcrkd2_lsbh and kcrkd1_pjlx=''j'' and kcrkd1_ckbh='''+@ckbh+''' and kcrkd1_bmbh=lsbmzd_bmbh and kcrkd2_wlbh=lswlzd_wlbh and kcrkd1_bmbh='''+@bmbh+''' 
group by kcrkd2_wlbh'
exec(@sql)
⑹ 如何用SQL把下面两个表完全倒过来,行变列,列变行完全转置!
静态脚本:
select '收入' as 项目
, case when 项目='一厂本月'  then 收入 else null end as 一厂本月
, case when 项目='一厂本年'  then 收入 else null end as 一厂本年
, case when 项目='二厂本月'  then 收入 else null end as 二厂本月
, case when 项目='二厂本年'  then 收入 else null end as 二厂本年
, case when 项目='三厂本月'  then 收入 else null end as 三厂本月
, case when 项目='三厂本年'  then 收入 else null end as 三厂本年
from 表名
union all
select '成本' as 项目
, case when 项目='一厂本月'  then 成本 else null end as 一厂本月
, case when 项目='一厂本年'  then 成本 else null end as 一厂本年
, case when 项目='二厂本月'  then 成本 else null end as 二厂本月
, case when 项目='二厂本年'  then 成本 else null end as 二厂本年
, case when 项目='三厂本月'  then 成本 else null end as 三厂本月
, case when 项目='三厂本年'  then 成本 else null end as 三厂本年
from 表名
union all
select '其他费用' as 项目
, case when 项目='一厂本月'  then 其他费用 else null end as 一厂本月
, case when 项目='一厂本年'  then 其他费用 else null end as 一厂本年
, case when 项目='二厂本月'  then 其他费用 else null end as 二厂本月
, case when 项目='二厂本年'  then 其他费用 else null end as 二厂本年
, case when 项目='三厂本月'  then 其他费用 else null end as 三厂本月
, case when 项目='三厂本年'  then 其他费用 else null end as 三厂本年
from 表名
union all
select '毛利' as 项目
, case when 项目='一厂本月'  then 毛利 else null end as 一厂本月
, case when 项目='一厂本年'  then 毛利 else null end as 一厂本年
, case when 项目='二厂本月'  then 毛利 else null end as 二厂本月
, case when 项目='二厂本年'  then 毛利 else null end as 二厂本年
, case when 项目='三厂本月'  then 毛利 else null end as 三厂本月
, case when 项目='三厂本年'  then 毛利 else null end as 三厂本年
from 表名
改动态脚本(只改项目,即改原表行不定,列数目固定):
declare @sql nvarchar(max)
set @sql=''
set @sql=@sql+'
select ''收入'' as 项目
'
select @sql=@sql+', case when 项目='''+项目+'''  then 收入 else null end as '+项目
from 表名
set @sql=@sql+'
from 表名
'
set @sql=@sql+'union all
select ''成本'' as 项目
'
select @sql=@sql+', case when 项目='''+项目+'''  then 成本 else null end as '+项目
from 表名
set @sql=@sql+'
from 表名
'
set @sql=@sql+'union all
select ''其他费用'' as 项目
'
select @sql=@sql+', case when 项目='''+项目+'''  then 其他费用 else null end as '+项目
from 表名
set @sql=@sql+'
from 表名
'
set @sql=@sql+'union all
select ''毛利'' as 项目
'
select @sql=@sql+', case when 项目='''+项目+'''  then 毛利 else null end as '+项目
from 表名
set @sql=@sql+'
from 表名
'
exec sp_executesql @sql
改动态脚本(改原表行和列数目不固定,两层动态脚本,能实现但基本难读):
declare @sql nvarchar(max)
set @sql='declare @sql_in nvarchar(max)
  set @sql_in='' '' '
set @sql=@sql+'select @sql_in=@sql_in+''union all
select ''''''+name+'''''' as 项目
'
select @sql=@sql+', case when 项目='''''+项目+'''''  then ''+name+'' else null end as '+项目
from 表名
set @sql=@sql+'
from 表名
''
from syscolumns where id=(select id from sysobjects where name=''表名'')
order by colorder
set @sql_in=stuff(@sql_in,1,10,'''')
exec sp_executesql @sql_in
'
exec sp_executesql @sql
⑺ 哪位大侠会用SQL写行列转置的语句,谢谢了
总结一下关于行列转置的实现方法 
1、固定列数的行列转换 
如 
student subject grade 
--------- ---------- -------- 
student1 语文 80 
student1 数学 70 
student1 英语 60 
student2 语文 90 
student2 数学 80 
student2 英语 100 
…… 
转换为 
语文 数学 英语 
student1 80 70 60 
student2 90 80 100 
…… 
语句如下:select student, 
sum(decode(subject,'语文', grade,null)) "语文", 
sum(decode(subject,'数学', grade,null)) "数学", 
sum(decode(subject,'英语', grade,null)) "英语" 
from table 
group by student; 
2、不定列行列转换 
如 
c1 c2 
--- ----------- 
1 我 
1 是 
1 谁 
2 知 
2 道 
3 不 
…… 
转换为 
1 我是谁 
2 知道 
3 不 
这一类型的转换可以借助于PL/SQL来完成,这里给一个例子 
CREATE OR REPLACE FUNCTION get_c2(tmp_c1 NUMBER) 
RETURN VARCHAR2 
IS 
Col_c2 VARCHAR2(4000); 
BEGIN 
FOR cur IN (SELECT c2 FROM t WHERE c1=tmp_c1) LOOP 
Col_c2 := Col_c2||cur.c2; 
END LOOP; 
Col_c2 := rtrim(Col_c2,1); 
RETURN Col_c2; 
END; 
select distinct c1 ,get_c2(c1) cc2 from table; 
或者不用pl/sql,利用分析函数和 CONNECT_BY 实现: 
SELECT c1, SUBSTR (MAX (SYS_CONNECT_BY_PATH (c2, ';')), 2) NAME 
FROM (SELECT c1, c2, rn, LEAD (rn) OVER (PARTITION BY c1 ORDER BY rn) rn1 
FROM (SELECT c1, c2, ROW_NUMBER () OVER (ORDER BY c2) rn 
FROM t)) 
START WITH rn1 IS NULL 
CONNECT BY rn1 = PRIOR rn 
GROUP BY c1; 
3、列数不固定(交叉表行列转置) 
这种是比较麻烦的一种,需要借助pl/sql: 
原始数据: 
CLASS1     CALLDATE         CALLCOUNT 
1 2005-08-08 40 
1 2005-08-07 6 
2 2005-08-08 77 
3 2005-08-09 33 
3 2005-08-08 9 
3 2005-08-07 21 
转置后: 
CALLDATE     CallCount1 CallCount2 CallCount3 
------------ ---------- ---------- ---------- 
2005-08-09 0 0 33 
2005-08-08 40 77 9 
2005-08-07 6 0 21 
试验如下: 
1). 建立测试表和数据 
CREATE TABLE t( 
    class1 VARCHAR2(2 BYTE), 
    calldate DATE, 
    callcount INTEGER 
); 
INSERT INTO t(class1, calldate, callcount) 
VALUES ('1', TO_DATE ('08/08/2005', 'MM/DD/YYYY'), 40); 
INSERT INTO t(class1, calldate, callcount) 
VALUES ('1', TO_DATE ('08/07/2005', 'MM/DD/YYYY'), 6); 
INSERT INTO t(class1, calldate, callcount) 
VALUES ('2', TO_DATE ('08/08/2005', 'MM/DD/YYYY'), 77); 
INSERT INTO t(class1, calldate, callcount) 
VALUES ('3', TO_DATE ('08/09/2005', 'MM/DD/YYYY'), 33); 
INSERT INTO t(class1, calldate, callcount) 
VALUES ('3', TO_DATE ('08/08/2005', 'MM/DD/YYYY'), 9); 
INSERT INTO t(class1, calldate, callcount) 
VALUES ('3', TO_DATE ('08/07/2005', 'MM/DD/YYYY'), 21); 
COMMIT ; 
2). 建立ref cursor准备输出结果集 
CREATE OR REPLACE PACKAGE pkg_getrecord 
IS 
    TYPE myrctype IS REF CURSOR; 
END pkg_getrecord; 
/ 
3). 建立动态sql交叉表函数,输出结果集 
CREATE OR REPLACE FUNCTION fn_rs 
RETURN pkg_getrecord.myrctype 
IS 
    s VARCHAR2 (4000); 
CURSOR c1 IS 
SELECT ',sum(case when Class1=' 
|| class1 
|| ' then CallCount else 0 end)' 
|| ' "CallCount' 
|| class1 
|| '"' c2 
FROM t 
GROUP BY class1; 
    r1 c1%ROWTYPE; 
    list_cursor pkg_getrecord.myrctype; 
BEGIN 
    s := 'select CallDate '; 
OPEN c1; 
    LOOP 
FETCH c1 INTO r1; 
EXIT WHEN c1%NOTFOUND; 
        s := s || r1.c2; 
END LOOP; 
CLOSE c1; 
    s := s || ' from T group by CallDate order by CallDate desc '; 
OPEN list_cursor FOR s; 
RETURN list_cursor; 
END fn_rs; 
/ 
4). 测试在sql plus下执行: 
var results refcursor; 
exec :results := fn_rs; 
print results; 
CALLDATE        CallCount1 CallCount2 CallCount3 
--------------- ---------- ---------- ---------- 
2005-08-09 0 0 33 
2005-08-08 40 77 9 
2005-08-07 6 0 21
⑻ 请教一个关于SQL行列转置的问题。
declare @maxcount int;
declare @i int;
declare @sql nvarchar(max);
select @maxcount=max(C) from (select 采购商,count(*) C from table1 group by 采购商) a
print @maxcount
set @i=1;
set @sql='select distinct 采购商,(select max(商品) from table1 b where b.采购商=a.采购商) 商品1' 
while @i<@maxcount
begin
set @sql=@sql+', (select max(商品) from table1 b where b.采购商=a.采购商 and 商品 not in (select top '+ cast(@i as nvarchar(4))+' 商品 from table1 c where b.采购商=c.采购商 order by 商品 desc)) 商品' + cast(@i+1 as nvarchar(4))
set @i=@i+1
end
set @sql=@sql+' from table1 a'
exec(@sql)
⑼ sql行列转置语句
declare @sql varchar(2000)
set @sql=''
select @sql=@sql+',max(case B when '+cast(B as varchar(10))+' then c end) ['+cast(B as varchar(10))+']' 
from (Select distinct B from TB) x
set @sql='select A'+@sql+' from (select A,B,count(distinct C) c from tb
group by A,B) v
group by a'
exec(@sql)
⑽ SQL查询表如何将表转置
ORACLE 11gR2
SQL> select * from t;
WEEK                               INCOME      STORE
------------------------------ ---------- ----------
mon                                  1000          2
tue                                  2000          4
wed                                  3000          3
thu                                  4000          6
fri                                  5000          9
sat                                  6000          1
sun                                  7000          2
7 rows selected.
SQL> select 'income' title,mon,tue,wed,thu,fri,sat,sun from (select week,income from t)
  2   pivot(
  3  sum(income) for week in (
  4  'mon' as mon,
  5  'tue' as tue,
  6  'wed' as wed,
  7  'thu' as thu,
  8  'fri' as fri,
  9  'sat' as sat,
 10  'sun' as sun))
 11  union all
 12  select 'store' title,mon,tue,wed,thu,fri,sat,sun from (select week,store from t)
 13   pivot(max(store) for week in (
 14  'mon' as mon,
 15  'tue' as tue,
 16  'wed' as wed,
 17  'thu' as thu,
 18  'fri' as fri,
 19  'sat' as sat,
 20  'sun' as sun));
TITLE                     MON        TUE        WED        THU        FRI        SAT        SUN
------------------ ---------- ---------- ---------- ---------- ---------- ---------- ----------
income                   1000       2000       3000       4000       5000       6000       7000
store                       2          4          3          6          9          1          2
