问一个sql语句,解决马上结贴给分!(快,先到独得)
时间:2011-12-12
来源:互联网
我有个一个表,结构是这样的:
CREATE TABLE [dbo].[TestTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Event1] [nvarchar](50) NULL,
[Event1At] [datetime] NULL,
[Event2] [nvarchar](50) NULL,
[Event2At] [datetime] NULL,
[Event3] [nvarchar](50) NULL,
[Event3At] [datetime] NULL
)
里面的数据这样的:
ID Event1 Event1At Event2 Event2At Event3 Event3At
--------------------------------------------------------------------------------------------------------
1 Login 2011-11-09 07:47:37.000 Attack 2011-11-09 07:47:52.000 Logout 2011-11-09 07:48:02.000
2 NULL NULL Attack 2011-11-10 15:23:05.000 NULL NULL
现在要一个select语句出来这样的记录:
ID Date Time Event1 Event2 Event3
---------------------------------------------------
1 2011-11-09 07:47:37 x
1 2011-11-09 07:47:52 x
1 2011-11-09 07:48:02 x
2 2011-11-10 15:23:05 x
我解释下:一条记录可能会SELECT出来多条(N<=3),如果记录中出现了Event1,那么就在Event1那个地方打个“x”,日期和时间列的值就是Event1的;如果记录中出现了Event2,那么就在Event2那个地方打个“x”,日期和时间列的值就是Event2的.... 如果没有就不要出现,所以一条记录会SELECT出来多条(N<=3)。这个应该不是很难。
CREATE TABLE [dbo].[TestTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Event1] [nvarchar](50) NULL,
[Event1At] [datetime] NULL,
[Event2] [nvarchar](50) NULL,
[Event2At] [datetime] NULL,
[Event3] [nvarchar](50) NULL,
[Event3At] [datetime] NULL
)
里面的数据这样的:
ID Event1 Event1At Event2 Event2At Event3 Event3At
--------------------------------------------------------------------------------------------------------
1 Login 2011-11-09 07:47:37.000 Attack 2011-11-09 07:47:52.000 Logout 2011-11-09 07:48:02.000
2 NULL NULL Attack 2011-11-10 15:23:05.000 NULL NULL
现在要一个select语句出来这样的记录:
ID Date Time Event1 Event2 Event3
---------------------------------------------------
1 2011-11-09 07:47:37 x
1 2011-11-09 07:47:52 x
1 2011-11-09 07:48:02 x
2 2011-11-10 15:23:05 x
我解释下:一条记录可能会SELECT出来多条(N<=3),如果记录中出现了Event1,那么就在Event1那个地方打个“x”,日期和时间列的值就是Event1的;如果记录中出现了Event2,那么就在Event2那个地方打个“x”,日期和时间列的值就是Event2的.... 如果没有就不要出现,所以一条记录会SELECT出来多条(N<=3)。这个应该不是很难。
作者: bigrongshu 发布时间: 2011-12-12
排版乱了。
作者: ssp2009 发布时间: 2011-12-12
SQL code
select * from [TestTable] as a unpivot (Value for Cols in([Event1At],[Event2At],[Event3At])) as b
作者: roy_88 发布时间: 2011-12-12
靠,csdn什么排版。最后那4个x不在一列上的啊。那4个x分别在1,2,3,2列上。对应Event1,2,3,Event2.
作者: bigrongshu 发布时间: 2011-12-12
SQL code
CREATE TABLE [dbo].[TestTable]( [ID] [int] IDENTITY(1,1) NOT NULL, [Event1] [nvarchar](50) NULL, [Event1At] [datetime] NULL, [Event2] [nvarchar](50) NULL, [Event2At] [datetime] NULL, [Event3] [nvarchar](50) NULL, [Event3At] [datetime] NULL ) insert into testtable select 'Login','2011-11-09 07:47:37.000','Attack','2011-11-09 07:47:52.000','Logout','2011-11-09 07:48:02.000' insert into testtable select NULL,NULL,'Attack','2011-11-10 15:23:05.000',NULL,NULL go select id,event1at as dt,'x' as event1,null as event2,null as event3 from testtable where event1 is not null union all select id,event1at as dt,null,'x',null from testtable where event2 is not null union all select id,event1at as dt,null,null,'x' from testtable where event3 is not null order by id /* id dt event1 event2 event3 ----------- ----------------------- ------ ------ ------ 1 2011-11-09 07:47:37.000 x NULL NULL 1 2011-11-09 07:47:37.000 NULL x NULL 1 2011-11-09 07:47:37.000 NULL NULL x 2 NULL NULL x NULL (4 行受影响) */ go drop table testtable
作者: qianjin036a 发布时间: 2011-12-12
SQL code
CREATE TABLE [dbo].[TestTable]( [ID] [int] IDENTITY(1,1) NOT NULL, [Event1] [nvarchar](50) NULL, [Event1At] [datetime] NULL, [Event2] [nvarchar](50) NULL, [Event2At] [datetime] NULL, [Event3] [nvarchar](50) NULL, [Event3At] [datetime] NULL ) insert into testtable select 'Login','2011-11-09 07:47:37.000','Attack','2011-11-09 07:47:52.000','Logout','2011-11-09 07:48:02.000' insert into testtable select NULL,NULL,'Attack','2011-11-10 15:23:05.000',NULL,NULL go select id,event1at as dt,'x' as event1,null as event2,null as event3 from testtable where event1 is not null union all select id,event2at as dt,null,'x',null from testtable where event2 is not null union all select id,event3at as dt,null,null,'x' from testtable where event3 is not null order by id /* id dt event1 event2 event3 ----------- ----------------------- ------ ------ ------ 1 2011-11-09 07:47:37.000 x NULL NULL 1 2011-11-09 07:47:52.000 NULL x NULL 1 2011-11-09 07:48:02.000 NULL NULL x 2 2011-11-10 15:23:05.000 NULL x NULL (4 行受影响) */ go drop table testtable
作者: qianjin036a 发布时间: 2011-12-12
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28