当前位置: 代码迷 >> Sql Server >> 求一sql语句 100分解决方案
  详细解决方案

求一sql语句 100分解决方案

热度:61   发布时间:2016-04-27 14:31:59.0
求一sql语句 100分
有三个表A,B,C相同结构如下:

Time,Data

如:

A B C

Time Data Time Data Time Data

08:59 12 07:59 1 08:59 23 
09:00 13 09:00 2 11:00 25
10:01 15 11:00 3 12:00 26

以时间排列,组合数据如下:
Time A_Data B_Data C_Data
07:59 1  
08:59 12 23
09:00 13 2
10:01 15
11:00 3 25
12:00 26  


sql 最后精简一些,运行速度快一点,非常感谢,这贴限制最多只能给100分,分不够说一声,我另开贴加分。


非常感谢。




------解决方案--------------------
SQL code
;with cte as(    select time from a    union    select time from b    union    select time from c)select t.time,a.data as A_data,b.data as B_data,c.data as C_datafrom cte t left join a on t.time = a.time           left join b on t.time = b.time           left join c on t.time = c.time
------解决方案--------------------
SQL code
if object_id('[a]') is not null drop table [a]gocreate table [a]([Time] varchar(5),[Data] int)insert [a]select '08:59',12 union allselect '09:00',13 union allselect '10:01',15goif object_id('[b]') is not null drop table [b]gocreate table [b]([Time] varchar(5),[Data] int)insert [b]select '07:59',1 union allselect '09:00',2 union allselect '11:00',3goif object_id('[c]') is not null drop table [c]gocreate table [c]([Time] varchar(5),[Data] int)insert [c]select '08:59',23 union allselect '11:00',25 union allselect '12:00',26goselect isnull(isnull(a.time,b.time),c.time) as time,a.data,b.data,c.datafrom afull join b on a.time=b.timefull join c on a.time=c.time or b.time=c.time/**time  data        data        data----- ----------- ----------- -----------08:59 12          NULL        2309:00 13          2           NULL10:01 15          NULL        NULL07:59 NULL        1           NULL11:00 NULL        3           2512:00 NULL        NULL        26(6 行受影响)**/
  相关解决方案