Solved

Help with Distinct and Sum

Posted on 2011-03-03
2
354 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
2 Comments
 
LVL 40

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now