sql server 2005 - Joins with aggregates doubling, sometimes tripling quantity amounts -


i'm trying join 4 tables several columns of results, 2 of sums/aggregates of respective columns. query returning multiples of true sums should be. here have:

select pl.[vendor item no_], bc.[item no_], min(ile.[description]) 'item description',         sum(ile.[quantity]) 'quantity on hand',          bc.[bin code] 'item location'  [live$bin content]bc left outer join [live$purchase line]pl   on bc.[item no_] = pl.[no_]left outer join [live$item ledger entry] ile   on bc.[item no_] = ile.[item no_] bc.[bin code] 'annex back' ,   bc.[item no_] 'sk%'   group pl.[vendor item no_], bc.[item no_], pl.[description], bc.[bin code] 

using subquery/inline view may solve problem. assuming else working. know need know pk/fk relationship between 3 tables.

select pl.[vendor item no_],         bc.[item no_],         min(ile.[description]) 'item description',        ile.[quantity] 'quantity on hand',         bc.[bin code] 'item location'  [live$bin content] bc   left join [live$purchase line] pl   on bc.[item no_] = pl.[no_]  left join  (select sum(quantity) quantity, [item no_]               [live$item ledger entry]               group [item no_]) ile   on bc.[item no_] = ile.[item no_] bc.[bin code] 'annex back' ,   bc.[item no_] 'sk%' group pl.[vendor item no_], bc.[item no_], pl.[description], bc.[bin code] 

per comment... if want add table , aggregrate quantity...

   select pl.[vendor item no_],             bc.[item no_],             min(ile.[description]) 'item description',            ile.[quantity] 'quantity on hand',             bc.[bin code] 'item location'      [live$bin content] bc       left join [live$purchase line] pl       on bc.[item no_] = pl.[no_]      left join  (select sum(quantity) quantity, [item no_]                   [live$item ledger entry]                   group [item no_]) ile       on bc.[item no_] = ile.[item no_]      left join  (select sum(newfield) quantity, [item no_]                   [newtable]                   group [item no_]) newalias       on bc.[item no_] = newalias.[item no_]     bc.[bin code] 'annex back'     ,   bc.[item no_] 'sk%'     group pl.[vendor item no_], bc.[item no_], pl.[description], bc.[bin code] 

however if field in 1 of existing tables, need add new field on subquery...

select pl.[vendor item no_],         bc.[item no_],         min(ile.[description]) 'item description',        ile.[quantity] 'quantity on hand',         bc.[bin code] 'item location',        ile.count count of items inventory in ile.  [live$bin content] bc   left join [live$purchase line] pl   on bc.[item no_] = pl.[no_]  left join  (select sum(quantity) quantity, count(quantity) count, [item no_]               [live$item ledger entry]               group [item no_]) ile   on bc.[item no_] = ile.[item no_] bc.[bin code] 'annex back' ,   bc.[item no_] 'sk%' group pl.[vendor item no_], bc.[item no_], pl.[description], bc.[bin code] 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -