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

推荐订阅源

小众软件
小众软件
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
Blog — PlanetScale
Blog — PlanetScale
爱范儿
爱范儿
J
Java Code Geeks
A
About on SuperTechFans
F
Fortinet All Blogs
B
Blog
aimingoo的专栏
aimingoo的专栏
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
有赞技术团队
有赞技术团队
G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
V
V2EX
博客园_首页
博客园 - 叶小钗
罗磊的独立博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
云风的 BLOG
云风的 BLOG

博客园 - 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