Solved

Help with Distinct and Sum

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Read about achieving the basic levels of HRIS security in the workplace.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

708 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

16 Experts available now in Live!

Get 1:1 Help Now