Solved

Help with Distinct and Sum

Posted on 2011-03-03
2
386 Views
Last Modified: 2012-06-27
I have the following query to get a total inventory for particular resource numbers and i am getting wrong number (too high) So i took the query out of the group cause, added Load ID and found out that they were counting duplicate load ids for each resource number.(there is a good reason for it) anyways how would i write the query so that it gets the distinct records first, then sum it up?  

I am not sure if i use SELECT sum( DISTINCT dbo.t_Load.Load_Qty * dbo.vx_Item_Master_MC.Factory_Board_Factor_count AS OH_Qty)  would be accurate because it would be using a distinct qty instead of Load ID (which is not what i need to show )

SELECT  sum(dimdbmc.dbo.t_Load.Load_Qty * dimdbmc.dbo.vx_Item_Master_MC.Factory_Board_Factor_count) AS OH_Qty, 'AB1' AS FFID
FROM         dimdbmc.dbo.t_Routing_Operation_Resource INNER JOIN
                      dimdbmc.dbo.t_Routing_Operation ON dimdbmc.dbo.t_Routing_Operation_Resource.Routing_Id = dimdbmc.dbo.t_Routing_Operation.Routing_Id INNER JOIN
                      dimdbmc.dbo.t_BOM ON dimdbmc.dbo.t_Routing_Operation_Resource.MMM_Facility_Code = dimdbmc.dbo.t_BOM.MMM_Facility_Code AND 
                      dimdbmc.dbo.t_Routing_Operation_Resource.Routing_Id = dimdbmc.dbo.t_BOM.Routing_Id INNER JOIN
                      dimdbmc.dbo.t_Load ON dimdbmc.dbo.t_Routing_Operation_Resource.MMM_Facility_Code = dimdbmc.dbo.t_Load.MMM_Facility_Code AND 
                      dimdbmc.dbo.t_BOM.MMM_Id_Nbr = dimdbmc.dbo.t_Load.MMM_Id_Nbr INNER JOIN
                      dimdbmc.dbo.vx_Item_Master_MC ON dimdbmc.dbo.t_Routing_Operation_Resource.MMM_Facility_Code = dimdbmc.dbo.vx_Item_Master_MC.MMM_Facility_Code AND 
                      dimdbmc.dbo.t_Load.MMM_Id_Nbr = dimdbmc.dbo.vx_Item_Master_MC.MMM_Id_Nbr
WHERE        ((dimdbmc.dbo.t_Routing_Operation_Resource.Resource_Nbr = '4503') OR  (dimdbmc.dbo.t_Routing_Operation_Resource.Resource_Nbr = '4504') OR  (dimdbmc.dbo.t_Routing_Operation_Resource.Resource_Nbr = '4511')) AND (dimdbmc.dbo.t_Load.Active_Status_Ind = 'A') and (dimdbmc.dbo.vx_Item_Master_MC.Material_Form_Code = '05')

Open in new window

