计算日期并在SQL Server 2008的同时对它们进行比较

时间:2016-07-12 01:36:25

标签: sql-server-2008 date datetime

我有几个datetime列。我需要在SQL Server 2008中为每个时间戳计算同一列中有多少个日期时间戳小于每个日期时间戳。

例如:对于2016-05-01 14:24:000.00列中的DateTime1,我需要计算DateTime1列中的日期时间值小于DateTime2列的数量。

我还需要知道有多少个datetimestamps小于3列和DateTime1 DateTime2 DateTime3 ---------------------------------------------------------------------------- 2016-05-01 13:24:000.00 2016-05-01 15:24:000.00 2016-05-01 16:20:000.00 2016-05-01 13:30:000.00 2016-05-01 14:21:000.00 2016-05-01 15:10:000.00 2016-05-01 14:24:000.00 2016-05-01 17:21:000.00 2016-05-01 18:10:000.00 中同一记录(在同一行)的日期时间戳。

var parent = [String: [String]]()
let value = ["Tommy Turner", "Wolfgang Motart"] + ["Bobby Bushe"]
parent["parent"] = value
print(parent) // ["parent": ["Tommy Turner", "Wolfgang Motart", "Bobby Bushe"]]

3 个答案:

答案 0 :(得分:0)

如果我理解正确,您可以使用li span.show::after{ content: url("/sites/default/files/new5.gif"); padding-left: 5px; }

rank()

根据您希望如何处理绑定值,您可能实际需要select t.*, rank() over (order by datetime1) as dt1_rank, rank() over (order by datetime2) as dt2_rank, rank() over (order by datetime3) as dt3_rank from t ; 。此外,您可能希望从排名值中减去1。

答案 1 :(得分:0)

假设我有一个表名[TestTB]有3列DateTime1,DateTime2,DateTime3。 我说CountSmallerDateTime1为"有多少日期时间值小于DateTime1列" 我说CountSmallerDateTime2为"多少个datetimestamps小于DateTime2"列中相同记录(在同一行)的日期时间戳。 ,相似度,DateS3的CountSmallerDateTime3。 然后我查询了您的请求:

SELECT [DateTime1]
  ,[DateTime2]
  ,[DateTime3]
  ,(SELECT COUNT(1) 
    FROM [TestTB] Sub 
    WHERE TB.[DateTime1] >Sub.[DateTime1]) AS CountSmallerDateTime1
  ,(
    CASE WHEN TB.[DateTime2] > TB.[DateTime1]  AND TB.[DateTime2] > TB.[DateTime3] THEN 
        2
        WHEN ( (TB.[DateTime2] <= TB.[DateTime1]  AND TB.[DateTime2] > TB.[DateTime3])
            OR (TB.[DateTime2] > TB.[DateTime1]  AND TB.[DateTime2] <= TB.[DateTime3])) THEN
            1
    ELSE
        0
    END
  ) AS CountSmallerDateTime2,
  (
    CASE WHEN TB.[DateTime3] > TB.[DateTime1]  AND TB.[DateTime3] > TB.[DateTime2] THEN 
        2
        WHEN ( (TB.[DateTime3] <= TB.[DateTime1]  AND TB.[DateTime3] > TB.[DateTime2])
            OR (TB.[DateTime3] > TB.[DateTime1]  AND TB.[DateTime3] <= TB.[DateTime2])) THEN
            1
    ELSE
        0
    END
  ) AS CountSmallerDateTime3 FROM [TestTB] TB

答案 2 :(得分:0)

;WITH CTE(DATE1, DATE2, DATE3,RN)
AS
(
SELECT CONVERT(DATETIME , '2016-05-01 13:24:000.00'),   CONVERT(DATETIME,'2016-05-01 15:24:000.00'), CONVERT(DATETIME,'2016-05-01 16:20:000.00'),1 
UNION ALL
SELECT CONVERT(DATETIME , '2016-05-01 13:30:000.00'),   CONVERT(DATETIME,'2016-05-01 14:21:000.00'), CONVERT(DATETIME,'2016-05-01 15:10:000.00'),2 
UNION ALL
SELECT CONVERT(DATETIME , '2016-05-01 14:24:000.00'),   CONVERT(DATETIME,'2016-05-01 17:21:000.00'), CONVERT(DATETIME,'2016-05-01 18:10:000.00'),3
)
SELECT     RANK() OVER (ORDER BY DATE1) -1 AS SAME_COLUMN_DATE1 
         , RANK() OVER (ORDER BY DATE2) -1 AS SAME_COLUMN_DATE2
         , RANK() OVER (ORDER BY DATE3) -1 AS SAME_COLUMN_DATE3  

         , CASE WHEN RN=1 AND DATE1< DATE2 AND DATE1<DATE3 THEN 0
                WHEN RN=1 AND DATE1< DATE2 AND DATE1>DATE3 THEN 1
                WHEN RN=1 AND DATE1> DATE2 AND DATE1<DATE3 THEN 1
                ELSE 2
                SAME_ROW_1

         , CASE WHEN RN=2 AND DATE2< DATE1 AND DATE2<DATE3 THEN 0
                WHEN RN=2 AND DATE2< DATE1 AND DATE2>DATE3 THEN 1
                WHEN RN=2 AND DATE2> DATE1 AND DATE2<DATE3 THEN 1
                ELSE 2
                END SAME_ROW_2

         , CASE WHEN RN=3 AND DATE3< DATE1 AND DATE3<DATE2 THEN 0
                WHEN RN=3 AND DATE3< DATE1 AND DATE3>DATE2 THEN 1
                WHEN RN=3 AND DATE3> DATE1 AND DATE3<DATE2 THEN 1
                ELSE 2
                END SAME_ROW_3           
         FROM CTE ORDER BY RN