如何在数字范围和排序结果上进行Pivot查询?

时间:2013-05-24 03:27:14

标签: sql-server pivot sql-server-2012

我正在寻找一个MS SQL查询(用于SQL Reporting Services),以便给我以下结果:

JobCode   0-10     11-20     21-30    31-40    41-50
A           1        2         0        0        2
B           0        2         2        0        0
C           3        0         1        1        0

当前查询:

SELECT  RecordID
    , CAST(GETDATE() - JobStartDate AS Int) AS DaysSinceStart
    , Code
FROM    tblJobs

返回以下结果:

RecordID | DaysSinceStart | Code
158987    11    A
158968     3    A
158972     4    C
158973    16    B
158974    23    C
158975    13    B
158976    45    A
158977    32    C
158985     9    C
158981    25    B
158982    47    A
158983    18    A
158978     5    C
158979    27    B

我可以使用以下内容返回总体摘要,但它不允许我通过JobCode查看单个结果:

SELECT  Range AS [Day Range], COUNT(*) AS Jobs
FROM    (SELECT CASE WHEN DaysSinceStart BETWEEN 0 AND 9 THEN ' 0- 9'
                     WHEN DaysSinceStart BETWEEN 10 AND 19 THEN '10-19'
             WHEN DaysSinceStart BETWEEN 20 AND 29 THEN '20-29'
             WHEN DaysSinceStart BETWEEN 30 AND 39 THEN '30-39'
             WHEN DaysSinceStart BETWEEN 40 AND 49 THEN '40-49'
             WHEN DaysSinceStart BETWEEN 50 AND 59 THEN '50-59'
             WHEN DaysSinceStart BETWEEN 60 AND 69 THEN '60-69'
             WHEN DaysSinceStart BETWEEN 70 AND 79 THEN '70-79'
             WHEN DaysSinceStart BETWEEN 80 AND 89 THEN '80-89'
             WHEN DaysSinceStart BETWEEN 90 AND 99 THEN '90-99' 
             ELSE 'Over 100' END AS Range
        FROM    (SELECT DaysSinceStart
                 FROM tblJobs) AS derivedtbl_1) AS t
GROUP BY Range

有人可以帮助我们按照“准则”字段获取细分吗? 提前谢谢了。 附:我正在使用MS SQL Server 2012

3 个答案:

答案 0 :(得分:7)

试试这个:

select * from 
(SELECT  code,Range AS [Day Range], COUNT(*) AS Jobs
FROM    (SELECT code,CASE WHEN DaysSinceStart BETWEEN 0 AND 9 THEN '0- 9'
                     WHEN DaysSinceStart BETWEEN 10 AND 19 THEN '10-19'
             WHEN DaysSinceStart BETWEEN 20 AND 29 THEN '20-29'
             WHEN DaysSinceStart BETWEEN 30 AND 39 THEN '30-39'
             WHEN DaysSinceStart BETWEEN 40 AND 49 THEN '40-49'
             WHEN DaysSinceStart BETWEEN 50 AND 59 THEN '50-59'
             WHEN DaysSinceStart BETWEEN 60 AND 69 THEN '60-69'
             WHEN DaysSinceStart BETWEEN 70 AND 79 THEN '70-79'
             WHEN DaysSinceStart BETWEEN 80 AND 89 THEN '80-89'
             WHEN DaysSinceStart BETWEEN 90 AND 99 THEN '90-99' 
             ELSE 'Over 100' END AS Range
        FROM    (SELECT code,DaysSinceStart
                 FROM demo) AS derivedtbl_1) AS t
GROUP BY Range,code) q1
pivot
(max(jobs)
 for [Day Range] in ([0- 9],[10-19],[20-29],[30-39],[40-49],[50-59],
                     [60-69],[70-79],[80-89],[90-99],[Over 100])
)as pvt

SQL Fiddle

答案 1 :(得分:1)

试试这个 -

<强>查询:

SET NOCOUNT ON;

DECLARE @tblJobs TABLE
(
      RecordID INT
    , DaysSinceStart INT
    , Code CHAR(1)
)

INSERT INTO @tblJobs (RecordID, DaysSinceStart, Code)
VALUES 
    (158987,    11,    'A'), (158968,     3,    'A'),
    (158972,     4,    'C'), (158973,    16,    'B'),
    (158974,    23,    'C'), (158975,    13,    'B'),
    (158976,    45,    'A'), (158977,    32,    'C'),
    (158985,     9,    'C'), (158981,    25,    'B'),
    (158982,    47,    'A'), (158983,    18,    'A'),
    (158978,     5,    'C'), (158979,    27,    'B')

SELECT *
FROM (
    SELECT  
          t.Code
        , [Range] = 
            CASE 
                WHEN DaysSinceStart BETWEEN 0 AND 9   THEN '0-9'
                WHEN DaysSinceStart BETWEEN 10 AND 19 THEN '10-19'
                WHEN DaysSinceStart BETWEEN 20 AND 29 THEN '20-29'
                WHEN DaysSinceStart BETWEEN 30 AND 39 THEN '30-39'
                WHEN DaysSinceStart BETWEEN 40 AND 49 THEN '40-49'
                WHEN DaysSinceStart BETWEEN 50 AND 59 THEN '50-59'
                WHEN DaysSinceStart BETWEEN 60 AND 69 THEN '60-69'
                WHEN DaysSinceStart BETWEEN 70 AND 79 THEN '70-79'
                WHEN DaysSinceStart BETWEEN 80 AND 89 THEN '80-89'
                WHEN DaysSinceStart BETWEEN 90 AND 99 THEN '90-99' 
                ELSE 'Over 100' 
            END
    FROM @tblJobs t
) o
PIVOT
(
    COUNT(o.[Range])
    FOR [Range] IN (
        [0-9], [10-19], [20-29],
        [30-39], [40-49], [50-59],
        [60-69], [70-79], [80-89], 
        [90-99], [Over 100]
    )
) pt

<强>输出:

Code 0-9         10-19       20-29       30-39       40-49       50-59       60-69       70-79       80-89       90-99       Over 100
---- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
A    1           2           0           0           2           0           0           0           0           0           0
B    0           2           2           0           0           0           0           0           0           0           0
C    3           0           1           1           0           0           0           0           0           0           0

答案 2 :(得分:1)

创建派生表/ CTE /表变量/临时表,或者创建保存间隔的常规表。使用该表与datediffJobStartDate之间的getdate()加入以获得intervall。然后,您可以使用pivot计算每个Codeinterval的值。

with C(low, high, interval) as
(
  select 0,  10, '0-10'  union all
  select 11, 20, '11-20' union all
  select 21, 30, '21-30' union all
  select 31, 40, '31-40' union all
  select 41, 50, '41-50' union all
  select 51, 2147483647, 'over 50'
)
select Code, [0-10], [11-20], [21-30], [31-40], [41-50], [over 50]
from  (
      select J.Code,
             J.RecordID,
             C.interval
      from tblJobs as J
        inner join C
          on datediff(day, J.JobStartDate, getdate()) between C.low and C.high
      ) as T
pivot (
      count(T.RecordID) 
      for T.Interval in ([0-10], [11-20], [21-30], [31-40], [41-50], [over 50])
      ) as P

SQL Fiddle