sql - 需要在 crosstab/aggregates 上为 average/min/max 澄清 "q.type"

sql - need clarification "q.type" on crosstab/aggregates for average/min/max

这是我之前问题的后续问题:

当我执行 parfait 的精彩回答时,我得到了 "Enter parameter value of q.Type"。

谁能解释一下为什么以及我该如何 substitute/specify 呢?

我不想一直手动输入值..

下面是他的代码答案:

交叉表2查询

TRANSFORM SUM(q.AggDetailQty)
SELECT q.ItemCode
FROM UnionAggQuery q
GROUP BY q.ItemCode
PIVOT **q.Type** IN ('AVG 16 WEEKS', 'MIN 9 WEEKS', 'MAX 15 WEEKS');

UnionAggQ 查询(加入两个交叉表)

下一个交叉表需要另一个查询作为源,特别是 ItemCode 与分类指标列的聚合联合查询:

SELECT Detail.ItemCode,
       'AVG 16 WEEKS' AS Metric,
       AVG(Detail.Quantity) AS AggDetailQty
FROM Detail INNER JOIN Header ON Detail.SalesOrderNo = Header.SalesOrderNo
WHERE Header.OrderDate >= DATEADD("ww",-16, Date())
GROUP BY Detail.ItemCode

UNION ALL

SELECT Detail.ItemCode,
       'MIN 9 WEEKS' AS Metric,
       MIN(Detail.Quantity) AS AggDetailQty 
FROM Detail INNER JOIN Header ON Detail.SalesOrderNo = Header.SalesOrderNo
WHERE Header.OrderDate >= DATEADD("ww",-9, Date())
GROUP BY Detail.ItemCode

UNION ALL

SELECT Detail.ItemCode,
       'MAX 15 WEEKS' AS Metric,
       MAX(Detail.Quantity) AS AggDetailQty
FROM Detail INNER JOIN Header ON Detail.SalesOrderNo = Header.SalesOrderNo
WHERE Header.OrderDate >= DATEADD("ww",-15, Date())
GROUP BY Detail.ItemCode

最终查询

SELECT t1.*, t2.*
FROM CrossTab1 t1 
INNER JOIN CrossTab2 t2
ON t1.ItemCode = t2.ItemCode

试试这个:

TRANSFORM SUM(q.AggDetailQty)
SELECT q.ItemCode
FROM UnionAggQuery q
GROUP BY q.ItemCode
PIVOT q.Metric IN ('AVG 16 WEEKS', 'MIN 9 WEEKS', 'MAX 15 WEEKS');