菜鸟求一句SQL语句

isgoodtime 2011-03-14 04:03:26
表 BrushCWA 有如下数据

PersonID BrushDate
1052 2011-03-01 07:34:30.000
1052 2011-03-01 07:44:16.000
1052 2011-03-01 12:01:48.000
1052 2011-03-01 12:43:46.000
1052 2011-03-01 17:00:49.000
1052 2011-03-01 17:24:08.000
1052 2011-03-01 20:32:03.000
1052 2011-03-02 07:48:09.000
1052 2011-03-02 12:01:51.000
1052 2011-03-02 13:27:39.000
1052 2011-03-02 17:01:30.000
1052 2011-03-02 17:24:28.000
1052 2011-03-02 20:05:45.000

我现在想求一条SQL语句,使查询出来的结果如下显示:
编号 日期 时间One 时间Two 时间Three 时间Four 时间Five 时间Six 时间Seven
1052 2011-03-01 07:34:30 07:44:16 12:01:48 12:43:46 17:00:49 17:24:08 20:32:03
1052 2011-03-02 07:48:09 12:01:51 13:27:39 17:01:30 17:24:28 20:05:45

那个时间1234567 每天最多不会超过八条记录 也就是说最多到 时间12345678

各位老师,大哥大姐,帅哥靓女,一定要帮小弟俺这个忙啦,感激不尽,分不多,也就是20分,先说声谢谢了
...全文
175 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
isgoodtime 2011-03-18
  • 打赏
  • 举报
回复
#11楼

消息 102,级别 15,状态 1,第 1 行
'b' 附近有语法错误。

#12楼

问题都没解决怎么给分呢
lijianyun2008 2011-03-15
  • 打赏
  • 举报
