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

推荐订阅源

P
Privacy International News Feed
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
爱范儿
爱范儿
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
AWS News Blog
AWS News Blog
T
Threatpost
博客园 - 叶小钗
Recent Announcements
Recent Announcements
C
Check Point Blog
H
Heimdal Security Blog
量子位
G
GRAHAM CLULEY
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
L
Lohrmann on Cybersecurity
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Cybersecurity and Infrastructure Security Agency CISA
The Last Watchdog
The Last Watchdog
B
Blog
T
Tor Project blog
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
T
Tenable Blog
aimingoo的专栏
aimingoo的专栏
P
Palo Alto Networks Blog
Vercel News
Vercel News
V
Visual Studio Blog
The Register - Security
The Register - Security
NISL@THU
NISL@THU
Spread Privacy
Spread Privacy
GbyAI
GbyAI
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
T
Troy Hunt's Blog
雷峰网
雷峰网
Security Latest
Security Latest
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Scott Helme
Scott Helme
S
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CERT Recently Published Vulnerability Notes
美团技术团队

博客园 - Lucky Jack

在C#中展示嵌入的RTF文件 SQL进行排序、分组、统计的10个新技巧 select into 和 insert into select的区别 Convert的妙用 DataGridView中回车键的妙用 如何去除C#Strings中的空格? Format String for XML Value - Lucky Jack 如何改变字体大小呢? 如何改变字体风格? C# String小技巧 如何避免按回车键时的嗡鸣声? - Lucky Jack - 博客园 如何嵌入图片资源? Lookupedit使用小记 如何优雅的编程? 文件监视器( FileSystemWatcher) 类的使用 - Lucky Jack 反射也可以这样? - Lucky Jack - 博客园 浅谈对象的初始化顺序 也谈String.IsNullOrEmpty 经典的属性设置!
经典sql
Lucky Jack · 2008-02-16 · via 博客园 - Lucky Jack

行转列:  

  --测试数据  

  create   table   基础表(姓名   nvarchar(10),课程名   nvarchar(20),成绩   decimal(10,1))  

  insert   基础表      select   '王家喜','计算机基础',69.0  

  union     all       select   '王家喜','*理论',74.0  

  union     all       select   '王家喜','英语(上)',86.0  

  union     all       select   '王家喜','普通逻辑学',91.0  

  union     all       select   '施春林','立法学教程',60.0  

  union     all       select   '施春林','经管原理'     ,73.0  

  union     all       select   '施春林','英语(上)',73.0  

  union     all       select   '施春林','普通逻辑学',90.0  

  go 

  --查询  

  declare   @s   varchar(8000),@i   varchar(10)

  select   top   1   @s='',@i=count(*)  

  from   基础表   group   by   姓名   order   by   count(*)   desc  

  while   @i>0  

  select   @s=',[课程'+@i+']=max(case   when   id='+@i+'   then   课程名   else   ''''   end),

              [成绩'+@i+']=max(case   when   id='+@i+'   then   成绩   end)'+@s ,@i=@i-1  

  exec(' select   姓名,课程名,成绩,id=0   into   #t   from   基础表   order   by   姓名  

      declare   @i   int,@姓名   varchar(10)  

      update   #t   set   @i=case   when   @姓名=姓名   then   @i+1   else   1   end,id=@i,@姓名=姓名  

      select   姓名'+@s+'   from   #t   group   by   姓名')  

  go  

  --删除测试 

  select * from   table_goods

  drop   table   基础表 

  select * from   table_goods

declare @str varchar(1000)

set @str='select id,visit '

select @str=@str+',min(case when goods='+ Cast(goods As Varchar) +' then goods_id else Null end) as goods_id'+ Cast(goods As Varchar)

+',min(case when goods='+ Cast(goods As Varchar) +' then name else Null end) as goods'+ Cast(goods As Varchar)

from table_goods group by goods

select @str=@str+' from table_goods group by id,visit order by id,visit'

print @str exec(@str)

select id,visit ,max(case when goods=1 then goods_id else Null end) as goods_id1,

               max(case when goods=1 then name else Null end) as goods1,

               max(case when goods=2 then goods_id else Null end) as goods_id2,

               max(case when goods=2 then name else Null end) as goods2,

             max(case when goods=3 then goods_id else Null end) as goods_id3,

              max(case when goods=3 then name else Null end) as goods3,

               max(case when goods=4 then goods_id else Null end) as goods_id4,

                 max(case when goods=4 then name else Null end) as goods4

from table_goods

group by  id,visit order by id,visit