[英]Multiple Where Clauses with Same Column in SQL Server
We have the below query running in one of our stored procedures:我们在我们的存储过程之一中运行以下查询:
SELECT
WOffice AS office,
RIGHT(JobTitle,4) JOBTITLE,
SUM(ISNULL(rHours,0)) AS rcount,
SUM(ISNULL(rdollars,0)) + SUM(ISNULL(amount,0)) AS rdollars,
CAST (DATEADD(wk,DATEDIFF(wk,0,paydate),-1)AS DATE )AS [week]
FROM
[dbo].[warehouse]
WHERE
paydate BETWEEN paydate AND DATEADD(DAY, 7, paydate)
AND (paydate BETWEEN @WkStart AND @EzMaxDate)
AND (PayDate BETWEEN @Seasonstart AND @SeasonEnd)
AND SUBSTRING(JobTitle,1,1) LIKE '%[A-Z]%'
GROUP BY
WOffice, RIGHT(JobTitle, 4), DATEADD(wk, DATEDIFF(wk, 0, paydate), -1)
In this code, the paydate column is used in multiple where clauses.在此代码中,paydate 列用于多个 where 子句中。 Is there a more efficient way to write the above logic in SQL?
有没有更有效的方法在 SQL 中编写上述逻辑?
The logic for paydate
is fine. paydate
的逻辑很好。 If SQL Server supported LEAST()
and GREATEST()
, you might be able to find other elegant solutions.如果 SQL Server 支持
LEAST()
和GREATEST()
,您也许可以找到其他优雅的解决方案。
Handling multiple overlapping time periods is tricky.处理多个重叠的时间段很棘手。 I cannot say that your logic is correct .
我不能说你的逻辑是正确的。 But, if your intention is to get the overlap for the different periods, then it is correct.
但是,如果您的目的是获得不同时期的重叠,那么它是正确的。
As a note, the logic for JOBTITLE
is overkill.需要注意的是,
JOBTITLE
的逻辑JOBTITLE
矫枉过正。 Either:任何一个:
JobTitle LIKE '[A-Z]%'
or:或者:
LEFT(JobTitle, 1) BETWEEN 'A' and 'Z'
I prefer the first approach, even since I started to like LIKE
again.我更喜欢第一种方法,即使我又开始喜欢
LIKE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.