回复
给分了。。。。。。。。。。。。
lijianyun2008 2011-03-15
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select PersonID,YM as 日期 '
select @sql = @sql + ' ,max(case cast(d as varchar) when '''+cast(d as varchar)+''' then right(CONVERT(varchar, brushDate, 120 ),9) else null end)[时间'+cast(isnull(d,0) as varchar)+']'
from (select distinct d from (select a.PersonID,left(CONVERT(varchar, a.brushDate, 120 ),10) as YM,a.brushDate,(select count(1)+1 from #temp b where b.brushDate >=
left(CONVERT(varchar, a.brushDate, 120 ),10) and b.brushDate < a.brushDate and b.PersonID=a.PersonID)
as d from #temp a ) s) as b
set @sql = @sql + 'from (select a.PersonID,left(CONVERT(varchar, a.brushDate, 120 ),10) as YM,a.brushDate,(select count(1)+1 from #temp b where b.brushDate >=
left(CONVERT(varchar, a.brushDate, 120 ),10) and b.brushDate < a.brushDate and b.PersonID=a.PersonID)
as d from #temp a ) as m group by PersonID,YM'
exec(@sql)
isgoodtime 2011-03-15
  • 打赏
  • 举报
回复
BrushCWA 是一个打卡记录,里面有几万条数据,当我把六楼和七楼的版本调高后,发现查询结果中,只有少数记录里面有一个时间,其它全为NULL

1296 2011-03-09 NULL NULL NULL NULL NULL NULL NULL
1299 2011-03-09 NULL NULL NULL NULL NULL NULL NULL
1301 2011-03-09 NULL 02:01:08NULL NULL NULL NULL NULL
1310 2011-03-09 NULL NULL NULL NULL NULL NULL NULL
isgoodtime 2011-03-15
  • 打赏
  • 举报
回复
select * from BrushCWA 得如下数据
1052 2011-03-01 07:34:30.000
1052 2011-03-01 07:44:16.000
1052 2011-03-01 12:01:48.000
1052 2011-03-01 12:43:46.000
1052 2011-03-01 17:00:49.000
1052 2011-03-01 17:24:08.000
1052 2011-03-01 20:32:03.000
1052 2011-03-02 07:48:09.000
1052 2011-03-02 12:01:51.000
1052 2011-03-02 13:27:39.000
1052 2011-03-02 17:01:30.000
1052 2011-03-02 17:24:28.000
1052 2011-03-02 20:05:45.000
..........

现求查询表BrushCWA中的数据如下显示

编号 日期 时间One 时间Two 时间Three 时间Four 时间Five 时间Six 时间Seven
1052 2011-03-01 07:34:30 07:44:16 12:01:48 12:43:46 17:00:49 17:24:08 20:32:03
1052 2011-03-02 07:48:09 12:01:51 13:27:39 17:01:30 17:24:28 20:05:45
..........
isgoodtime 2011-03-15
  • 打赏
  • 举报
回复
六楼以及七楼报如下错误:

'pivot' 附近有语法错误。您可能需要将当前数据库的兼容级别设置为更高的值,以启用此功能。有关存储过程 sp_dbcmptlevel 的信息,请参见帮助。
Shawn 2011-03-14
  • 打赏
  • 举报
回复
--SQL
select
PersonID,
日期 = [date],
时间One = convert(varchar(8),[1],108),
时间Two = convert(varchar(8),[2],108),
时间Three = convert(varchar(8),[3],108),
时间Four = convert(varchar(8),[4],108),
时间Five = convert(varchar(8),[5],108),
时间Six = convert(varchar(8),[6],108),
时间Seven= convert(varchar(8),[7],108)
from
(
select
rowno=ROW_NUMBER() over(partition by convert(varchar(10), BrushDate, 120) order by BrushDate),
[date] = convert(varchar(10), BrushDate, 120), *
from #temp
) a
pivot
(max(BrushDate) for rowno in([1],[2],[3],[4],[5],[6],[7])) b
/*
PersonID 日期 时间One 时间Two 时间Three 时间Four 时间Five 时间Six 时间Seven
1052 2011-03-01 07:34:30 07:44:16 12:01:48 12:43:46 17:00:49 17:24:08 20:32:03
1052 2011-03-02 07:48:09 12:01:51 13:27:39 17:01:30 17:24:28 20:05:45 NULL
*/
Shawn 2011-03-14
  • 打赏
  • 举报
回复
create table #temp(PersonID int, BrushDate datetime)
insert #temp
select 1052, '2011-03-01 07:34:30.000' union all
select 1052, '2011-03-01 07:44:16.000' union all
select 1052, '2011-03-01 12:01:48.000' union all
select 1052, '2011-03-01 12:43:46.000' union all
select 1052, '2011-03-01 17:00:49.000' union all
select 1052, '2011-03-01 17:24:08.000' union all
select 1052, '2011-03-01 20:32:03.000' union all
select 1052, '2011-03-02 07:48:09.000' union all
select 1052, '2011-03-02 12:01:51.000' union all
select 1052, '2011-03-02 13:27:39.000' union all
select 1052, '2011-03-02 17:01:30.000' union all
select 1052, '2011-03-02 17:24:28.000' union all
select 1052, '2011-03-02 20:05:45.000'
go
--SQL
select
PersonID,
日期 = [date],
时间One = [1],
时间Two = [2],
时间Three = [3],
时间Four = [4],
时间Five = [5],
时间Six = [6],
时间Seven= [7]
from
(
select
rowno=ROW_NUMBER() over(partition by convert(varchar(10), BrushDate, 120) order by BrushDate),
[date] = convert(varchar(10), BrushDate, 120), *
from #temp
) a
pivot
(max(BrushDate) for rowno in([1],[2],[3],[4],[5],[6],[7])) b
/*
PersonID 日期 时间One 时间Two 时间Three 时间Four 时间Five 时间Six 时间Seven
1052 2011-03-01 2011-03-01 07:34:30.000 2011-03-01 07:44:16.000 2011-03-01 12:01:48.000 2011-03-01 12:43:46.000 2011-03-01 17:00:49.000 2011-03-01 17:24:08.000 2011-03-01 20:32:03.000
1052 2011-03-02 2011-03-02 07:48:09.000 2011-03-02 12:01:51.000 2011-03-02 13:27:39.000 2011-03-02 17:01:30.000 2011-03-02 17:24:28.000 2011-03-02 20:05:45.000 NULL
*/
bluesmiler 2011-03-14
  • 打赏
  • 举报
回复
这样的应该可以满足了吧
bluesmiler 2011-03-14
  • 打赏
  • 举报
回复
时间1,时间2...这样的好弄,时间one,时间two这样的要自己定义。。。


declare @sql nvarchar(4000)
select @sql=ISNULL(@sql+'],[','')+'时间'+CAST(a.id as varchar) from
(select distinct ROW_NUMBER() over(partition by convert(nvarchar(10),BrushDate,120) order by BrushDate) id from BrushCWA) a
set @sql='['+@sql+']'

exec ('select * from
(select personid,convert(nvarchar(10),BrushDate,120) BrushDate,convert(nvarchar(12),BrushDate,114) BrushDate1,''时间''+cast(ROW_NUMBER() over(partition by convert(nvarchar(10),BrushDate,120) order by BrushDate) as varchar) id
from BrushCWA) a pivot (max(BrushDate1) for id in (' + @sql + ')) b')
isgoodtime 2011-03-14
  • 打赏
  • 举报
回复
求代码了,再次谢谢了!
cutebear2008 2011-03-14
  • 打赏
  • 举报
回复
其实有点难的!
bluesmiler 2011-03-14
  • 打赏
  • 举报
回复
行转列。。。

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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