我正在使用两个不同的查询来获取数据。在这里,我已经使用union
加入了。
SELECT
IPOD.StockCode
,SUM(IPOD.OrderQty) / 12 AS newAverage
FROM
InvPurchaseOrderHeader AS IPOH
INNER JOIN
InvPurchaseOrderDetail AS IPOD
ON
IPOH.InvPurchaseOrderHeaderID = IPOD.InvPurchaseOrderHeaderID
WHERE
IPOH.DocumentStatus = 1
AND IPOH.DocumentDate >= (DATEADD(MONTH, -12, GETDATE()))
GROUP BY
IPOD.StockCode
UNION
SELECT
IPOD.StockCode
,SUM(IPOD.OrderQty) / 6 AS newAverage
FROM
InvPurchaseOrderHeader AS IPOH
INNER JOIN
InvPurchaseOrderDetail AS IPOD
ON
IPOH.InvPurchaseOrderHeaderID = IPOD.InvPurchaseOrderHeaderID
WHERE
IPOH.DocumentStatus = 1
AND IPOH.DocumentDate >= (DATEADD(MONTH, -6, GETDATE()))
GROUP BY
IPOD.StockCode;
但是上面的查询需要总结类似的记录 目前它给出这样的结果。
4846000001-M 20.000000
4846000001-M 40.000000
4846000001-S 10.000000
4846000001-S 20.000000
但是我需要这样的数据集
4846000001-M 60.000000
4846000001-S 30.000000
此外,我需要将每个结果行值除以3
最终数据集应如下所示。
4846000001-M 20.000000
4846000001-S 10.000000
我该如何实现?
答案 0 :(得分:2)
您不需要两个查询。您可以将单独的列获取为:
select IPOD.StockCode,
sum(case when IPOH.DocumentDate >= dateadd(month, -12, getdate()) then IPOD.OrderQty end)/12 as newAverage_12,
sum(case when IPOH.DocumentDate >= dateadd(month, -6, getdate()) then IPOD.OrderQty end)/6 as newAverage_6
from InvPurchaseOrderHeader IPOH inner join
InvPurchaseOrderDetail IPOD
on IPOH.InvPurchaseOrderHeaderID = IPOD.InvPurchaseOrderHeaderID
where IPOH.DocumentStatus = 1
group by IPOD.StockCode;
您可以轻松地将它们加在一起并除以3。
编辑:
因此,您的特定问题的答案是:
select IPOD.StockCode,
(sum(case when IPOH.DocumentDate >= dateadd(month, -12, getdate()) then IPOD.OrderQty end)/12 +
sum(case when IPOH.DocumentDate >= dateadd(month, -6, getdate()) then IPOD.OrderQty end)/6
) / 3
from InvPurchaseOrderHeader IPOH inner join
InvPurchaseOrderDetail IPOD
on IPOH.InvPurchaseOrderHeaderID = IPOD.InvPurchaseOrderHeaderID
where IPOH.DocumentStatus = 1
group by IPOD.StockCode;
答案 1 :(得分:0)
您可以通过子查询来使用sum()
函数,但条件汇总最好由@gordon先生回答
select t.StockCode,sum(newAverage)/3 from
(
select IPOD.StockCode, SUM(IPOD.OrderQty)/12 as newAverage from
InvPurchaseOrderHeader IPOH
INNER JOIN InvPurchaseOrderDetail IPOD on IPOH.InvPurchaseOrderHeaderID = IPOD.InvPurchaseOrderHeaderID
where IPOH.DocumentStatus = 1 AND IPOH.DocumentDate >= (DATEADD(MONTH, -12, GETDATE()))
GROUP BY IPOD.StockCode
union
select IPOD.StockCode, SUM(IPOD.OrderQty)/6 as newAverage from
InvPurchaseOrderHeader IPOH
INNER JOIN InvPurchaseOrderDetail IPOD on IPOH.InvPurchaseOrderHeaderID = IPOD.InvPurchaseOrderHeaderID
where IPOH.DocumentStatus = 1 AND IPOH.DocumentDate >= (DATEADD(MONTH, -6, GETDATE()))
GROUP BY IPOD.StockCode)
) as t group by t.StockCode
答案 2 :(得分:0)
我们也可以在此使用 avg()函数
select t.StockCode,avg(newAverage) from
(
select IPOD.StockCode as stockcode, SUM(IPOD.OrderQty)/12 as newAverage from
InvPurchaseOrderHeader IPOH
INNER JOIN InvPurchaseOrderDetail IPOD on IPOH.InvPurchaseOrderHeaderID = IPOD.InvPurchaseOrderHeaderID
where IPOH.DocumentStatus = 1 AND IPOH.DocumentDate >= (DATEADD(MONTH, -12, GETDATE()))
GROUP BY IPOD.StockCode
union
select IPOD.StockCode as stockcode, SUM(IPOD.OrderQty)/6 as newAverage from
InvPurchaseOrderHeader IPOH
INNER JOIN InvPurchaseOrderDetail IPOD on IPOH.InvPurchaseOrderHeaderID = IPOD.InvPurchaseOrderHeaderID
where IPOH.DocumentStatus = 1 AND IPOH.DocumentDate >= (DATEADD(MONTH, -6, GETDATE()))
GROUP BY IPOD.StockCode)
) as t group by t.StockCode
您还可以将和条款一起使用
with data as
(
select IPOD.StockCode as stockcode, SUM(IPOD.OrderQty)/12 as newAverage from
InvPurchaseOrderHeader IPOH
INNER JOIN InvPurchaseOrderDetail IPOD on IPOH.InvPurchaseOrderHeaderID = IPOD.InvPurchaseOrderHeaderID
where IPOH.DocumentStatus = 1 AND IPOH.DocumentDate >= (DATEADD(MONTH, -12, GETDATE()))
GROUP BY IPOD.StockCode
union
select IPOD.StockCode as stockcode, SUM(IPOD.OrderQty)/6 as newAverage from
InvPurchaseOrderHeader IPOH
INNER JOIN InvPurchaseOrderDetail IPOD on IPOH.InvPurchaseOrderHeaderID = IPOD.InvPurchaseOrderHeaderID
where IPOH.DocumentStatus = 1 AND IPOH.DocumentDate >= (DATEADD(MONTH, -6, GETDATE()))
GROUP BY IPOD.StockCode)
),select stockcode,avg(newAverage) from data group by stockcode;