0

私はSQLクエリを持っています:

  SELECT
  TAT,
  SUM(CASE WHEN [Month Entered] = 1 THEN 1 ELSE 0 END) JAN,
   SUM(CASE WHEN [Month Entered] = 2 THEN 1 ELSE 0 END) FEB,
   SUM(CASE WHEN [Month Entered] = 3 THEN 1 ELSE 0 END) MAR,
   SUM(CASE WHEN [Month Entered] = 4 THEN 1 ELSE 0 END) APR,
   SUM(CASE WHEN [Month Entered] = 5 THEN 1 ELSE 0 END) MAY,
   SUM(CASE WHEN [Month Entered] = 6 THEN 1 ELSE 0 END) JUN,
   SUM(CASE WHEN [Month Entered] = 7 THEN 1 ELSE 0 END) JUL,
   SUM(CASE WHEN [Month Entered] = 8 THEN 1 ELSE 0 END) AUG,
   SUM(CASE WHEN [Month Entered] = 9 THEN 1 ELSE 0 END) SEP,
   SUM(CASE WHEN [Month Entered] = 10 THEN 1 ELSE 0 END) OCT,
   SUM(CASE WHEN [Month Entered] = 11 THEN 1 ELSE 0 END) NOV,
   SUM(CASE WHEN [Month Entered] = 12 THEN 1 ELSE 0 END) [DEC]

  FROM
  [SalesDWH].[dbo].[TurnAround]
  where DATEPART(yyyy,[datetime entered])=2010
  GROUP BY TAT
  ORDER BY 1;

返されるものは次のとおりです。

+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| TAT | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   0 |   0 |   0 |   0 |   3 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |
|   1 |   1 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |
|   7 |   0 |   0 |   0 |   0 |   1 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |
|  18 |  28 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |   0 |
|  19 |  39 |   0 |   0 |   0 |   0 |   1 |   0 |   0 |   0 |  53 |   0 |   0 |
|  20 | 106 |   0 |   0 |   0 |   0 |   0 |   2 |  17 |   0 |   1 |   2 |   0 |
|  21 | 113 |  12 |   0 |   0 |   0 |  22 |   1 |  81 | 137 |  27 |   1 | 110 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

出力を次のようにしたい:

+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| TAT | JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
|   0 |     |     |     |   3 |     |     |     |     |     |     |     |     |
|   1 |   1 |     |     |     |     |     |     |     |     |     |     |     |
|   2 |     |     |     |     |     |     |     |     |     |     |     |     |
|   3 |     |     |     |     |     |     |     |     |     |     |     |     |
|   4 |     |     |     |     |     |     |     |     |     |     |     |     |
|   5 |     |     |     |     |     |     |     |     |     |     |     |     |
|   6 |     |     |     |     |     |     |     |     |     |     |     |     |
|   7 |     |     |     |     |   1 |     |     |     |     |     |     |     |
|   8 |     |     |     |     |     |     |     |     |     |     |     |     |
|   9 |     |     |     |     |     |     |     |     |     |     |     |     |
|  10 |     |     |     |     |     |     |     |     |     |     |     |     |
|  11 |     |     |     |     |     |     |     |     |     |     |     |     |
|  12 |     |     |     |     |     |     |     |     |     |     |     |     |
|  13 |     |     |     |     |     |     |     |     |     |     |     |     |
|  14 |     |     |     |     |     |     |     |     |     |     |     |     |
|  15 |     |     |     |     |     |     |     |     |     |     |     |     |
|  16 |     |     |     |     |     |     |     |     |     |     |     |     |
|  17 |     |     |     |     |     |     |     |     |     |     |     |     |
|  18 |  28 |     |     |     |     |     |     |     |     |     |     |     |
|  19 |  39 |     |     |     |     |   1 |     |     |     |  53 |     |     |
|  20 | 106 |     |     |     |     |     |   2 |  17 |     |   1 |   2 |     |
|  21 | 113 |  12 |     |     |     |  22 |   1 |  81 | 137 |  27 |   1 | 110 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

