日期:2014-05-18  浏览次数:20515 次

求sql语句 谢谢
month_id day_time extend_CHJ extend_HSDY extend_SBQX  
1 2011-09-15 00:00:00.000 435.00 435.00 345.00  
2 2011-09-16 00:00:00.000 2343.00 45.00 334.33

上面是表结构。
根据月份时间查询,想要结果  
1 2011-09-1 00:00:00.000 NUll NUll NUll  
2 2011-09-2 00:00:00.000 NUll NUll NUll  
3 2011-09-3 00:00:00.000 NUll NUll NUll  
4 2011-09-4 00:00:00.000 NUll NUll NUll  
5 2011-09-5 00:00:00.000 NUll NUll NUll  
6 2011-09-6 00:00:00.000 NUll NUll NUll  
.........
15 2011-09-15 00:00:00.000 435.00 435.00 345.00  
16 2011-09-16 00:00:00.000 2343.00 45.00 334.33
.........
29 2011-09-29 00:00:00.000 NUll NUll NUll  
30 2011-09-30 00:00:00.000 NUll NUll NUll  


   


------解决方案--------------------
SQL code
生成一个日期表,然后使用左连接

select m.* , n.extend_CHJ ,n.extend_HSDY ,n.extend_SBQX from
(
select 1 monthid , '2011-09-1' day_time union all
select 2 monthid , '2011-09-2' day_time union all
...
select 30 monthid , '2011-09-30' day_time
) m left join
tb n
on m.day_time = n.day_time

------解决方案--------------------
SQL code
create table tb(month_id int,day_time datetime,extend_CHJ decimal(18,2),extend_HSDY decimal(18,2),extend_SBQX decimal(18,2))
insert into tb values(1 ,'2011-09-15 00:00:00.000', 435.00  ,435.00 ,345.00)
insert into tb values(2 ,'2011-09-16 00:00:00.000', 2343.00 ,45.00  ,334.33)
go

--生成一个日期表,然后使用左连接

select m.* , n.extend_CHJ ,n.extend_HSDY ,n.extend_SBQX from
(
select 1 monthid  , '2011-09-01' day_time union all
select 2 monthid  , '2011-09-02' day_time union all
select 3 monthid  , '2011-09-03' day_time union all
select 4 monthid  , '2011-09-04' day_time union all
select 5 monthid  , '2011-09-05' day_time union all
select 6 monthid  , '2011-09-06' day_time union all
select 7 monthid  , '2011-09-07' day_time union all
select 8 monthid  , '2011-09-08' day_time union all
select 9 monthid  , '2011-09-09' day_time union all
select 10 monthid , '2011-09-10' day_time union all
select 11 monthid , '2011-09-11' day_time union all
select 12 monthid , '2011-09-12' day_time union all
select 13 monthid , '2011-09-13' day_time union all
select 14 monthid , '2011-09-14' day_time union all
select 15 monthid , '2011-09-15' day_time union all
select 16 monthid , '2011-09-16' day_time union all
select 17 monthid , '2011-09-17' day_time union all
select 18 monthid , '2011-09-18' day_time union all
select 19 monthid , '2011-09-19' day_time union all
select 20 monthid , '2011-09-20' day_time union all
select 21 monthid , '2011-09-21' day_time union all
select 22 monthid , '2011-09-22' day_time union all
select 23 monthid , '2011-09-23' day_time union all
select 24 monthid , '2011-09-24' day_time union all
select 25 monthid , '2011-09-25' day_time union all
select 26 monthid , '2011-09-26' day_time union all
select 27 monthid , '2011-09-27' day_time union all
select 28 monthid , '2011-09-28' day_time union all
select 29 monthid , '2011-09-29' day_time union all
select 30 monthid , '2011-09-30' day_time
) m left join
tb n
on m.day_time = n.day_time

drop table tb

/*
monthid     day_time   extend_CHJ           extend_HSDY          extend_SBQX          
----------- ---------- -------------------- -------------------- -------------------- 
1           2011-09-01 NULL                 NULL                 NULL
2           2011-09-02 NULL                 NULL                 NULL
3           2011-09-03 NULL                 NULL                 NULL
4           2011-09-04 NULL                 NULL                 NULL
5           2011-09-05 NULL                 NULL