/*create table booklibary([圖書館] varchar(10), [科目] varchar(10), [數量] int)
insert into booklibary
select 'a', '語文', 5
union all select 'b', '數學', 6
union all select 'c', '英語', 3
union all select 'b', '語文', 4
union all select 'a', '數學', 3
*/declare @exec varchar(8000)
set @exec='select [圖書館]'
select @exec=@exec+',max(case when [科目] ='''+ [科目] +''' then [數量] else '''' end) as '''+[科目]+'''' from booklibary group by [科目]
set @exec=@exec+'from booklibary group by [圖書館]'
print @exec
exec(@exec)
select * from booklibary
-------------------------------
drop procedure model_his_mid;
gocreate procedure model_his_mid(
/**//*
* @vcolumns 分組的列 (最後乙個字段後面一定不要加,)
* @vclause 查詢表的條件(沒有條件='')
* @vorderby 查詢結果排序的字段 ''為預設排序
* @vtop 取出的行數 ''為全部
*/@vcolumns nvarchar(1000),
@vclause nvarchar(4000),
@vorderby nvarchar(100),
@vtop nvarchar(50))as
declare @sql nvarchar(4000)
declare @xcolumns nvarchar(4000)
declare @vtable nvarchar(50)
declare @vcolumnname nvarchar(100)
declare @vsumcolname nvarchar(100)
declare @vnum nvarchar(50)
declare @vallnum nvarchar(50)
declare @vsumorcount nvarchar(50)
set @xcolumns = ''
/**//*
* 需要賦值的引數
** @vtable 真正查詢的表名
* @vcolumnname 縱向轉橫向的列名
* @vsumcolname 取合計值的列名
* @vnum 數值的別名
* @vallnum 每行合計的列名 (如果不要合計則='')
* @vsumorcount sum或者count
* */
set @vtable = 'his_mid_tab'
set @vcolumnname = 'hsks'
set @vsumcolname = 'qty'
set @vnum = 'num'
set @vallnum = 'allnum'
set @vsumorcount = 'sum'
select
@xcolumns = @xcolumns
+ 'sum(case ' + @vcolumnname + ' when '+ char(39) + [a].[column] + char(39)+ ' then ' + @vnum + ' else 0 end) as '''
+ [a].[column] + ''', '
/**//*
* 需要比例加入此語句
* + '(' + 'cast(round(sum(case ' + @vcolumnname + ' when '+ char(39) + [a].[column] + char(39)+ ' then ' + @vnum + ' else 0 end)*100.00' + '/sum(' + @vnum + '),2) as decimal(10,2))) as ' + [a].[column] + '_i, '
*//**//**
* 要動態顯示列的sql語句 住院 outp_or_inp = '0' 門診outp_or_inp = '1'
*/from (select distinct dept_name as [column] from his_mzkm_tab where outp_or_inp = '0') as a
set @xcolumns = substring(@xcolumns, 0, len(@xcolumns))
set @sql = 'select '
if(@vtop <> '')
set @sql = @sql + 'top ' + @vtop + ' '
set @sql = @sql + @vcolumns + ', ' + @xcolumns
if(@vallnum <> '')
set @sql = @sql + ', sum(' + @vnum + ') as ' + @vallnum
set @sql = @sql + ' from ('
+ 'select ' + @vcolumns + ', ' + @vcolumnname + ', ' + @vsumorcount + '(' + @vsumcolname + ') as ' + @vnum + ' from ' + @vtable
if(@vclause <> '')
set @sql = @sql + ' where ' + @vclause
set @sql = @sql + ' group by ' + @vcolumns + ', ' + @vcolumnname
set @sql = @sql + ') as casual_table group by ' + @vcolumns + ' '
if(@vorderby <> '')
set @sql = @sql + 'order by ' + @vorderby
print @sql
exec sp_executesql @sql with recompile
/**//*
* sql語句 引號之內的為sql中的幾個引數分別為(分組的列,條件,排序,取出行數)
* exec model_his_mid 'kjkm','date = ''8''','kjkm desc',''
*//**//*
* sql語句
* exec model_his_mid 'kjkm','','kjkm desc','15'
*/
oracle 將查詢結果縱向橫向顯示。
使用max decode 函式可以將將查詢結果縱向橫向顯示 例如 表 dict 編號 lxbh 型別名稱 lxmc 101 變壓器 102 斷路器 103 電磁是電壓互感器 104 電容式電壓互感器顯示變壓器 斷路器 電磁是電壓互感器 電容式電壓互感器101 102 103 104select ma...
Sql分組查詢,如何把縱向顯示的結果變成橫向顯示
建庫並錄入資料 create table booklibary 圖書館 varchar 10 科目 varchar 10 數量 int insert into booklibary select a 語文 5 union all select b 數學 6 union all select c 英語...
縱向查詢顯示小示例
初始化環境 create table grade code intnot null name varchar 10 scores int 測試資料 insert into grade select 1,a 100union select 1,b 90union select 1,c 85union ...