求一SQL语句写法解决方案
求一SQL语句写法
数据库中的数据如下:
ID date summary
0000001 2010-01-01 早班
0000001 2010-01-01 晚班
0000001 2010-01-01 夜班
想用SQl语句查处数据 如下所示:
ID date summary1 summary2 summary3
0000001 2010-01-01 早班 晚班 夜班
请问如何写此语句?
------解决方案--------------------
mark
回复内容太短了!
------解决方案--------------------
select id,date,
case summary
when ‘早班’then summarey1,
when
when
from 表
group by id,date
------解决方案--------------------
不预先确定,无法用sql语句写出,用交叉数据窗口
------解决方案--------------------
动态的,只能通过后台存储过程实现,静态的可以用decode或者case来实现
------解决方案--------------------
内容不确定,一条语句就不够了,的写存储过程,或者用交叉表
------解决方案--------------------
涉及到行列互换的显示,要么自己在程序里转换,也可以使用pb数据窗口中最后一类,交叉表类型的数据窗口来显示 。
------解决方案--------------------
数据库中的数据如下:
ID date summary
0000001 2010-01-01 早班
0000001 2010-01-01 晚班
0000001 2010-01-01 夜班
想用SQl语句查处数据 如下所示:
ID date summary1 summary2 summary3
0000001 2010-01-01 早班 晚班 夜班
请问如何写此语句?
------解决方案--------------------
mark
回复内容太短了!
------解决方案--------------------
select id,date,
case summary
when ‘早班’then summarey1,
when
when
from 表
group by id,date
------解决方案--------------------
不预先确定,无法用sql语句写出,用交叉数据窗口
------解决方案--------------------
动态的,只能通过后台存储过程实现,静态的可以用decode或者case来实现
------解决方案--------------------
内容不确定,一条语句就不够了,的写存储过程,或者用交叉表
------解决方案--------------------
涉及到行列互换的显示,要么自己在程序里转换,也可以使用pb数据窗口中最后一类,交叉表类型的数据窗口来显示 。
------解决方案--------------------
- SQL code
--测试数据 if exists(select * from sysobjects where name = 't' and type = 'U') drop table t create table t(id varchar(10), date datetime, summary varchar(10)) insert t select '0000001', '2010-01-01', '早班' union all select '0000001', '2010-01-01', '晚班' union all select '0000001', '2010-01-01', '夜班' --查询 set nocount on select distinct summary into #1 from t select count(1) xh, a.summary into #2 from #1 a join #1 b on a.summary >= b.summary group by a.summary declare @sql varchar(8000) set @sql = 'select id, date' select @sql = @sql + ', max(case summary when ''' + summary + ''' then summary else null end) [summary' + convert(char(2), xh) + ']' from #2 set @sql = @sql + ' FROM t group by id, date' exec(@sql) drop table #1 drop table #2 drop table t set nocount off /* id date summary1 summary2 summary3 ---------- ----------------------- ---------- ---------- ---------- 0000001 2010-01-01 00:00:00.000 晚班 夜班 早班 */
------解决方案--------------------
最好用交叉报表来实现,比较简单点。
------解决方案--------------------
summary不确定,是不是说还要动态显示列。。