需要帮助理解SUM

时间:2015-04-08 17:10:51

标签: sql sql-server-2008 sum

我希望看到结果总计收到的列,并且只返回项目ID一次,收到的总数。有人可以解释为什么这不起作用并帮助我到达我需要的地方吗?

编辑 - 我喜欢结果

Item A - 65
Item B - 52
Item C - 150

instead of what I am getting currently:
Item A - 15
Item A - 20
Item A - 30
Item B - 10
Item B - 15
etc

谢谢!

select S.supplier_id,
   s.supplier_name,
   ph.po_no,
   ph.order_date,
   ph.location_id,
   im.item_id,
   im.item_desc,
   invs.supplier_part_no,
   sum( pl.qty_received) as received,
   pl.unit_price
from po_line pl
inner join po_hdr ph on ph.po_no = pl.po_no
inner join supplier s on s.supplier_id = ph.supplier_id
inner join inv_mast im on im.inv_mast_uid = pl.inv_mast_uid
left join inventory_supplier invs on invs.supplier_id = s.supplier_id 
and invs.inv_mast_uid = im.inv_mast_uid
where ph.order_date between '2014-01-01' and '2014-12-31'
and ph.supplier_id = '101315'
and ph.delete_flag != 'Y'
and ph.complete = 'y'
and pl.delete_flag != 'y'
and pl.cancel_flag != 'y'
and ph.cancel_flag != 'y'
Group by 
pl.qty_received,
im.item_id,
S.supplier_id,
s.supplier_name,
ph.po_no,
ph.location_id,
im.item_desc,
invs.supplier_part_no,
pl.unit_price,
ph.order_date
order by item_id

Results

1 个答案:

答案 0 :(得分:1)

问题在于聚合正在对导致数据分离的其他更改值进行分组。

select S.supplier_id, -- values are the same, but is this necessary in your result set; if this value changes, you would have the separation in your data
   s.supplier_name, -- cannot see in your picture, but these are likely all the same
   ph.po_no, -- values are different, is this necessary in your result set?
   ph.order_date, -- values are different, again, is this necessary in your result set?
   ph.location_id, -- values are the same, but is this necessary in your result set; if this value changes, you would have the separation in your data
   im.item_id, -- the primary grouping factor
   im.item_desc, -- the description of the primary grouping factor (should not change per record)
   invs.supplier_part_no, -- values are the same, but is this necessary in your result set; if this value changes, you would have the separation in your data
   sum( pl.qty_received) as received, -- the aggregate you are performing
   pl.unit_price -- we cannot see this in your picture, but this could also be a changing value that could cause separation in your data, is it necessary in your result set?
from po_line pl
inner join po_hdr ph on ph.po_no = pl.po_no
inner join supplier s on s.supplier_id = ph.supplier_id
inner join inv_mast im on im.inv_mast_uid = pl.inv_mast_uid
left join inventory_supplier invs on invs.supplier_id = s.supplier_id 
and invs.inv_mast_uid = im.inv_mast_uid
where ph.order_date between '2014-01-01' and '2014-12-31'
and ph.supplier_id = '101315'
and ph.delete_flag != 'Y'
and ph.complete = 'y'
and pl.delete_flag != 'y'
and pl.cancel_flag != 'y'
and ph.cancel_flag != 'y'
Group by -- the Group by clause should change with a change in the columns selected
pl.qty_received, -- the aggregate should also, likely, not be part of the group by clause
im.item_id,
S.supplier_id,
s.supplier_name,
ph.po_no,
ph.location_id,
im.item_desc,
invs.supplier_part_no,
pl.unit_price,
ph.order_date
order by item_id

-- an example of how this might look

select S.supplier_id,
   MAX(s.supplier_name),
   im.item_id,
   MAX(im.item_desc),
   sum( pl.qty_received) as received,
from po_line pl
inner join po_hdr ph on ph.po_no = pl.po_no
inner join supplier s on s.supplier_id = ph.supplier_id
inner join inv_mast im on im.inv_mast_uid = pl.inv_mast_uid
left join inventory_supplier invs on invs.supplier_id = s.supplier_id 
and invs.inv_mast_uid = im.inv_mast_uid
where ph.order_date between '2014-01-01' and '2014-12-31'
and ph.supplier_id = '101315'
and ph.delete_flag != 'Y'
and ph.complete = 'y'
and pl.delete_flag != 'y'
and pl.cancel_flag != 'y'
and ph.cancel_flag != 'y'
Group by
S.supplier_id,
im.item_id
order by item_id