一道面試題

jayjiang0910 2011-03-07 06:27:10
有一張表兩個字段:_date,_num,
現在要產生如下的表格,請設計相關SQL語句。


年份 1月 2月 3月 …… 10月 11月 12月
2010 數量 數量 數量 …… 數量 數量 數量
2011 數量 數量 數量 …… 數量 數量 數量
……
...全文
164 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
michaelgong 2011-03-08
  • 打赏
  • 举报
回复

select nYear as 年份,(case id when 1 then '一月份' else '' end) as 一月份 ,
(case id when 2 then '二月份' else '' end) as 二月份 ,
(case id when 3 then '三月份' else '' end) as 三月份 ,
(case id when 4 then '四月份' else '' end) as 四月份 ,
(case id when 5 then '五月份' else '' end) as 五月份,
(case id when 6 then '六月份' else '' end) as 六月份 ,
(case id when 7 then '七月份' else '' end) as 七月份 ,
(case id when 8 then '八月份' else '' end) as 八月份
from Table_Time
//改改列名条件 应该可以。。。


gw6328 2011-03-08
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 xyctc 的回复:]
行转列成为十二五的重要议题
[/Quote]
强大!
Billy 2011-03-08
  • 打赏
  • 举报
回复
行转列成为十二五的重要议题
liang145 2011-03-08
  • 打赏
  • 举报
回复

select year(_date) as 年份,
Sum(case when month(_date)='01' then _num else 0 end ) as 一月,
Sum(case when month(_date)='02' then _num else 0 end ) as 二月,
Sum(case when month(_date)='03' then _num else 0 end ) as 三月,
Sum(case when month(_date)='04' then _num else 0 end ) as 四月,
Sum(case when month(_date)='05' then _num else 0 end ) as 五月,
Sum(case when month(_date)='06' then _num else 0 end ) as 六月,
Sum(case when month(_date)='07' then _num else 0 end ) as 七月,
Sum(case when month(_date)='08' then _num else 0 end ) as 八月,
Sum(case when month(_date)='09' then _num else 0 end ) as 九月,
Sum(case when month(_date)='10' then _num else 0 end ) as 十月,
Sum(case when month(_date)='11' then _num else 0 end ) as 十一月,
Sum(case when month(_date)='12' then _num else 0 end ) as 十二月
from #tryTable group by year(_date)
jayjiang0910 2011-03-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ssp2009 的回复:]
SQL code
select datepart(year,_date) [年份],
sum( case when datepart(month,_date)=1 then _num else 0 end) [1月],
sum( case when datepart(month,_date)=2 then _num else 0 end) [2月],
……
[/Quote]

這個不錯,當時面試我就是這樣做的,但是沒有用datepart()函數,直接用year()和month()函數的
happy664618843 2011-03-08
  • 打赏
  • 举报
回复
行转列 功能强大
jayjiang0910 2011-03-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 acherat 的回复:]
SQL code

declare @sql varchar(max)
set @sql = 'select convert(varchar(4),_date,120)'
select @sql = @sql + ',max(case convert(varchar(6),_date,112) when convert(varchar(4),_date,112) + ''' + ltrim(_date) + ''' then _num else 0 end)[' + ltrim(_date) + ']'
from (select month(_date) from tb)t
select @sql = @sql + ' from tb group by convert(varchar(4),_date,120)'
exec(@sql)

[/Quote]

2F回答的不錯,可是沒看懂題目,有一些瑕疵
其中convert(varchar(4),_date,120)可以修改為year(_date),max()應該使用sum(),convert(varchar(6),_date,112)可以修改為month(_date),對於select month(_date) from tb是不是少了group by month(_date)子句?

最後完整code如下:
declare @sql varchar(max)
set @sql = 'select year(_date) [年份]'
select @sql = @sql + ',sum(case month(_date) when ''' + ltrim(_date) + ''' then _num else 0 end) [' + ltrim(_date) + '月]'
from (select month(_date) _date from dbo.Test group by month(_date)) t
select @sql = @sql + ' from Test group by year(_date)'
exec(@sql)
szjol 2011-03-07
  • 打赏
  • 举报
回复
二楼正解,楼煮加油啊
打一壶酱油 2011-03-07
  • 打赏
  • 举报
回复
我不写行转列好多年
gggscfaii 2011-03-07
  • 打赏
  • 举报
回复
呵呵...sql函数还必须去练啊...
--小F-- 2011-03-07
  • 打赏
  • 举报
回复
又见行转列..
快溜 2011-03-07
  • 打赏
  • 举报
回复
select datepart(year,_date) [年份],
sum( case when datepart(month,_date)=1 then _num else 0 end) [1月],
sum( case when datepart(month,_date)=2 then _num else 0 end) [2月],
--......
from tb group by datepart(year,_date)
AcHerat 元老 2011-03-07
  • 打赏
  • 举报
回复

declare @sql varchar(max)
set @sql = 'select convert(varchar(4),_date,120)'
select @sql = @sql + ',max(case convert(varchar(6),_date,112) when convert(varchar(4),_date,112) + ''' + ltrim(_date) + ''' then _num else 0 end)[' + ltrim(_date) + ']'
from (select month(_date) from tb)t
select @sql = @sql + ' from tb group by convert(varchar(4),_date,120)'
exec(@sql)

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