我有两张桌子:
表A'真实订单':
表B'上传':
我想使用以下结构进行查询:
合约,YYYYMM,Nb of Real Orders,Nb of Uploads
如果知道表A中的某些合同没有出现在表B中,反之亦然,我怎样才能做到这一点?我使用的是SQL Server 2012。
答案 0 :(得分:0)
让我知道这是否有效
select coalesce( a.contract,b.contract) as contract,
coalesce(a.YYYYMM,b.YYYYMM) as YYYYMM,
count (a.order_id) as no_real_orders,
count (b.order_id) as no_uploads
from Table_a as a
full join Table_b as b
on a.contract = b.contract
and a.YYYYMM = b.YYYYMM
group by coalesce(a.contract,b.contract), coalesce(a.YYYYMM,b.YYYYMM)
答案 1 :(得分:0)
不幸的是,我对两个列的计数都相同。以下是输出的摘录:
Contract YYYYMM Nb Real Orders Nb Uploads
Contract_x 201701 17 17
Contract_x 201612 72 72
Contract_y 201702 196 196
Contract_y 201612 345 345
Contract_y 201701 264 264
代码是:
select coalesce(a.Contract_Code,b.Contract_Code) as Contract,
coalesce(a.OIA_Creation_Date_YYYYMM,b.Ordear_Creation_YYYYMM) as
YearMonth, count(a.OIA_Order_Number) as Nb_Real_Orders,
count(b.Order_Number) as Nb_Uploads from Raw_Data_A as a full join
Raw_Data_B as b on a.Contract_Code=b.Contract_Code and
a.OIA_Creation_Date_YYYYMM=b.Ordear_Creation_YYYYMM group by
coalesce(a.Contract_Code,b.Contract_Code),coalesce(a.OIA_Creation_Date_YYYYMM,b.Ordear_Creation_YYYYMM)
答案 2 :(得分:0)
我尝试将代码分成两个子查询,让我知道这是否有效..
select coalesce(a.contract,b.contract) as contract,
coalesce(a.YYYYMM,b.YYYYMM) as YYYYMM,
no_real_orders,
no_uploads
from
(
select contract,YYYYMM, count (order_id) as no_real_orders,
from Table_a
group by contract, YYYYMM
) as a
full join
(
select contract,YYYYMM, count (order_id) as no_uploads,
from Table_b
group by contract, YYYYMM
) as b
on a.contract = b.contract
and a.YYYYMM = b.YYYYMM