2

SQLServer2000の使用

表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
....

値(value1列)を日付ごと(日付列)で表示したいgro​​up by id

のような出力

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')

上記のクエリは機能しています。上記のクエリから日曜日を検索したいのですが、日曜日の場合は日付列にvalue2列の値が表示され、そうでない場合はvalue1列に表示されます。

これを行う方法。

クエリのヘルプが必要

4

2 に答える 2

1

DATEPART 関数は整数の曜日を返します。日曜日~土曜日は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
于 2012-05-23T12:17:55.520 に答える
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
;
于 2012-05-24T07:44:43.430 に答える