早上好。 我需要帮助。
我有这段代码:
select A.AREA_CODE, A.OUTLET_NAME, SUM(B.AMOUNT) as NETSALES
from M_OUTLET A, T_SALES_DETAIL B
where A.OUTLET_NO = B.OUTLET_NO and A.OUTLET_TYPE_DESC not
like '%head%' and A.OUTLET_TYPE_DESC not like '%prod%' and
A.OUTLET_TYPE_DESC not like '%stor%'
and B.SYSTEM_DATE between CONVERT (datetime, '3/1/2014')
and CONVERT (datetime, '3/31/2014')
and B.VOID = 'N' group by A.AREA_CODE , A.OUTLET_NAME order by A.AREA_CODE
这是输出:
AREA_CODE OUTLET_NAME NETSALES
1 MAKATI BU CAFE 2 226202.52
2 MAKATI BU CART 170305.01
其他代码是:
SELECT A.AREA_CODE, SUM (C.AMOUNT) AS E_SALES
FROM E_SALES_DETAIL C , M_OUTLET A
WHERE C.SYSTEM_DATE
between CONVERT (datetime, '3/1/2014') and
CONVERT (datetime, '3/31/2014') and C.VOID = 'N'
GROUP BY A.AREA_CODE ORDER BY A.AREA_CODE
,输出为:
AREA_CODE E_SALES
1 22208347.35
2 14453051.45
我的问题是,如何加入此代码才能获得此输出:
AREA_CODE OUTLET_NAME E_SALES NETSALES
1 MAKATI BU CAFE 2 22208347.35 226202.52
2 MAKATI BU CART 14453051.45 170305.01
谢谢。
答案 0 :(得分:0)
最简单的方法是将两个查询用作子查询。也就是说,我认为你在第二个子查询中缺少join
条件:
select ab.area_code. ab.outlet_name, ab.netsales, ac.e_sales
from (select A.AREA_CODE, A.OUTLET_NAME, SUM(B.AMOUNT) as NETSALES
from M_OUTLET A join
T_SALES_DETAIL B
on A.OUTLET_NO = B.OUTLET_NO
where A.OUTLET_TYPE_DESC not like '%head%' and
A.OUTLET_TYPE_DESC not like '%prod%' and
A.OUTLET_TYPE_DESC not like '%stor%' and
B.SYSTEM_DATE between CONVERT(datetime, '3/1/2014') and CONVERT(datetime, '3/31/2014') and
B.VOID = 'N'
group by A.AREA_CODE, A.OUTLET_NAME
) ab join
(SELECT A.AREA_CODE, SUM(C.AMOUNT) AS E_SALES
FROM E_SALES_DETAIL C join
M_OUTLET A
ON ????
WHERE C.SYSTEM_DATE between CONVERT(datetime, '3/1/2014') and CONVERT(datetime, '3/31/2014') and
C.VOID = 'N'
GROUP BY A.AREA_CODE
) ac
on ab.area_code ac.area_code;