日期:2014-05-17 浏览次数:20373 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (num nvarchar(2),date datetime,money int)
insert into [TB]
select 'A','2013-05-27',100 union all
select 'A','2013-05-24',200 union all
select 'A','2013-05-22',100 union all
select 'A','2013-05-20',1200 union all
select 'A','2013-05-19',300 union all
select 'A','2013-05-16',400 union all
select 'A','2013-05-12',200 union all
select 'A','2013-05-09',100 union all
select 'A','2013-05-06',500 union all
select 'A','2013-05-01',150
select * from [TB]
;WITH TT
AS(
SELECT DATEADD(dd,number,'2013-05-01') AS dd
FROM master..spt_values M
WHERE type ='P'),
T1 AS(
SELECT TT.dd,TB.date,TB.money
FROM TT
LEFT JOIN TB ON TT.dd = TB.date),
T2 AS(
SELECT TOP 100 PERCENT dd,date,money,convert(int,ROW_NUMBER() OVER(ORDER BY GETDATE())/6) AS nu
FROM T1
WHERE NOT EXISTS ( SELECT 1
FROM T1 A
WHERE NOT EXISTS ( SELECT 1