Help Me to Remove duplicat values produced by the joins in sum() function


Hi every one i post one question before but no one Reply to it ... Now am going to ask my second question ...

am just developing pharmacy database and i use join function to join three tables and also sum function to sum and subtract item quantity from three tables this is the code ...

SELECT tblStore.Description ,tblStore.StreangthAndDosage, tblStore.Unit,SUM(tblStore.Quantity) as StoreSum,SUM(tblDispensary.Quantity) as DispensSum,SUM(tblSele.Quantity) as SeleSum,sum(tblStore.Quantity)- SUM(tblDispensary.Quantity)- SUM(tblSele.Quantity) as TotalRemain FROM (tblStore LEFT JOIN tblDispensary on tblStore.Description = tblDispensary.Description and tblStore.StreangthAndDosage = tblDispensary.StreangthAndDosage and tblStore.Unit = tblDispensary.Unit) LEFT JOIN tblSele on tblSele.Description = tblDispensary.Description and tblSele.StreangthAndDosage = tblDispensary.StreangthAndDosage and tblSele.Unit = tblDispensary.Unit GROUP BY tblStore.Description,tblStore.StreangthAndDosage, tblStore.Unit,tblDispensary.Description,tblDispensary.StreangthAndDosage, tblDispensary.Unit,tblSele.Description,tblSele.StreangthAndDosage, tblSele.Unit order by tblStore.Description

(this works in SQL serever 2005 and Access Database)
When i use this code it works fain but it duplicat the sum values ... when i use DISTINCT in each field like this

SELECT tblStore.Description ,tblStore.StreangthAndDosage, tblStore.Unit,SUM(DISTINCT tblStore.Quantity) as StoreSum,SUM(DISTINCT tblDispensary.Quantity) as DispensSum,SUM(DISTINCT tblSele.Quantity) as SeleSum,sum(DISTINCT tblStore.Quantity)- SUM(DISTINCT tblDispensary.Quantity)- SUM(DISTINCT tblSele.Quantity) as TotalRemain FROM (DISTINCT tblStore LEFT JOIN tblDispensary on tblStore.Description = tblDispensary.Description and tblStore.StreangthAndDosage = tblDispensary.StreangthAndDosage and tblStore.Unit = tblDispensary.Unit) LEFT JOIN tblSele on tblSele.Description = tblDispensary.Description and tblSele.StreangthAndDosage = tblDispensary.StreangthAndDosage and tblSele.Unit = tblDispensary.Unit GROUP BY tblStore.Description,tblStore.StreangthAndDosage, tblStore.Unit,tblDispensary.Description,tblDispensary.StreangthAndDosage, tblDispensary.Unit,tblSele.Description,tblSele.StreangthAndDosage, tblSele.Unit order by tblStore.Description

(this works in SQL serever 2005 database only)

it works good and the duplicats removed but it doesn't SUM (add) Similar values like this

if i have
S/N _____ Description ____StreangthAndDosage____Unit____Quantity
1 _______Aspirin _________250mmg___________ Tab______ 7
2________Aspirin_________250mmg____________Tab______7
3________ Aspirin_________250mm_____________Tab______5
4 _______ Aspirin _________250mmg ____________Tab_____ 7
5 _______ Aspirin _________500mmg ____________ Tab ____ 4

it only sum (7 + 5 + 4) but not the other similar values with similar conditions the reason is Because of the rows produced by the joins in sum() function Doese any one know how to remove this duplicated row/Values ? or and Other solution ...? any solution the solve this problem ...?
PLS try to help .......

If you enjoyed this post, subscribe for updates (it's free)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.