如何使用 SQL2000 进行数据透视/逆透视
我正在查询一个表,其中本质上是一堆日期。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您可以将第一个查询中的数据加载到中间临时表中,就可以做到这一点:
This will do it, assuming that you can load the data from your first query into a intermediate temporary table: