以下是我的查询。无论如何,有没有使用不同级别的组级别。我需要获取product_count列以计算不同的Product_id,但不在account_id and acct_type_id level
处。
当前,每行的product_count列为1。我只需要获取不同数量的产品ID,就需要将短期和长期销售汇总为account_id,product_id,acct_type_id
级。
所以我希望输出像这样:
order_date account_id short_sales long_sales **product_count** price product_id acct_type_id <br>
31-Oct-19 6698777 -2800 500 **0** 30 100005 3 <br>
31-Oct-19 6998874 -4000 325 **0** 30 100005 2 <br>
31-Oct-19 5555555 -5000 1000 **0** 30 100005 3 <br>
31-Oct-19 4444444 -1000 5000 **1** 30 100005 2
select POS.ORDER_DATE,POS.accOUNT_ID,SUM(POS.SHORT_SALES) as SHORT_SALES,SUM(POS.LONG_SALES) as LONG_SALES,COUNT(Distinct POS.PRODUCT_ID) AS PRODUCT_COUNT,POS.PRICE,POS.PRODUCT_ID,POS.accT_TYPE_ID
from PRODUCT
(
SELECT P.ORDER_DATE,P.accOUNT_ID,P.PRODUCT_ID,P.accT_TYPE_ID,P.PRICE,case when P.POSITION_SD < 0 THEN P.NET_POSITION_SD END AS SHORT_SALES,case when P.POSITION_SD > 0 THEN P.NET_POSITION_SD END AS LONG_SALES
FROM PRODUCT P
WHERE P.POSITION_DATE = '31-OCT-19'
) POS
group by POS.ORDER_DATE,POS.accT_TYPE_ID,POS.PRICE
ORDER BY PRODUCT_ID