如何使用 SQL2000 进行数据透视/逆透视

发布于 2024-10-03 05:14:10 字数 2557 浏览 4 评论 0原文

我正在查询一个表,其中本质上是一堆日期。

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 中执行此操作吗?

I'm querying a table which essentially a bunch of dates.

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

I am taking the date difference between two dates on each line. For example, the days to inspect is found like this:

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

This creates a row of different DateDiff calcuations which end up looking like this:

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

Please forgive the spacing problems.

So, each of these rows in the data set starts with a number which represents a product category in this case 01 and is followed by a series of day calculations.

I can average these values and give you an average for each category per product class. However, they want the day calculations along the left side of a table or matrix with the product classes along the top, exactly opposite of what I have here. If I pivot them in Excel, I get the following:

                          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

I hope this makes sense. Any idea how I can perform this in SSRS or t-sql?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-10-10 05:14:10

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

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

This will do it, assuming that you can load the data from your first query into a intermediate temporary table:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文