[英]SQL Querying by a specific month
I'm doing some homework and part of a question is asked like so: "a list of assists open in the month of December 2004" 我正在做一些作业,一部分问题是这样问的:“辅助工具清单将于2004年12月开放”
I've got a majority of a subquery down for paring down what else is required, however I'm coming up short with a way to only get results specifically from only December '04, regardless of when the OpenDate is. 我已经减少了子查询的大部分内容,以减少其他需求,但是我想办法只从04年12月开始才获得结果,而与OpenDate的时间无关。 I've been unable to get the OpenDate field to play nicely with DATEPART(Month,x) in my query because of the above restriction of it not mattering when it was opened.
我一直无法使OpenDate字段在查询中与DATEPART(Month,x)完美配合,因为上述限制使其在打开时无关紧要。
The following is the Assists table. 以下是Assists表。
CREATE TABLE Assists
(
AssistID INT IDENTITY NOT NULL PRIMARY KEY,
OpenDate DATE NOT NULL DEFAULT GETDATE(),
CloseDate DATE NULL,
ContactType VARCHAR(15) NOT NULL,
ReferralSource VARCHAR(60) NULL,
RefPhone VARCHAR(10) NULL,
RefConfidential BIT NULL,
RefFollowUpNeeded BIT NULL,
AssistType VARCHAR(55) NOT NULL,
SpecialistID INT NOT NULL
CONSTRAINT fk_Assists_Specialists FOREIGN KEY (SpecialistID) REFERENCES Specialists(SpecialistID),
ClientID INT NOT NULL
CONSTRAINT fk_Assists_Clients FOREIGN KEY (ClientID) REFERENCES Clients(ClientID),
CONSTRAINT ck_Assists_OpenDate CHECK (OpenDate <= GETDATE()),
CONSTRAINT ck_Assists_CloseDate CHECK (CloseDate <= GETDATE()),
CONSTRAINT ck_Assists_RefPhone CHECK (RefPhone LIKE REPLICATE('[0-9]',10)),
)
Any help is much appreciated. 任何帮助深表感谢。
I think these are the conditions you should be looking at: 我认为这些是您应该注意的条件:
OpenDate
is in or before December 2004 AND OpenDate
在2004年12月或之前,并且 CloseDate
is either NULL
OR is in or after December 2004 CloseDate
为NULL
或在2004年12月或之后 SELECT *
FROM Assists
WHERE OpenDate <= '20041231'
AND (CloseDate IS NULL OR CloseDate >= '20041201')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.