?
Solved

SQL subquery displays too many results

Posted on 2014-03-10
2
Medium Priority
?
197 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 2000 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
Suggested Courses

777 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