Solved

SQL subquery displays too many results

Posted on 2014-03-10
2
195 Views
Last Modified: 2014-03-19
I am in a bind because I suck at writing queries. Any help will be appreciated. I have a query with some joins and a subquery that doesn't display the data I need. It is either duplicating the data or grabbing data that is not needed. I segregated the join that is causing the issue.

Select EMKey, oa.Num, RB.Grouping4 as CompanyName, RB.EmployeeName, emppr.hiredate, Sum(RB.HOURSBILLED) as HrsBilled, Sum(RB.AMOUNTBILLED) as AmountBilled,
Sum(RB.COSTEARN) as CostEarn, Sum(RB.WCCOST) as WCCost, Sum(RB.OASDICOST) as OASDICost,
Sum(RB.MEDICARECOST) as MedicareCost, Sum(RB.FUTACOST) as FUTACost, Sum(RB.SUICOST) as SUICost, Sum(RB.EICCREDIT) as EICCredit,
Sum(RB.AMOUNTBILLED-RB.COSTEARN-RB.WCCOST-RB.OASDICOST-RB.MEDICARECOST-RB.FUTACOST-RB.SUICOST) as Profit,
Sum(RB.MARKUP) as Markup, Sum(RB.TOTALCOST) as TotalCost,
emppr.SocialSecurityNumber, emp.Address1, emp.City, emp.StateCode, emp.PostalCode
From #Result as RB
left join employeeprmaster as emppr ON emppr.employee_ID = RB.EMKey
left join employeemaster as emp ON emp.employee_ID = RB.EMKey

left join (select employee_id, userdefined1 as Num, assignment_id from orderassignment group by employee_id, assignment_id, userdefined1) as oa ON oa.employee_id = RB.EMKey

Where RB.Earningscode <> 'EXPEN' and RB.Earningscode <> 'EXPEN2' or RB.EarningsCode is Null
Group by RB.EMKey, RB.Grouping3, RB.Grouping4, RB.EmployeeName, emppr.SocialSecurityNumber, emppr.hiredate,
emp.Address1, emp.City, emp.StateCode, emp.PostalCode, oa.Num
order by EMKey
0
Comment
Question by:Techneut
[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 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39918391
>It is either duplicating the data or grabbing data that is not needed.
Usually when a query returns duplicate data it is because one of the JOINS is missing a column in order to have a unique relationship between the two sets involved.

We'd have to see some sample data to flush this out.
0
 
LVL 1

Author Comment

by:Techneut
ID: 39939879
I had to find external help with this because of the situation. I found that because of the joins, I had to do multiple join ON connections since there were many to many objects.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 ?
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.
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…

688 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