如何找到日期的日期

时间:2012-05-23 12:04:22

标签: sql sql-server sql-server-2000

使用SQL Server 2000

表1

ID Date Value1 Value2

001 01/01/2012 100 0
001 02/01/2012 200 200
...
...
001 31/01/2012 250 0
002 01/01/2012 050 100
002 02/01/2012 100 0
...
002 31/01/2012 075
....

我希望按日期(日期列)按ID

显示值(value1列)

输出如

ID 01/01/2012 02/01/2012 ... 31/01/2012

001  100 200 .... 250
002  050 100 .... 075
.....

查询

DECLARE @loop int,@date Date,@sql nvarchar(4000)
DECLARE @TempTable TABLE
(
  [Date] DATE
)
INSERT INTO @TempTable SELECT DISTINCT [Date] FROM table1 ORDER BY [Date]
  SET @sql = ''
  SET @loop = 1
  WHILE (@loop<=31)
    BEGIN
        IF EXISTS(SELECT * FROM @TempTable WHERE DAY([Date])=@loop) 
        BEGIN
            SET @date = (SELECT [Date] FROM @TempTable WHERE DAY([Date])=@loop)
            SET @sql = @sql+ ',MAX(CASE CONVERT(nvarchar(10),[Date],103) WHEN '''+CONVERT(nvarchar(10),@date,103)+''' THEN [Value1] END) AS [DATE'+CONVERT(nvarchar(2),@loop)+']'
         END
         ELSE
            SET @sql = @sql+ ', NULL AS [DATE'+CONVERT(nvarchar(2),@loop)+']'
         SET @loop = @loop+1
    END 

EXEC('SELECT ID'+@sql+' FROM table1 GROUP BY ID')

上面的查询是有效的,从上面的查询我想找到星期日,如果它是星期日,那么date列应该显示value2列的值,否则value1列 应该显示

如何做到这一点。

需要查询帮助

2 个答案:

答案 0 :(得分:1)

DATEPART function为您提供一周中的整数日。星期日 - 星期六变成1 - 7。

尝试通过此更改行

SET @sql = @sql+ ',MAX(CASE CONVERT(nvarchar(10),[Date],103) WHEN '''+CONVERT(nvarchar(10),@date,103)+''' THEN CASE DATEPART(weekday,[Date])WHEN 1 THEN [Value1] ELSE [Value2] END END) AS [DATE'+CONVERT(nvarchar(2),@loop)+']'

每个ID的总计算

SELECT ID,
SUM(CASE WHEN DATEPART(weekday,[Date]) <> 1 THEN [Value1] END) AS Value1,
SUM(CASE DATEPART(weekday,[Date]) WHEN 1 THEN [Value2] END) AS Value2,
FROM table1 GROUP BY ID

答案 1 :(得分:0)

可能会发现您不需要迭代,也不需要动态脚本。请考虑以下事项:

SELECT
  ID,
  Day1  = MAX(CASE D WHEN  1 THEN V END),
  Day2  = MAX(CASE D WHEN  2 THEN V END),
  Day3  = MAX(CASE D WHEN  3 THEN V END),
  Day4  = MAX(CASE D WHEN  4 THEN V END),
  Day5  = MAX(CASE D WHEN  5 THEN V END),
  Day6  = MAX(CASE D WHEN  6 THEN V END),
  Day7  = MAX(CASE D WHEN  7 THEN V END),
  Day8  = MAX(CASE D WHEN  8 THEN V END),
  Day9  = MAX(CASE D WHEN  9 THEN V END),
  Day10 = MAX(CASE D WHEN 10 THEN V END),
  Day11 = MAX(CASE D WHEN 11 THEN V END),
  Day12 = MAX(CASE D WHEN 12 THEN V END),
  Day13 = MAX(CASE D WHEN 13 THEN V END),
  Day14 = MAX(CASE D WHEN 14 THEN V END),
  Day15 = MAX(CASE D WHEN 15 THEN V END),
  Day16 = MAX(CASE D WHEN 16 THEN V END),
  Day17 = MAX(CASE D WHEN 17 THEN V END),
  Day18 = MAX(CASE D WHEN 18 THEN V END),
  Day19 = MAX(CASE D WHEN 19 THEN V END),
  Day20 = MAX(CASE D WHEN 20 THEN V END),
  Day21 = MAX(CASE D WHEN 21 THEN V END),
  Day22 = MAX(CASE D WHEN 22 THEN V END),
  Day23 = MAX(CASE D WHEN 23 THEN V END),
  Day24 = MAX(CASE D WHEN 24 THEN V END),
  Day25 = MAX(CASE D WHEN 25 THEN V END),
  Day26 = MAX(CASE D WHEN 26 THEN V END),
  Day27 = MAX(CASE D WHEN 27 THEN V END),
  Day28 = MAX(CASE D WHEN 28 THEN V END),
  Day29 = MAX(CASE D WHEN 29 THEN V END),
  Day30 = MAX(CASE D WHEN 30 THEN V END),
  Day31 = MAX(CASE D WHEN 31 THEN V END)
FROM (
  SELECT
    ID,
    V = CASE DATENAME(WEEKDAY, Date) WHEN 'Sunday' THEN Value2 ELSE Value1 END,
    D = DAY(Date)
  FROM Table1
  WHERE … /* perhaps, a condition to retrieve a specific month is due here */
) s
GROUP BY ID
;