[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Help with Distinct and Sum

Posted on 2011-03-03
2
Medium Priority
?
391 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 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 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