【SQL】高级SQL处理,根据基本表,动态设置显示表列的样式

kingcsx666 2009-05-26 09:14:42
直接看测试数据

主要是动态设置结果表的列,根据@TmpTable的Remark,设置结果表的列,并填充数据

请各位高手看看,先谢谢各位啦


--建立测试数据
Declare @TmpTable table(ID smallint,level varchar(20),Remark Varchar(20))
Insert Into @TmpTable(ID,level,Remark)
Select 1,'aa','1-10'
Union All Select 2,'bb','11-20'
Union All Select 3,'cc','21-30'
Union All Select 4,'dd','31-50'
Union All Select 5,'ee','51-9999'
Select * From @TmpTable

Declare @TmpBill Table(ID smallint,Student varchar(20),Grade smallint)
Insert Into @TmpBill(ID,Student,Grade)
Select 1,'张三',10
Union All Select 2,'李四',34
Union All Select 3,'王五',24
Union All Select 4,'阿扁',18
Union All Select 5,'花花',98
Union All Select 6,'黄河',45
Select * From @TmpBill

--算法:@TmpTable 用来维护结果表分数区间断的动态列,可自由添加
-- @TmpBill 为学生分数表
-- @TmpReslut 为最终结果表,即需要的动态样式


--处理后的结果
Declare @TmpReslut Table(ID smallint,Student varchar(20),aa smallint,bb smallint,cc smallint,dd smallint,ee smallint)
Insert Into @TmpReslut(ID,Student,aa,bb,cc,dd,ee)
Select 1,'张三',10,0,0,0,0
Union All Select 2,'李四',0,0,0,34,0
Union All Select 3,'王五',0,0,24,0,0
Union All Select 4,'阿扁',0,18,0,0,0
Union All Select 5,'花花',0,0,0,0,98
Union All Select 6,'黄河',0,0,0,45,0
Select * From @TmpReslut

...全文
133 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
guoying_lu 2009-05-31
  • 打赏
  • 举报
回复
学习~
kingcsx666 2009-05-27
  • 打赏
  • 举报
回复
谢谢josy 和htl258 方法都是正确的,


因为私下问了htl258一些问题,所以这样结贴给分了
kingcsx666 2009-05-26
  • 打赏
  • 举报
回复
谢谢htl258

还是动态的好

后面的查询时枚举出来

达不到实际的效果
htl258_Tony 2009-05-26
  • 打赏
  • 举报
回复
IF OBJECT_ID('[TmpTable]') IS NOT NULL DROP TABLE [TmpTable] 
GO
create table TmpTable(ID smallint,level varchar(20),Remark Varchar(20))
Insert Into TmpTable(ID,level,Remark)
Select 1,'aa','1-10'
Union All Select 2,'bb','11-20'
Union All Select 3,'cc','21-30'
Union All Select 4,'dd','31-50'
Union All Select 5,'ee','51-9999'

IF OBJECT_ID('[TmpBill]') IS NOT NULL DROP TABLE [TmpBill]
GO
create table TmpBill(ID smallint,Student varchar(20),Grade smallint)
Insert Into TmpBill(ID,Student,Grade)
Select 1,'张三',10
Union All Select 2,'李四',34
Union All Select 3,'王五',24
Union All Select 4,'阿扁',18
Union All Select 5,'花花',98
Union All Select 6,'黄河',45
--查询
select a.id,a.student,
max(case when b.[level]='aa' then Grade else 0 end) [aa],
max(case when b.[level]='bb' then Grade else 0 end) [bb],
max(case when b.[level]='cc' then Grade else 0 end) [cc],
max(case when b.[level]='dd' then Grade else 0 end) [dd],
max(case when b.[level]='ee' then Grade else 0 end) [ee]
from TmpBill a
join TmpTable b
on a.Grade between left(Remark,charindex('-',Remark)-1)*1 and right(Remark,len(Remark)-charindex('-',Remark))*1
group by a.id,a.student
order by id
/*
id student aa bb cc dd ee
------ -------------------- ----------- ----------- ----------- ----------- -----------
1 张三 10 0 0 0 0
2 李四 0 0 0 34 0
3 王五 0 0 24 0 0
4 阿扁 0 18 0 0 0
5 花花 0 0 0 0 98
6 黄河 0 0 0 45 0

(6 行受影响)
*/
可以
kingcsx666 2009-05-26
  • 打赏
  • 举报
回复
问高手josy 和htl258

问什么都要动态sql

我改成普通sql怎么不行呢

完全一样的写法
ai_li7758521 2009-05-26
  • 打赏
  • 举报
回复
学习
kingcsx666 2009-05-26
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 Zoezs 的回复:]
引用 8 楼 kingcsx666 的回复:
谢谢josy 和htl258

各位继续

没什么好继续的了,都写好了。
[/Quote]

比较一下算法
Zoezs 2009-05-26
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 kingcsx666 的回复:]
谢谢josy 和htl258

