12

标题并没有完全表达我的意思,这可能是重复的。

这是长版本:给定客人的姓名、他们的注册日期和他们的结帐日期,我如何为他们作为客人的每一天生成一行?

例如:Bob 于 7/14 入住并于 7/17 离开。我想

('Bob', 7/14), ('Bob', 7/15), ('Bob', 7/16), ('Bob', 7/17) 

作为我的结果。

谢谢!

4

5 回答 5

31

我会争辩说,对于这个特定目的,下面的查询与使用专用查找表一样有效。

DECLARE @start DATE, @end DATE;
SELECT @start = '20110714', @end = '20110717';

;WITH n AS 
(
  SELECT TOP (DATEDIFF(DAY, @start, @end) + 1) 
    n = ROW_NUMBER() OVER (ORDER BY [object_id])
  FROM sys.all_objects
)
SELECT 'Bob', DATEADD(DAY, n-1, @start)
FROM n;

结果:

Bob     2011-07-14
Bob     2011-07-15
Bob     2011-07-16
Bob     2011-07-17

大概你需要这个作为一个集合,而不是单个成员,所以这里有一种适应这种技术的方法:

DECLARE @t TABLE
(
    Member NVARCHAR(32), 
    RegistrationDate DATE, 
    CheckoutDate DATE
);

INSERT @t SELECT N'Bob', '20110714', '20110717'
UNION ALL SELECT N'Sam', '20110712', '20110715'
UNION ALL SELECT N'Jim', '20110716', '20110719';

;WITH [range](d,s) AS 
(
  SELECT DATEDIFF(DAY, MIN(RegistrationDate), MAX(CheckoutDate))+1,
    MIN(RegistrationDate)
    FROM @t -- WHERE ?
),
n(d) AS
(
  SELECT DATEADD(DAY, n-1, (SELECT MIN(s) FROM [range]))
  FROM (SELECT ROW_NUMBER() OVER (ORDER BY [object_id])
  FROM sys.all_objects) AS s(n)
  WHERE n <= (SELECT MAX(d) FROM [range])
)
SELECT t.Member, n.d
FROM n CROSS JOIN @t AS t
WHERE n.d BETWEEN t.RegistrationDate AND t.CheckoutDate;
----------^^^^^^^ not many cases where I'd advocate between!

结果:

Member    d
--------  ----------
Bob       2011-07-14
Bob       2011-07-15
Bob       2011-07-16
Bob       2011-07-17
Sam       2011-07-12
Sam       2011-07-13
Sam       2011-07-14
Sam       2011-07-15
Jim       2011-07-16
Jim       2011-07-17
Jim       2011-07-18
Jim       2011-07-19

正如@Dems 指出的那样,这可以简化为:

;WITH natural AS 
(
  SELECT ROW_NUMBER() OVER (ORDER BY [object_id]) - 1 AS val 
  FROM sys.all_objects
) 
SELECT t.Member, d = DATEADD(DAY, natural.val, t.RegistrationDate) 
  FROM @t AS t INNER JOIN natural 
  ON natural.val <= DATEDIFF(DAY, t.RegistrationDate, t.CheckoutDate);
于 2012-06-21T15:35:08.143 回答
8

我通常通过在某个表上使用 row_number() 的技巧来做到这一点。所以:

select t.name, dateadd(d, seq.seqnum, t.start_date)
from t left outer join
     (select row_number() over (order by (select NULL)) as seqnum
      from t
     ) seq
     on seqnum <= datediff(d, t.start_date, t.end_date)

seq 的计算非常快,因为不需要计算或排序。但是,您需要确保表对于所有时间跨度都足够大。

于 2012-06-21T15:37:10.927 回答
2

如果您有一个“Tally”或“Numbers”表,那么对于这样的事情来说,生活会变得非常简单。

 SELECT Member, DatePresent = DATEADD(dd,t.N,RegistrationDate)
   FROM @t 
  CROSS JOIN dbo.Tally t
  WHERE t.N BETWEEN 0 AND DATEDIFF(dd,RegistrationDate,CheckoutDate)
;

这是构建“Tally”表的方法。

--===================================================================
--      Create a Tally table from 0 to 11000
--===================================================================
--===== Create and populate the Tally table on the fly.
 SELECT TOP 11001
        IDENTITY(INT,0,1) AS N
   INTO dbo.Tally
   FROM Master.sys.ALL_Columns ac1
  CROSS JOIN Master.sys.ALL_Columns ac2
;
--===== Add a CLUSTERED Primary Key to maximize performance
  ALTER TABLE dbo.Tally
    ADD CONSTRAINT PK_Tally_N 
        PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100
;
--===== Allow the general public to use it
  GRANT SELECT ON dbo.Tally TO PUBLIC
;
GO

有关什么是 SQL 中的“Tally”表以及如何使用它来替换 While 循环和重要的递归 CTE 的“隐藏 RBAR”的更多信息,请参阅以下文章。

http://www.sqlservercentral.com/articles/T-SQL/62867/

于 2012-06-25T04:54:16.423 回答
0

这可能对你有用:

with mycte as
 (
     select cast('2000-01-01' as datetime) DateValue, 'Bob' as Name
     union all
     select DateValue + 1 ,'Bob' as Name
     from    mycte   
     where   DateValue + 1 < '2000-12-31'
 )
 select *
from    mycte
OPTION (MAXRECURSION 0)
于 2012-06-21T15:30:42.390 回答
-5

我会创建一个触发器来创建额外的记录并在结帐时运行它。或者,您可以每天午夜做同样的工作(如果您需要数据库中的最新信息)。

于 2012-06-21T15:31:47.820 回答