Попробуйте:
;with tableA (LOCATION, PRODUCTS, QTY)
as
(
select 'WH 1','PRODUCT NO. 1',10
union all
select 'WH 1','PRODUCT NO. 2',100
union all
select 'WH 1','PRODUCT NO. 333',5
union all
select 'WH 2','PRODUCT NO. YYY',22
)
, tableB (LOCATION, PRODUCTS, QTY)
as
(
select 'WH 1','PRODUCT NO. 1',10
union all
select 'WH 1','PRODUCT NO. 2',100
union all
select 'WH 1','PRODUCT NO. 333',5
union all
select 'WH 1','PRODUCT NO. XXX',77
union all
select 'WH 2','PRODUCT NO. YYY',45
union all
select 'WH 1','PRODUCT NO. YYY',555
union all
select 'WH 2','PRODUCT NO. 1',14
)
select
LOCATION
, PRODUCTS
, isnull(QtyA,0) [Qty A]
, isnull(QtyB,0) [Qty B]
from
(
select
LOCATION
, PRODUCTS
, QTY
, 'QtyA' ColumnQty
from tableA
union all
select
LOCATION
, PRODUCTS
, QTY
, 'QtyB' ColumnQty
from tableB
) source
pivot
(
sum(QTY) for ColumnQty in ([QtyA],[QtyB])
) pvt
Я проверил это, и оно работает, как можно увидеть здесь.