我正试图在我们的两个仓库之间得到一个总数的qty_on_hand但是在我按总计进行分组之后不要合并。我不确定我做错了什么。
SELECT
sku_master.warehouse,
sku_master.sku
, sku_master.min_on_hand
, sku_master.max_on_hand
, x.total_qty_on_hand
FROM [wms].dbo.[sku_master]
left join
(
SELECT
sku_master.sku, sum(location_inventory.qty_on_hand) as total_qty_on_hand
FROM [wms].[dbo].[location_inventory]
JOIN [wms].dbo.[sku_master] ON location_inventory.sku = sku_master.sku
GROUP BY sku_master.sku) x on sku_master.sku = x.sku
结果
预期结果
答案 0 :(得分:1)
您希望每个qty_on_hand
的所有sku
加起来。使用OUTER APPLY
SELECT m.sku,
m.min_on_hand,
m.max_on_hand,
oa.total_qty_on_hand
FROM (SELECT sku,
Min(min_on_hand) min_on_hand,
Max(max_on_hand) max_on_hand
FROM [wms].dbo.[sku_master] m
GROUP BY sku) m
OUTER Apply (SELECT Sum(l.qty_on_hand) AS total_qty_on_hand
FROM [wms].[dbo].[location_inventory] l
WHERE l.sku = m.sku) oa
答案 1 :(得分:0)
如果您只需要总计,那么就不要加入,或只是为了加入一个工会这个
SELECT
sku_master.warehouse,
sku_master.sku
, sku_master.min_on_hand
, sku_master.max_on_hand
, x.total_qty_on_hand
FROM [wms].dbo.[sku_master]
left join
(
SELECT
sku_master.sku, sum(location_inventory.qty_on_hand) as total_qty_on_hand
FROM [wms].[dbo].[location_inventory]
JOIN [wms].dbo.[sku_master] ON location_inventory.sku = sku_master.sku
GROUP BY sku_master.sku) x on sku_master.sku = x.sku
union
SELECT null, null, nullm null, sum(location_inventory.qty_on_hand)
FROM [wms].[dbo].[location_inventory]
JOIN [wms].dbo.[sku_master] ON location_inventory.sku = sku_master.sku
或者你没有对sku_master.warehouse进行分组和重复这一事实..将其从表单中删除
SELECT
sku_master.sku
, sku_master.min_on_hand
, sku_master.max_on_hand
, x.total_qty_on_hand
FROM [wms].dbo.[sku_master]
left join
(
SELECT
sku_master.sku, sum(location_inventory.qty_on_hand) as total_qty_on_hand
FROM [wms].[dbo].[location_inventory]
JOIN [wms].dbo.[sku_master] ON location_inventory.sku = sku_master.sku
GROUP BY sku_master.sku) x on sku_master.sku = x.sku
答案 2 :(得分:0)
你可以使用这个简单的查询
SELECT
t.sku,
min(t.min_on_hand) as min_qty_on_Hand,
max(t.max_on_hand) as max_on_hand,
sum(t.total_qty_on_hand) + sum(t2.total_qty_on_hand) as total_qty_on_hand
FROM table1 t
inner join table2 t2
on t.sku = t2.sku
group by t.sku
附图显示了包含结果的过程
注意:此处的结果与您在问题中提到的相同,此处您需要将表名更改为表名
答案 3 :(得分:0)
在您的请求评论中,您解释说您更喜欢一个仓库而不是另一个仓库。在这种情况下,您应该有一个包含仓库的表,其中包含优先级列。因此,查询不必了解仓库优先级。 (想象一下你有一天会添加一个仓库 - 你必须改变所有的查询。)
我的查询汇总每个SKU和仓库的sku_master记录,然后根据SKU选择最佳仓库。然后它通过SKU加入汇总的库存行。
这是没有仓库表的查询。
select
m.warehouse,
m.sku,
m.total_min_on_hand,
m.total_max_on_hand,
li.total_qty_on_hand
from
(
select
sku,
warehouse,
sum(min_on_hand) as total_min_on_hand,
sum(max_on_hand) as total_max_on_hand,
row_number() over (partition by sku
order by case when warehouse = 'XDGM' then 1 else 2 end) as rn
from sku_master
group by sku
) m
join
(
select sku, sum(qty_on_hand) as total_qty_on_hand
from location_inventory
group by sku
) li on li.sku = m.sku
where m.rn = 1; -- only the better warehouse when there is more than one for the SKU
对于仓库表,from子句会稍微改为:
from
(
select
sku,
warehouse,
sum(min_on_hand) as total_min_on_hand,
sum(max_on_hand) as total_max_on_hand,
row_number() over (partition by sku order by w.priority) as rn
from sku_master sm
join warehouse w on w.warehouse = sm.warehouse
group by sku
) m