在两个日期之间每天输出一行

时间:2016-04-20 04:22:40

标签: sql-server tsql

想象一下,一个酒店,住户进出不同的房间。此信息作为占用表中的单行存储在数据库中。

例如,乘员1000在3月3日到达,并在3/5时检查到床100.他们在4/2再次回来并且留在床101.乘员1001在床上100住4晚/ 1。

我需要在乘客和床的入住和搬出日期之间每天生成一行。

以下是数据目前的情况:

+------------+-------+------------+-------------+
| OccupantID | BedID | MoveInDate | MoveOutDate |
+------------+-------+------------+-------------+
|       1000 |   100 | 3/3/2016   | 3/5/2016    |
|       1000 |   101 | 4/2/2016   | 4/3/2016    |
|       1001 |   100 | 4/1/2016   | 4/1/2016    |
+------------+-------+------------+-------------+

这是所需输出的样子。

+------------+-------+----------+
| OccupantID | BedID |   Date   |
+------------+-------+----------+
|       1000 |   100 | 3/3/2016 |
|       1000 |   100 | 3/4/2016 |
|       1000 |   100 | 3/5/2016 |
|       1000 |   101 | 4/2/2016 |
|       1000 |   101 | 4/3/2016 |
|       1001 |   100 | 4/1/2016 |
+------------+-------+----------+

3 个答案:

答案 0 :(得分:1)

您可以借助Tally Table

来完成此操作
WITH E1(N) AS( -- 10 ^ 1 = 10 rows
    SELECT 1 FROM(VALUES (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))t(N)
),
E2(N) AS(SELECT 1 FROM E1 a CROSS JOIN E1 b), -- 10 ^ 2 = 100 rows
E4(N) AS(SELECT 1 FROM E2 a CROSS JOIN E2 b), -- 10 ^ 4 = 10,000 rows
CteTally(N) AS(
    SELECT TOP(SELECT MAX(DATEDIFF(DAY, MoveInDate, MoveOutDate)) + 1 FROM tbl) 
        ROW_NUMBER() OVER(ORDER BY(SELECT NULL))
    FROM E4
)
SELECT
    t.OccupantID,
    t.BedID,
    Date = DATEADD(DAY, ct.N-1, t.MoveInDate)
FROM tbl t
CROSS JOIN CteTally ct
WHERE
    DATEADD(DAY, ct.N-1, t.MoveInDate) <= t.MoveOutDate
ORDER BY
    t.OccupantID, t.BedID, Date

ONLINE DEMO

答案 1 :(得分:0)

您可以使用以下TSQL执行此操作:

SQL demo here

--create table occupancy
--(OccupantID int,BedID int,MoveInDate date,MoveOutDate  date);
--insert into occupancy values
--(1000,100,'3/3/2016','3/5/2016'),
--(1000,101,'4/2/2016','4/3/2016'),
--(1001,100,'4/1/2016','4/1/2016');

create table #t(OccupantID int,BedID int,OccupiedDate date);
create table #temp (id int,OccupantID int,BedID int,MoveInDate date,MoveOutDate  date);
insert into #temp
select 
row_number() over( order by occupantId,moveinDate desc) id, occupantid,bedid,moveindate, moveoutdate from occupancy
Declare @c int
Declare @startdate date, @enddate date
Select @c=MAX(id) from #temp
WHILE(@c>0)
BEGIN
SELECT @startdate=MoveinDate, @enddate=Moveoutdate from #temp where id=@c
INSERT INTO #t
SELECT occupantid,bedid,@startdate from #temp where id=@c
    WHILE (@startdate<@enddate)
    BEGIN
        SET @startdate=DATEADD(d,1,@startdate)
        INSERT INTO #t
        SELECT occupantid,bedid,@startdate from #temp where id=@c
    END
SET @c=@c-1
END

select * from #t
drop table #temp,#t

答案 2 :(得分:0)

试试这个,不需要任何分区功能。

declare @t table (OccupantID int,BedID int, MoveInDate date, MoveOutDate date)
insert into @t values
(1000,100,'3/3/2016','3/5/2016')
,(1000,101,'4/2/2016','4/3/2016')
,(1001,100,'4/1/2016','4/1/2016')

;WITH CTE
AS (
    SELECT OccupantID
        ,BedID
        ,MoveInDate [StayDate]
    FROM @t

    UNION ALL

    SELECT a.OccupantID
        ,a.BedID
        ,dateadd(day, 1, b.StayDate)
    FROM @t A
    INNER JOIN CTE B ON a.OccupantID = b.OccupantID
        AND a.BedID = b.BedID
    WHERE b.StayDate < a.MoveOutDate
    )
SELECT *
FROM CTE
ORDER BY OccupantID
    ,bedid