❶ 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行列轉置
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 行列轉置
看看這個吧
SQLServer中(行列轉換)行轉列及列轉行且加平均值及匯總值
❹ 請教一個關於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寫行列轉置的語句,謝謝了
總結一下關於行列轉置的實現方法
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