各位继续
[/Quote]
没什么好继续的了,都写好了。
kingcsx666 2009-05-26
  • 打赏
  • 举报
回复
谢谢josy 和htl258

各位继续
--小F-- 2009-05-26
  • 打赏
  • 举报
回复
我是进来学习的
kingcsx666 2009-05-26
  • 打赏
  • 举报
回复
楼上高手强悍,谢谢啦

先试试,

各位继续
htl258_Tony 2009-05-26
  • 打赏
  • 举报
回复
--建立测试数据
IF OBJECT_ID('[TmpTable]') IS NOT NULL DROP TABLE [TmpTable]
GO
create table TmpTable(ID smallint,level varchar(20),Remark Varchar(20))
Insert Into TmpTable(ID,level,Remark)
Select 1,'aa','1-10'
Union All Select 2,'bb','11-20'
Union All Select 3,'cc','21-30'
Union All Select 4,'dd','31-50'
Union All Select 5,'ee','51-9999'
Select * From TmpTable
IF OBJECT_ID('[TmpBill]') IS NOT NULL DROP TABLE [TmpBill]
GO
create table TmpBill(ID smallint,Student varchar(20),Grade smallint)
Insert Into TmpBill(ID,Student,Grade)
Select 1,'张三',10
Union All Select 2,'李四',34
Union All Select 3,'王五',24
Union All Select 4,'阿扁',18
Union All Select 5,'花花',98
Union All Select 6,'黄河',45
Select * From TmpBill

--查询
declare @s varchar(8000)
select @s='select a.id,a.student'
select @s=@s+',max(case when b.[level]='''+[level]+''' then Grade else 0 end) ['+[level]+']'
from TmpTable
select @s=@s+'
from TmpBill a
join TmpTable b
on a.Grade between left(Remark,charindex(''-'',Remark)-1)*1 and right(Remark,len(Remark)-charindex(''-'',Remark))*1
group by a.id,a.student
order by id'
exec(@s)

--结果
/*
id student aa bb cc dd ee
------ -------------------- ----------- ----------- ----------- ----------- -----------
1 张三 10 0 0 0 0
2 李四 0 0 0 34 0
3 王五 0 0 24 0 0
4 阿扁 0 18 0 0 0
5 花花 0 0 0 0 98
6 黄河 0 0 0 45 0

(6 行受影响)
*/
百年树人 2009-05-26
  • 打赏
  • 举报
回复
--建立测试数据
create table TmpTable(ID smallint,level varchar(20),Remark Varchar(20))
Insert Into TmpTable(ID,level,Remark)
Select 1,'aa','1-10'
Union All Select 2,'bb','11-20'
Union All Select 3,'cc','21-30'
Union All Select 4,'dd','31-50'
Union All Select 5,'ee','51-9999'
Select * From TmpTable

create table TmpBill(ID smallint,Student varchar(20),Grade smallint)
Insert Into TmpBill(ID,Student,Grade)
Select 1,'张三',10
Union All Select 2,'李四',34
Union All Select 3,'王五',24
Union All Select 4,'阿扁',18
Union All Select 5,'花花',98
Union All Select 6,'黄河',45
Select * From TmpBill


declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([level])+'=max(case when [level]='+quotename([level],'''')+' then [GRADE] else 0 end)'
from
(SELECT A.*,B.GRADE FROM TmpTable A,TmpBill B WHERE B.GRADE BETWEEN CAST(LEFT(REMARK,CHARINDEX('-',REMARK)-1) AS INT) AND CAST(RIGHT(REMARK,LEN(REMARK)-CHARINDEX('-',REMARK)) AS INT)) T
group by [level]
exec('select [ID],[Student]'+@s+' from (SELECT A.[level],B.* FROM TmpTable A,TmpBill B WHERE B.GRADE BETWEEN CAST(LEFT(REMARK,CHARINDEX(''-'',REMARK)-1) AS INT) AND CAST(RIGHT(REMARK,LEN(REMARK)-CHARINDEX(''-'',REMARK)) AS INT)) T group by [ID],[Student] order by id')



/**
ID Student aa bb cc dd ee
------ -------------------- ----------- ----------- ----------- ----------- -----------
1 张三 10 0 0 0 0
2 李四 0 0 0 34 0
3 王五 0 0 24 0 0
4 阿扁 0 18 0 0 0
5 花花 0 0 0 0 98
6 黄河 0 0 0 45 0
**/
htl258_Tony 2009-05-26
  • 打赏
  • 举报
回复
不急,看看先
kingcsx666 2009-05-26
  • 打赏
  • 举报
回复
up,这个东西确实不容易弄啊

还请各位高手留步
SQL77 2009-05-26
  • 打赏
  • 举报
回复
可能要用到动态SQL!!!不过不清楚根据REMARK怎么弄成那样的???不明白意思

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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