Click here to Skip to main content
16,011,905 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to get rid of the union but still need the same resultset.

I'm new to sql Please help.

[EIDT]Duplicated content has been removed - Maciej Los[/EDIT]

What I have tried:

Query::

SQL
select con.FirstName +' ' +con.LastName as ConsultantName,PositionName,MAX(tm.TsId)  as TsId, (pm.PositionId), tm.FromDate,tm.ToDate from TimesheetMasters tm
          left join PositionMasters pm on tm.PositionId=pm.PositionId
          left join Consultants con on con.ConsultantId=pm.ConsultantId

          where  tm.SubmissionStatus=0 
          --and tm.TsId in ( select MAX( TsId) from TimesheetMasters group by PositionId)
          and convert(date, tm.ToDate) between convert(date, DATEADD(day,-30,GETDATE())) and CONVERT(date, GETDATE())
          and pm.CompanyKeyId=@CompanyKeyId
          group by con.FirstName +' ' +con.LastName,PositionName,tm.TsId,pm.PositionId, tm.FromDate,tm.ToDate
          --order by TsId desc


          union

-- Position created but Timesheet is not submitted till now

select con.FirstName +' ' +con.LastName as ConsultantName,PositionName,null as TsId, pm.PositionId,pm.StartDate, pm.EndDate 
          from PositionMasters pm 
          left join Consultants con on con.ConsultantId=pm.ConsultantId
          where pm.PositionId not in (select PositionId from TimesheetMasters) 
          and pm.CompanyKeyId=@CompanyKeyId
          and convert (date,pm.StartDate) between convert(date, DATEADD(day,-30,GETDATE())) and CONVERT(date, GETDATE())
Posted
Updated 3-Jan-17 19:55pm
v2
Comments
Suvendu Shekhar Giri 4-Jan-17 1:04am    
What is the problem, if you are getting correct result?
Is it slower than expected?

1 solution

There is nothing wrong with using UNION. It would probably perform faster than using the OR logical boolean operator because it most likely will have less table reads. If you want, you can try and take the top half of the query and substitute the WHERE clause with the code below. I believe it will work, but hard to tell without any data.

SQL
where (tm.SubmissionStatus = 0 OR pm.PositionId not in (select PositionId from TimesheetMasters))
 
Share this answer
 
Comments
Member 10468587 4-Jan-17 1:54am    
It's not yielding the same result

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900