惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
MyScale Blog
MyScale Blog
雷峰网
雷峰网
量子位
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
T
Tailwind CSS Blog
月光博客
月光博客
博客园 - 【当耐特】
博客园_首页
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
爱范儿
爱范儿
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
The Cloudflare Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - CODE

jquery 加载 - CODE - 博客园 jquery ajxj - CODE - 博客园 页面复合表头的做法 - CODE - 博客园 自动产生树,并建立树结构 - CODE - 博客园 HTML 自动产生Radio分组 - CODE - 博客园 获取Table当前单元格的值 - CODE - 博客园 (转)优化数据库的思想及sql语句优化的原则 软件孵化室自助基金成立仪式 转: 移动客户端系统升级思路 转:调用Windows Mobile自带的控制面板项 程序员的基本素质 BCP的应用 VC++ 6.0访问Sql Server 协会的篮球比赛 软件协会05级高级会员离校仪式--发言稿 软输入面板 软件孵化室为筹集“静·挚”基金倡议书 微软的Webcasts系列讲座 http://www.cnblogs.com/upto/archive/2006/04/10/CompressWebService.html(收集)
sql server 行变成列的DEMO
CODE · 2009-11-02 · via 博客园 - CODE

create table A
(
 属性ID int,
 年份 int
)

delete a
select * From  A

insert  A
select 1, 2008
insert  A
select 2, 2009

create table B
(
 年份 int,
 月份 int,
    金额 int

)
insert B
select 2008,1,20
insert B
select 2008,2,30
insert B
select 2008,3,40
insert B
select 2008,4,10
insert B
select 2009,1,60
insert B
select 2009,2,25
insert B
select 2009,3,5
insert B
select 2009,4,10


select
  A.属性ID,
  A.年份,
   max(case when (A.年份=B.年份) and (B.月份=1) then B.金额 END) as '一月',
 max(case when (A.年份=B.年份) and (B.月份=2) then B.金额 END) as '二月',
 max(case when (A.年份=B.年份) and (B.月份=3) then B.金额 END) as '三月',
 max(case when (A.年份=B.年份) and (B.月份=4) then B.金额 END) as '四月'
From A
  inner join B on A.年份 = B.年份
group by A.属性ID,A.年份

1 2008 20 30 40 10
2 2009 60 NULL 5 10