nd) as 标准分,
min(case when 科目='数学' then 科目 else null end) as [数学],
min(case when 科目='数学' then 得分 else null end) as 得分,
min(case when 科目='数学' then 班排名 else null end) as 班排名,
min(case when 科目='数学' then 校排名 else null end) as 校排名,
min(case when 科目='数学' then 标准分 else null end) as 标准分
from tab
group by 班级,学号,姓名
2、sql语句,某一行数据变为列名。
我有一个表
日期 合肥 F3 F4 芜湖 F6 F7
--------------------------------------------------------------------
NULL 气象 最高 最低 气象 最高 最低
2013-10 多云 32 20 雨 35 20
2013-11 晴 30 20 晴 25 20
现在要变为:
日期 城市 气象 最高 最低
--------------------------------------------------------------
2013-10 合肥 多云 32 20
2013-10 芜湖 雨 35 20
2013-11 合肥 晴 30 20
2013-11 芜湖 晴 25 20
可以的话尽量写成动态,因为不只两个城市
如果我的第一个表是这样呢:
日期 合肥 F3 F4 芜湖 F6 F7 北京 F9 F10 福州 F12 F13
------------------------------------------------------------------------------------------------
NULL 气象 最高 最低 气象 最高 最低 气象 最高 最低 气象 最高 最低
2013-10 多云 32 20 雨 35 20 晴 32 20 多云 25 20
2013-11 晴 30 20 晴 25 20 雨 31 21 晴 25 21
你能否写一个循环语句,首先城市的数量是(最后一个列名的阿拉伯数字-1)/3,然后再作判断写出像你第一个贴得出的结果集。
我的解法:
[sql]
if object_id('tb') is not null
drop table tb
go
create table tb
(
日期 varchar(15),
合肥 varchar(10),
F3 varchar(10),
F4 varchar(10),
芜湖 varchar(10),
F6 varchar(10),
F7 varchar(10),
北京 varchar(10),
F9 varchar(10),
F10 varchar(10),
福州 varchar(10),
F12 varchar(10),
F13 varchar(10)
)
insert into tb
select NULL,'气象', '最高','最低','气象','最高','最低','气象','最高','最低' ,'气象','最高','最低' union all
select '2013-10','多云', '32' ,'20', '雨','35', '20', '晴','32','20' , '多云','25','20' union all
select '2013-11','晴', '30','20','晴','25', '20', '雨','31','21','晴','25' , '21'
go
declare @tb_name nvarchar(100);
declare @t table(tb_name nvarchar(100),column_name nvarchar(100),column_id int)
declare @i int
declare @count int
declare @sql nvarchar(max);
--这里的表为 tb,需要换成你自己的表名
set @tb_name = 'tb'
insert into @t
select t.name ,
c.name,
c.column_id
from sys.tables t
inner join sys.columns c
on t.object_id = c.object_id
where t.name = @tb_name
set @i = 2
set @count = (select count(*) from @t)
set @sql ='';
while @i < @count
begin
set @sql = @sql + 'union all select 日期,''' +
(select column_name from @t where column_id = @i) + ''' as 城市,' +
(select column_name from @t where column_id = @i) + ' as 气象,' +
(select column_name from @t where column_id = @i+1) + ' as 最高,'+
(select column_name from @t where column_id = @i+1+1) + ' as 最低 ' +
' from ' + (select distinct tb_name from @t) + ' where 日期 is not null '
set @i = @i + 3
end
select @sql = stuff(@sql,1,len('union all'),'')
--select @sql
exec(@sql)
/*
日期 城市 气象 最高 最低
--------------- ---- ---------- ---------- ----------
2013-10 合肥 多云 32 20
2013-11 合肥 晴 30 20
2013-10 芜湖 雨 35 20
2013-11 芜湖 晴 25 20
2013-10 北京 晴 32 20
2013-11 北京 雨 31 21
2013-10 福州 多云 25 20
2013-11 福州 晴 25 21
*/
动态生成的sql语句:
[sql]
select 日期,'合肥' as 城市,合肥 as 气象,F3 as 最高,F4 as 最低
from tb where 日期 is not null
union all
select 日期,'芜湖' as 城市,芜湖 as 气象,F6 as 最高,F7 as 最低
from tb where 日期 is not null
union all
select 日期,'北京' as 城市,北京 as 气象,F9 as 最高,F10 as 最低
from tb where 日期 is not null
union all
select 日期,'福州' as 城市,福州 as 气象,F12 as 最高,F13 as 最低
from tb where 日期 is not null
3、在存储过程中的参数问题。
下面是问题:
[sql]
create table #tableTest(id int identity , name varchar(20),age int,)
go
insert into #tableTest
select '小明',23 union all
select '小红',28 union all
select '小军',27
go
select *from #tableTest
go
create proc procTest
@name varchar(20),
@age int,
@IDs varchar(30)
as
begin
select *from #tableTest where 1=1
end
--当我传入@name参数等于 小明,23岁,还有ID在(1,3)的时候
--我怎么可以弄成可选的参数
--比如,name不为空时候
select *from #tableTest where 1=1 and name like '小明'
--如果name参数为空的时候,IDs参数不为空的时候
select *from #tableTest where 1=1 and id in(1,3)
--请问一下,就有参数不为空的时候存储过程中的SQL追加条件,为空的时候就不追加,这样带可选参数的存储过程怎么写,以及怎么调用,请帮小弟写一个实例
这种问题,本质上就是根据传入的参数不同,进行不同的查询,也就是where 后面的查询条件是动态的。
一般有2中处理方式,一种就是写动态语句,但动态语句由于是动态拼接字符串,所以比较难维护,而且如果存储过程需要执行多次,那么每次都需要重新编译,但每次生成的执行计划,应该是比较优化的。但如果拼接字符串部分,只是少量的话,还是可以用动态语句的,下面我的解法就是用动态语句来实现的,结构清晰,易于维护。
另一种,就是通过在where语句后面写case when来进行判断,这种方法的好处是不用动态拼接语句,但不