如何使用SQL2000转动/取消某些东西

时间:2010-11-17 21:41:33

标签: sql-server tsql sql-server-2000 ssrs-2008

我正在查询一张基本上是一堆日期的表格。

FRECDCUST              DTRECDCUST              DTINSPECTED             DTRECDCUSTPO           DTADDEDSO                 DTSHIPPEDSUP                  DTQUOTEDCUST
1/1/1900 12:00:00 AM    7/27/2010 12:00:00 AM   7/30/2010 12:00:00 AM   7/26/2010 12:00:00 AM   8/6/2010 12:00:00 AM    1/1/1900 12:00:00 AM    8/6/2010 12:00:00 AM
1/1/1900 12:00:00 AM    7/27/2010 12:00:00 AM   7/30/2010 12:00:00 AM   7/26/2010 12:00:00 AM   8/6/2010 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM
1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM
1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM
1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM    1/1/1900 12:00:00 AM
1/1/1900 12:00:00 AM    3/12/2010 12:00:00 AM   3/15/2010 12:00:00 AM   3/11/2010 12:00:00 AM   3/16/2010 12:00:00 AM   3/24/2010 12:00:00 AM   4/13/2010 12:00:00 AM
1/1/1900 12:00:00 AM    11/6/2009 12:00:00 AM   11/6/2009 12:00:00 AM   11/3/2009 12:00:00 AM   11/9/2009 12:00:00 AM   11/20/2009 12:00:00 AM  12/7/2009 12:00:00 AM

我正在考虑每行上两个日期之间的日期差异。例如,检查的日期如下:

SELECT (CASE
          WHEN year(SYC_ext.DTRECDCUST) <> 1900
               AND year(SYC_ext.DTINSPECTED) <> 1900
          THEN
            DATEDIFF(dd, SYC_ext.DTRECDCUST, SYC_ext.DTINSPECTED)
          ELSE
            NULL
        END)
         AS [AverageDaystoInspect]
Blah blah...

这会创建一行不同的DateDiff计算,最终看起来像这样:

Product Class   Average Days to Inspect Average Days to Process SO  Average Days to Ship to Affiliate   Average Days to Quote   Average Days to Receive Ammended PO
01  2   4   8   27  21
01  2   4   8   27  21
01  3   5   2   88  8
01  3   5   2   88  8
01  1   8   11  72  1
01  3   2   7   27  0
01  5   2   7   27  0

请原谅间距问题。

因此,数据集中的每一行都以一个数字开头,该数字表示此情况下的产品类别01,然后是一系列日计算。

我可以平均这些值,并为每个产品类别的每个类别提供平均值。但是,他们希望在表格或矩阵的左侧进行日计算,产品类别位于顶部,与此处的完全相反。如果我在Excel中转​​动它们,我会得到以下结果:

                          01    05     10
Avg Days to Inspect       5      3      5
Avg Days to Process PO    4      5      5
Avg Days to Ship          20     18     19

我希望这是有道理的。知道如何在SSRS或t-sql中执行此操作吗?

1 个答案:

答案 0 :(得分:0)

这样做,假设您可以将第一个查询中的数据加载到中间临时表中:

DECLARE @t TABLE
    (
      ProductClass INT
    , InspectDays INT
    , ProcessDays INT
    , ShipDays INT
    )

INSERT  INTO @t
        SELECT  1
              , 2
              , 4
              , 8
        UNION ALL
        SELECT  1
              , 4
              , 5
              , 2
        UNION ALL
        SELECT  5
              , 2
              , 4
              , 8
        UNION ALL
        SELECT  10
              , 3
              , 5
              , 2

SELECT  'Avg Inspect'
      , [01] = AVG(CASE WHEN ProductClass = 1 THEN InspectDays
                   END)
      , [05] = AVG(CASE WHEN ProductClass = 5 THEN InspectDays
                   END)
      , [10] = AVG(CASE WHEN ProductClass = 10 THEN InspectDays
                   END)
FROM    @t
UNION ALL
SELECT  'Avg Process'
      , [01] = AVG(CASE WHEN ProductClass = 1 THEN ProcessDays
                   END)
      , [05] = AVG(CASE WHEN ProductClass = 5 THEN ProcessDays
                   END)
      , [10] = AVG(CASE WHEN ProductClass = 10 THEN ProcessDays
                   END)
FROM    @t