22,209
社区成员
发帖
与我相关
我的任务
分享
if object_id('tempdb.dbo.#BrandMessage') is not null drop table #BrandMessage
create table #BrandMessage([brandID] int,[brandJoinDate] datetime)
insert #BrandMessage
select 7,'2009-08-21 18:15:12.000' union all
select 4,'2009-08-21 15:07:59.000' union all
select 3,'2009-08-20 10:26:24.000' union all
select 2,'2009-08-14 19:04:27.000' union all
select 1,'2009-08-05 15:03:04.000'
--> 测试数据:#ALLCompany
if object_id('tempdb.dbo.#ALLCompany') is not null drop table #ALLCompany
create table #ALLCompany([companyID] int,[companyJoinDate] datetime)
insert #ALLCompany
select 7,'2009-08-21 18:16:45.000' union all
select 5,'2009-08-21 15:07:14.000' union all
select 4,'2009-08-20 10:24:46.000' union all
select 2,'2009-08-14 19:02:52.000' union all
select 1,'2009-08-05 15:00:34.000'
--> 测试数据:#MessageBoard
if object_id('tempdb.dbo.#MessageBoard') is not null drop table #MessageBoard
create table #MessageBoard([messageID] int,[messageDate] datetime)
insert #MessageBoard
select 5,'2009-08-11 18:06:44.437' union all
select 4,'2009-08-10 17:21:49.107' union all
select 3,'2009-08-10 17:14:13.450' union all
select 2,'2009-08-10 17:11:27.250' union all
select 1,'2009-08-10 17:01:45.140'
select * from
(
select brandID,brandJoinDate,'a' as tableName from #BrandMessage
union all
select companyID,companyJoinDate,'b' as tableName from #ALLCompany
union all
select messageID,messageDate,'c' as tableName from #MessageBoard
) a
order by tableName,brandJoinDate desc
brandID brandJoinDate tableName
----------- ----------------------- ---------
7 2009-08-21 18:15:12.000 a
4 2009-08-21 15:07:59.000 a
3 2009-08-20 10:26:24.000 a
2 2009-08-14 19:04:27.000 a
1 2009-08-05 15:03:04.000 a
7 2009-08-21 18:16:45.000 b
5 2009-08-21 15:07:14.000 b
4 2009-08-20 10:24:46.000 b
2 2009-08-14 19:02:52.000 b
1 2009-08-05 15:00:34.000 b
5 2009-08-11 18:06:44.437 c
4 2009-08-10 17:21:49.107 c
3 2009-08-10 17:14:13.450 c
2 2009-08-10 17:11:27.250 c
1 2009-08-10 17:01:45.140 c
(15 行受影响)
--> 测试数据:#BrandMessage
if object_id('tempdb.dbo.#BrandMessage') is not null drop table #BrandMessage
create table #BrandMessage([brandID] int,[brandJoinDate] datetime)
insert #BrandMessage
select 7,'2009-08-21 18:15:12.000' union all
select 4,'2009-08-21 15:07:59.000' union all
select 3,'2009-08-20 10:26:24.000' union all
select 2,'2009-08-14 19:04:27.000' union all
select 1,'2009-08-05 15:03:04.000'
--> 测试数据:#ALLCompany
if object_id('tempdb.dbo.#ALLCompany') is not null drop table #ALLCompany
create table #ALLCompany([companyID] int,[companyJoinDate] datetime)
insert #ALLCompany
select 7,'2009-08-21 18:16:45.000' union all
select 5,'2009-08-21 15:07:14.000' union all
select 4,'2009-08-20 10:24:46.000' union all
select 2,'2009-08-14 19:02:52.000' union all
select 1,'2009-08-05 15:00:34.000'
--> 测试数据:#MessageBoard
if object_id('tempdb.dbo.#MessageBoard') is not null drop table #MessageBoard
create table #MessageBoard([messageID] int,[messageDate] datetime)
insert #MessageBoard
select 5,'2009-08-11 18:06:44.437' union all
select 4,'2009-08-10 17:21:49.107' union all
select 3,'2009-08-10 17:14:13.450' union all
select 2,'2009-08-10 17:11:27.250' union all
select 1,'2009-08-10 17:01:45.140'
-->查询
select * from (
select brandID,brandJoinDate,tableName='a' from #BrandMessage
union all
select companyID,companyJoinDate,tableName='b' from #ALLCompany
union all
select messageID,messageDate,tableName='c' from #MessageBoard
) a
order by
tableName,
brandJoinDate desc
/**
brandID brandJoinDate tableName
----------- ----------------------- ---------
7 2009-08-21 18:15:12.000 a
4 2009-08-21 15:07:59.000 a
3 2009-08-20 10:26:24.000 a
2 2009-08-14 19:04:27.000 a
1 2009-08-05 15:03:04.000 a
7 2009-08-21 18:16:45.000 b
5 2009-08-21 15:07:14.000 b
4 2009-08-20 10:24:46.000 b
2 2009-08-14 19:02:52.000 b
1 2009-08-05 15:00:34.000 b
5 2009-08-11 18:06:44.437 c
4 2009-08-10 17:21:49.107 c
3 2009-08-10 17:14:13.450 c
2 2009-08-10 17:11:27.250 c
1 2009-08-10 17:01:45.140 c
(15 行受影响)
**/
create table #TB
(
A char(10),
B char(10),
C char(10)
)
insert into #TB select 'a1','b1','c1'
union all select 'a2','b2','c2'
union all select 'a3','b3','c3'
select * from
(
select *,'a' as tableName from #TB
union all
select *,'b' as tableName from #TB
union all
select *,'c' as tableName from #TB
)T
A B C tableName
---------- ---------- ---------- ---------
a1 b1 c1 a
a2 b2 c2 a
a3 b3 c3 a
a1 b1 c1 b
a2 b2 c2 b
a3 b3 c3 b
a1 b1 c1 c
a2 b2 c2 c
a3 b3 c3 c
select * from (
select top 100 percent brandID,brandJoinDate,'a' as a from BrandMessage order by brandJoinDate desc
union
select top 100 percent companyID,companyJoinDate,'b' as b from ALLCompany order by companyJoinDate
desc
union
select top 100 percent messageID,messageDate,'c' as c from MessageBoard order by messageDate desc
) a order by brandJoinDate desc
select * from (
select brandID,brandJoinDate,tableName='a' from BrandMessage
union all
select companyID,companyJoinDate,tableName='b' from ALLCompany
union all
select messageID,messageDate,tableName='c' from MessageBoard
) a
order by
tableName,
brandJoinDate desc
select * from (
select top 100 percent brandID,brandJoinDate,'a' from BrandMessage order by brandJoinDate desc
union
select top 100 percent companyID,companyJoinDate,'b' from ALLCompany order by companyJoinDate
desc
union
select top 100 percent messageID,messageDate'c' from MessageBoard order by messageDate desc
) a order by brandJoinDate desc