select 日期,
[长沙]=max(case 城市 when '长沙' then 温度 else 0 end),
[湘潭]=max(case 城市 when '湘潭' then 温度 else 0 end),
[株洲]=max(case 城市 when '株洲' then 温度 else 0 end)
from 表
group by 日期
order by 日期
[长沙]=max(case 城市 when '长沙' then 温度 else 0 end),
[湘潭]=max(case 城市 when '湘潭' then 温度 else 0 end),
[株洲]=max(case 城市 when '株洲' then 温度 else 0 end)
from 表
group by 日期
order by 日期
解决方案 »
- sql 多表查询问题
- (简单)如何修改数据库所有者SID
- 请高手支招:将.xml配置文件Data Source改为本机IP时,数据库(SQL2005)链接失败
- 这个TSQL怎样写
- 怎么实现在每次操作数据库的时候,其记录的排列顺序都是不同的?
- 我在SQL SERVER7.0 enterprise manger 中没有办法打开任何一张表来看里面的内容
- 何处有LOTUS NOTES R5的相关资料下载?多谢!
- 两个表的多个字段为匹配条件的情况下如何进行删除数据?
- timestamp怎么使用?
- 怎么设置触发器先后顺序?菜鸟不知道在哪儿操作啊
- 不会用SQL2000真痛苦,大家帮帮我~~
- 使用sql语句如何知道一张表的表结构sql 语句该如何写
select @s=@s+',['+城市+']=max(case 城市 when '''+城市+''' then 温度 else 0 end)'
from 表
group by 城市
set @s=@s+' from 表 group by 日期'exec(@s)
这句:[长沙]=max(case 城市 when '长沙' then 温度 else 0 end),
是什么意思?为什么要用max?
set @s=''
select @s=@s+',['+城市+']=case 城市 when '''+城市+''' then 溫度 else NULL end)'
from 表 group by 城市
exec('select 日期+@s+' from 表 group by 日期)
改正:(我測試過)
declare @s varchar(8000)
set @s=''
select @s=@s+',['+城市+']=sum(case 城市 when '''+城市+''' then 溫度 else null end)'
from 表 group by 城市
exec('select 日期'+@s+' from 表 group by 日期')
@prodId int
as
select prodName,prodId,bomNum
from prod
where prodId = @prodId
-------------------------------------------------------------------------
日期 | AA | BB | CC |
-------------------------------------------------------------------------
2004-12-21 | 23 | 28 | 26 |
-------------------------------------------------------------------------
2004-12-22 | 27 | 25 | 22 |
-------------------------------------------------------------------------
该如何办?请大家一定帮我...解决马上结贴.
是一个字符串迭代,每次的@s都会增加一个种类的城市,
是一个字符串迭代,每次的@s都会增加一个种类的城市,
假设你的城市对应表名叫做城市对应表,里面有个字段叫城市 ,那就这样...
delcare @s varchar(8000)
set @s='select 日期 '
select @s=@s+',['+城市+']=max(case 城市 when '''+城市+''' then 温度 else 0 end)'
from (select distinct 城市 from 城市对应表) as a
set @s=@s+' from 表 group by 日期'exec(@s)
from (select distinct 城市 from 城市对应表) as a在城市对应表中取不到温度,所以不行
set @s='select 日期 '
select @s=@s+',['+城市+']=max(case 城市 when '''+城市+''' then 温度 else 0 end)'
from (select distinct 城市 from (select b.日期,a.城市,b.温度 from 城市对应表 a join 表 b on a.城市=b.城市) a) as a
set @s=@s+' from (select b.日期,a.城市,b.温度 from 城市对应表 a left join 表 b on a.城市=b.城市) a group by 日期'
exec(@s)
declare @s varchar(8000)
set @s=''
select @s=@s+',['+城市+']=sum(case 城市 when '''+城市+''' then 溫度 else null end)'
from 表 group by 城市
exec('select 日期'+@s+' from 表 group by 日期')
第三行是select @s=@s+',['+城市+']=sum(case 城市 when '''+城市+''' then 溫度 else null end)'你貼的卻是select @s=@s+',['+城市+']=max(case 城市 when '''+城市+''' then 溫度 else null end)'
set @s='select 日期 '
select @s=@s+',['+城市+']=sum(case 城市 when '''+城市+''' then 温度 else 0 end)'
from (select distinct 城市 from 城市对应表) as a
set @s='select 日期'+@s+' from 表 group by 日期'exec(@s)