Solved

SQL subquery displays too many results

Posted on 2014-03-10
2
189 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
2 Comments
 
LVL 65

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

758 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

19 Experts available now in Live!

Get 1:1 Help Now