0
Comment
Question by:sassy168
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 35032910
try this.
SELECT SUM(Load_Qty * Factory_Board_Factor_count) AS OH_Qty, 
       'AB1'                                      AS FFID 
  FROM (SELECT DISTINCT dimdbmc.dbo.t_Load.Load_Qty, 
                        dimdbmc.dbo.vx_Item_Master_MC.Factory_Board_Factor_count 
          FROM dimdbmc.dbo.t_Routing_Operation_Resource 
               INNER JOIN dimdbmc.dbo.t_Routing_Operation 
                 ON dimdbmc.dbo.t_Routing_Operation_Resource.Routing_Id = dimdbmc.dbo.t_Routing_Operation.Routing_Id
               INNER JOIN dimdbmc.dbo.t_BOM 
                 ON dimdbmc.dbo.t_Routing_Operation_Resource.MMM_Facility_Code = dimdbmc.dbo.t_BOM.MMM_Facility_Code
                    AND dimdbmc.dbo.t_Routing_Operation_Resource.Routing_Id = dimdbmc.dbo.t_BOM.Routing_Id
               INNER JOIN dimdbmc.dbo.t_Load 
                 ON dimdbmc.dbo.t_Routing_Operation_Resource.MMM_Facility_Code = dimdbmc.dbo.t_Load.MMM_Facility_Code
                    AND dimdbmc.dbo.t_BOM.MMM_Id_Nbr = dimdbmc.dbo.t_Load.MMM_Id_Nbr 
               INNER JOIN dimdbmc.dbo.vx_Item_Master_MC 
                 ON dimdbmc.dbo.t_Routing_Operation_Resource.MMM_Facility_Code = dimdbmc.dbo.vx_Item_Master_MC.MMM_Facility_Code
                    AND dimdbmc.dbo.t_Load.MMM_Id_Nbr = dimdbmc.dbo.vx_Item_Master_MC.MMM_Id_Nbr
         WHERE ((dimdbmc.dbo.t_Routing_Operation_Resource.Resource_Nbr = '4503') 
                 OR (dimdbmc.dbo.t_Routing_Operation_Resource.Resource_Nbr = '4504') 
                 OR (dimdbmc.dbo.t_Routing_Operation_Resource.Resource_Nbr = '4511')) 
               AND (dimdbmc.dbo.t_Load.Active_Status_Ind = 'A') 
               AND (dimdbmc.dbo.vx_Item_Master_MC.Material_Form_Code = '05')) t1

Open in new window

0
 
LVL 22

Expert Comment

by:Thomasian
ID: 35033273
>>I am not sure if i use SELECT sum( DISTINCT dbo.t_Load.Load_Qty * dbo.vx_Item_Master_MC.Factory_Board_Factor_count AS OH_Qty)
>> would be accurate because it would be using a distinct qty instead of Load ID (which is not what i need to show )
No, unless you can guarantee that the product of the 2 values is unique for all the records you need.

What is the relationship between tables t_Routing_Operation_Resource and t_Routing_Operation?

What is the purpose of including t_Routing_Operation in the query?

Try removing it from the query and check if it returns correct results
SELECT  sum(dimdbmc.dbo.t_Load.Load_Qty * dimdbmc.dbo.vx_Item_Master_MC.Factory_Board_Factor_count) AS OH_Qty, 'AB1' AS FFID
FROM         dimdbmc.dbo.t_Routing_Operation_Resource INNER JOIN
                      dimdbmc.dbo.t_BOM ON dimdbmc.dbo.t_Routing_Operation_Resource.MMM_Facility_Code = dimdbmc.dbo.t_BOM.MMM_Facility_Code AND 
                      dimdbmc.dbo.t_Routing_Operation_Resource.Routing_Id = dimdbmc.dbo.t_BOM.Routing_Id INNER JOIN
                      dimdbmc.dbo.t_Load ON dimdbmc.dbo.t_Routing_Operation_Resource.MMM_Facility_Code = dimdbmc.dbo.t_Load.MMM_Facility_Code AND 
                      dimdbmc.dbo.t_BOM.MMM_Id_Nbr = dimdbmc.dbo.t_Load.MMM_Id_Nbr INNER JOIN
                      dimdbmc.dbo.vx_Item_Master_MC ON dimdbmc.dbo.t_Routing_Operation_Resource.MMM_Facility_Code = dimdbmc.dbo.vx_Item_Master_MC.MMM_Facility_Code AND 
                      dimdbmc.dbo.t_Load.MMM_Id_Nbr = dimdbmc.dbo.vx_Item_Master_MC.MMM_Id_Nbr
WHERE        ((dimdbmc.dbo.t_Routing_Operation_Resource.Resource_Nbr = '4503') OR  (dimdbmc.dbo.t_Routing_Operation_Resource.Resource_Nbr = '4504') OR  (dimdbmc.dbo.t_Routing_Operation_Resource.Resource_Nbr = '4511')) AND (dimdbmc.dbo.t_Load.Active_Status_Ind = 'A') and (dimdbmc.dbo.vx_Item_Master_MC.Material_Form_Code = '05')

Open in new window

0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question