0すべての が削除され、TAT列が 0,1,2,3,4,...... と連続していることに注意してください。

これら2つの条件を強制するにはどうすればよいですか?

0 から 191 までの TAT の値にこれが必要です。

値が 192 以上の場合、192 での TAT を 192 以上のすべての値の合計にしたい

4

2 に答える 2

1

まず、最初の列のすべての値を含むテーブルを作成し、データテーブルを結合する必要があります

次に、このクエリに加えて別のクエリを実行すると(つまり、これはサブクエリになります)、各月の列は次のようになります。

 case jan when 0 then '' else cast(jan as nvarchar) end jan
于 2012-05-22T23:19:48.583 に答える
1

新しい情報を編集する

;WITH d AS ( SELECT TAT = CASE WHEN TAT > 191 THEN 192 ELSE TAT END,
   SUM(CASE WHEN [Month Entered] = 1  THEN 1 ELSE 0 END) AS JAN,
   SUM(CASE WHEN [Month Entered] = 2  THEN 1 ELSE 0 END) AS FEB,
   SUM(CASE WHEN [Month Entered] = 3  THEN 1 ELSE 0 END) AS MAR,
   SUM(CASE WHEN [Month Entered] = 4  THEN 1 ELSE 0 END) AS APR,
   SUM(CASE WHEN [Month Entered] = 5  THEN 1 ELSE 0 END) AS MAY,
   SUM(CASE WHEN [Month Entered] = 6  THEN 1 ELSE 0 END) AS JUN,
   SUM(CASE WHEN [Month Entered] = 7  THEN 1 ELSE 0 END) AS JUL,
   SUM(CASE WHEN [Month Entered] = 8  THEN 1 ELSE 0 END) AS AUG,
   SUM(CASE WHEN [Month Entered] = 9  THEN 1 ELSE 0 END) AS SEP,
   SUM(CASE WHEN [Month Entered] = 10 THEN 1 ELSE 0 END) AS OCT,
   SUM(CASE WHEN [Month Entered] = 11 THEN 1 ELSE 0 END) AS NOV,
   SUM(CASE WHEN [Month Entered] = 12 THEN 1 ELSE 0 END) AS DEC
  FROM [SalesDWH].[dbo].[TurnAround]
  WHERE [datetime entered] >= '20100101'
    AND [datetime entered] <  '20110101'
  GROUP BY CASE WHEN TAT > 191 THEN 192 ELSE TAT END
),
n AS ( SELECT TOP (193) n = ROW_NUMBER() OVER (ORDER BY [object_id]) - 1
  FROM sys.all_columns ORDER BY [object_id]
)
SELECT TAT = n.n,
  JAN = COALESCE(RTRIM(NULLIF(JAN, 0)), ''),
  FEB = COALESCE(RTRIM(NULLIF(FEB, 0)), ''),
  MAR = COALESCE(RTRIM(NULLIF(MAR, 0)), ''),
  APR = COALESCE(RTRIM(NULLIF(APR, 0)), ''),
  MAY = COALESCE(RTRIM(NULLIF(MAY, 0)), ''),
  JUN = COALESCE(RTRIM(NULLIF(JUN, 0)), ''),
  JUL = COALESCE(RTRIM(NULLIF(JUL, 0)), ''),
  AUG = COALESCE(RTRIM(NULLIF(AUG, 0)), ''),
  SEP = COALESCE(RTRIM(NULLIF(SEP, 0)), ''),
  OCT = COALESCE(RTRIM(NULLIF(OCT, 0)), ''),
  NOV = COALESCE(RTRIM(NULLIF(NOV, 0)), ''),
  DEC = COALESCE(RTRIM(NULLIF(DEC, 0)), '')
FROM n LEFT OUTER JOIN d ON n.n = d.TAT 
ORDER BY n.n; -- do not use ordinal numbers for ORDER BY!
于 2012-05-22T23:12:33.243 に答える