Solved

SQL subquery displays too many results

Posted on 2014-03-10
2
190 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

911 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

22 Experts available now in Live!

Get 1:1 Help Now