今天填充一列直到今天6

时间:2015-04-07 14:25:36

标签: sql sql-server sql-server-2008-r2

如何使用Week填充Today列到Today -6之前?请参阅下面的小提琴,了解实际结果和预期结果:

SQL Fiddle

MS SQL Server 2008架构设置

CREATE TABLE Registry
    ([RegistryDate] datetime)
;

INSERT INTO Registry
    ([RegistryDate])

VALUES
    ('2015-04-07'),
    ('2015-04-07'),
    ('2015-04-06'),
    ('2015-04-02'),
    ('2015-04-01'),
    ('2015-04-01'),
    ('2015-03-31'),
    ('2015-03-31'),
    ('2015-03-30'),
    ('2015-03-29')
;

查询

SELECT DISTINCT
    TOP 7
    CASE WHEN CAST(RegistryDate AS DATE) = CAST(GETDATE() AS DATE) THEN 1 ELSE 0 END AS Indicator,
    CAST (RegistryDate AS DATE) AS LatestUpdates,
    CAST(GETDATE() AS DATE) AS Week
FROM
    Registry
ORDER BY
    1 DESC

结果

| Indicator | LatestUpdates |       Week |
|-----------|---------------|------------|
|         1 |    2015-04-07 | 2015-04-07 |
|         0 |    2015-03-29 | 2015-04-07 |
|         0 |    2015-03-30 | 2015-04-07 |
|         0 |    2015-03-31 | 2015-04-07 |
|         0 |    2015-04-01 | 2015-04-07 |
|         0 |    2015-04-02 | 2015-04-07 |
|         0 |    2015-04-06 | 2015-04-07 |

预期结果

| Indicator | LatestUpdates |       Week |
|-----------|---------------|------------|
|         1 |    2015-04-07 | 2015-04-07 | < Today
|         0 |    2015-03-29 | 2015-04-06 | < Today -1
|         0 |    2015-03-30 | 2015-04-05 | < Today -2
|         0 |    2015-03-31 | 2015-04-04 | < Today -3
|         0 |    2015-04-01 | 2015-04-03 | < Today -4
|         0 |    2015-04-02 | 2015-04-02 | < Today -5
|         0 |    2015-04-06 | 2015-04-01 | < Today -6

3 个答案:

答案 0 :(得分:1)

我想你想要一个这样的查询:

WITH  SevenRowsWeek as (
SELECT LatestUpdates, CAST((GETDATE() - ROW_NUMBER() OVER (ORDER BY Latestupdates DESC) + 1) as DATE) As [Week]
FROM   (SELECT DISTINCT TOP 7 
            CAST (RegistryDate AS DATE) AS LatestUpdates
        FROM
            #Registry
        ORDER BY
            CAST(RegistryDate AS DATE) DESC ) SevenRows
)
Select  CASE WHEN CAST(LatestUpdates AS DATE) = [Week] THEN 1 ELSE 0 END AS Indicator,
        LatestUpdates, [week]
FROM SevenRowsWeek
ORDER BY LatestUpdates DESC

答案 1 :(得分:0)

将您的查询包装在subquery中并在cte内使用,然后使用dateadd(day,-(row number-1),Week)

 with cte as
 (
 select q.Indicator ,q.LatestUpdates, q.Week,
   row_number() over(order by q.Indicator desc,q.LatestUpdates)rn 
   from (
      SELECT DISTINCT
          TOP 7
          CASE WHEN CAST(RegistryDate AS DATE) = CAST(GETDATE() AS DATE) THEN 1 ELSE 0 END AS Indicator,
          CAST (RegistryDate AS DATE) AS LatestUpdates,
          CAST(GETDATE() AS DATE) AS Week
      FROM
          Registry
      ORDER BY
          1 DESC
     )q
 )
  select Indicator,LatestUpdates,dateadd(day,-(rn-1),Week) Week
  from cte 
  order by 1 desc,2 asc

fiddle demo

答案 2 :(得分:0)

试试这个:

SELECT TOP 7
        CASE WHEN CAST(RegistryDate AS DATE) = CAST(GETDATE() AS DATE) THEN 1
             ELSE 0
        END AS Indicator ,
        CAST (RegistryDate AS DATE) AS LatestUpdates ,
        DATEADD(dd,
                1 - 1
                * ROW_NUMBER() OVER ( ORDER BY CASE WHEN CAST(RegistryDate AS DATE) = CAST(GETDATE() AS DATE)
                                                    THEN 1
                                                    ELSE 0
                                               END DESC, CAST(RegistryDate AS DATE) ),
                CAST(GETDATE() AS DATE)) AS WEEK
FROM    Registry
GROUP BY CAST(RegistryDate AS DATE)
ORDER BY CASE WHEN CAST(RegistryDate AS DATE) = CAST(GETDATE() AS DATE) THEN 1
              ELSE 0
         END DESC ,
        CAST(RegistryDate AS DATE)

输出:

Indicator   LatestUpdates   Week
1           2015-04-07      2015-04-07
0           2015-03-29      2015-04-06
0           2015-03-30      2015-04-05
0           2015-03-31      2015-04-04
0           2015-04-01      2015-04-03
0           2015-04-02      2015-04-02
0           2015-04-06      2015-04-01