如何在没有联合的情况下修改以下查询? [英] How do I modify the below query without union ?

查看:67
本文介绍了如何在没有联合的情况下修改以下查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想摆脱联盟,但仍然需要相同的结果集。



我是sql的新手请帮忙。



[EIDT]重复的内容有已被删除 - Maciej Los [/ EDIT]



我尝试过:

< br $>
查询::



 选择 con .FirstName + ' ' + con.LastName  as  ConsultantName,PositionName,MAX(tm.TsId) as  TsId,(pm.​​PositionId),tm.FromDate,tm.ToDate 来自 TimesheetMasters tm 
left join PositionMasters pm on tm.PositionId = pm.PositionId
left join 顾问con con.ConsultantId = pm.ConsultantId

其中 tm.SubmissionStatus = 0
- 和tm.TsId(通过PositionId从TimesheetMasters组中选择MAX(TsId))
转换 date ,tm.ToDate) 转换日期,DATEADD(日,-30,GETDATE())) CONVERT date ,GETDATE())
pm.CompanyKeyId=@CompanyKeyId
group by con.FirstName + < span class =code-string>' ' + con.LastName,PositionName,tm.TsId,pm.PositionId,tm.FromDate,tm。 ToDate
- 按订单排序


union

- 已创建职位但截至目前尚未提交时间表

选择 con.FirstName + ' ' + con.LastName as ConsultantName,PositionName, null as TsId,pm.PositionId,pm.StartDate,pm.EndDate
from PositionMasters pm
left join 顾问con on con.ConsultantId = pm.ConsultantId
其中 pm.PositionId 选择 PositionId 来自 TimesheetMasters )
pm.CompanyKeyId=@CompanyKeyId
convert date ,pm.StartDate) convert date ,DATEADD(day,-30,GETDATE())) CONVERT date ,GETDATE())

解决方案

使用UNION没有任何问题。它可能比使用OR逻辑布尔运算符执行得更快,因为它很可能会有更少的表读取。如果需要,可以尝试获取查询的上半部分,并使用下面的代码替换WHERE子句。我相信它会起作用,但没有任何数据很难说。



 其中(tm.SubmissionStatus =  0   pm.PositionId   in 选择 PositionId 来自 TimesheetMasters))


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::

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())

解决方案

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.

where (tm.SubmissionStatus = 0 OR pm.PositionId not in (select PositionId from TimesheetMasters))


这篇关于如何在没有联合的情况下修改以下查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