选择两个日期之间的所有日期

时间:2014-11-10 10:30:57

标签: sql sql-server-2008

我的表只有一条记录,如下所示

Date1            Date2           Amount       CountDays
2014-01-01       2014-01-4       1000           4

field" CountDays"将由触发器计算。我想写一个返回结果的查询:

Date             Amount
2014-01-01       250
2014-01-02       250
2014-01-03       250
2014-01-04       250

对我来说真的很有必要请帮助我。

4 个答案:

答案 0 :(得分:3)

这应该有效:

WITH Nums AS(
    SELECT DISTINCT Value = number 
    FROM master..[spt_values] 
)
SELECT Date = DATEADD(d, n.Value - 1, t.Date1),
       Amount = t.Amount / t.CountDays * 1.0
FROM Nums n CROSS JOIN TableName t
WHERE n.Value BETWEEN 1 AND t.CountDays

Demo

请注意,此方法仅适用于2,164。另一种方法是使用数字表。读:

http://sqlperformance.com/2013/01/t-sql-queries/generate-a-set-1

答案 1 :(得分:0)

最好的选择是使用数字(计数)表,但是如果你没有一个或者设置了一个选项,你可以使用递归公用表表达式动态生成一个序列,如下所示:

-- test data
declare @t table (Date1 date, Date2 date, Amount int, CountDays int);
insert @t values ('2014-01-01','2014-01-04',1000,4);

-- query
;with cte (date1, date2, amount) as (
    select date1, date2, Amount * 1.0 / CountDays as amount
    from @t -- your table
    union all
    select DATEADD(day, 1, date1) date1, date2, amount
    from cte
    where date1 < date2
)

select date1, amount from cte

结果:

date1      amount
---------- -----------
2014-01-01 250
2014-01-02 250
2014-01-03 250
2014-01-04 250

答案 2 :(得分:0)

请尝试以下方法:

DECLARE @FromDate DATETIME,    
@ToDate DATETIME,
@DateDiff   INT,
@Amount     INT,
@Total      INT

--SET @FromDate = '2014-01-01'
--SET @ToDate = '2014-01-20'
--SET @Amount = 1000

SELECT TOP(1) @FromDate = Date1 FROM TABLENAME
SELECT TOP(1) @ToDate = Date2 FROM TABLENAME
SELECT TOP(1) @Amount = Amount FROM TABLENAME

SET @DateDiff = DATEDIFF(DAY, @FromDate, @ToDate)+1
SET @Total = @Amount / @DateDiff


BEGIN

DECLARE @TOTALCount INT
SET @FromDate = DATEADD(DAY,-1,@FromDate)
Select  @TOTALCount= DATEDIFF(DD,@FromDate,@ToDate);


WITH d AS 
        (
          SELECT top (@TOTALCount) AllDays = DATEADD(DAY, ROW_NUMBER() 
            OVER (ORDER BY object_id), REPLACE(@FromDate,'-',''))
          FROM sys.all_objects
        )
    SELECT AllDays, @Total AS [Amount] From d

RETURN 
END
GO

我已使用硬编码值测试脚本以确保其运行正确。 请使用正确的表名更新表名。

希望这有帮助。

答案 3 :(得分:0)

创建测试数据:

create table yourtable (Id int, Date1 datetime, Date2 datetime, Amount int, CountDays decimal);

insert into yourtable values (1, '2014-01-01','2014-01-04', 1000, 4);
insert into yourtable values (2, '2014-01-13','2014-01-19', 1200, 6);

和sql查询(带递归的公用表表达式):

WITH cte (id, date1, date2, amount) AS (
    SELECT id, date1, date2, (Amount / CountDays) as amount
    FROM yourtable
    UNION ALL
    select id, DATEADD(day, 1, date1) date1, date2, amount
    from cte
    where date1 < date2
)

最后接收数据:

select * from cte
order by id

输出:

ID  DATE1                           DATE2                           AMOUNT
1   January, 01 2014 00:00:00+0000  January, 04 2014 00:00:00+0000  250
1   January, 02 2014 00:00:00+0000  January, 04 2014 00:00:00+0000  250
1   January, 03 2014 00:00:00+0000  January, 04 2014 00:00:00+0000  250
1   January, 04 2014 00:00:00+0000  January, 04 2014 00:00:00+0000  250
2   January, 13 2014 00:00:00+0000  January, 19 2014 00:00:00+0000  200
2   January, 14 2014 00:00:00+0000  January, 19 2014 00:00:00+0000  200
2   January, 15 2014 00:00:00+0000  January, 19 2014 00:00:00+0000  200
2   January, 16 2014 00:00:00+0000  January, 19 2014 00:00:00+0000  200
2   January, 17 2014 00:00:00+0000  January, 19 2014 00:00:00+0000  200
2   January, 18 2014 00:00:00+0000  January, 19 2014 00:00:00+0000  200
2   January, 19 2014 00:00:00+0000  January, 19 2014 00:00:00+0000  200

Fiddle Demo