Solved

Help with Distinct and Sum

Posted on 2011-03-03
2
378 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

756 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