简体   繁体   English

存在SQL Server的位置

[英]Where Exists SQL Server

I have the following statement which gets clients over the age of 80 我有以下声明可以使80岁以上的客户

SELECT
    C.ClientID, 
    C.surname  + C.forename As Name,
    CONVERT(VARCHAR(10),  C.DateOfBirth, 103) AS DOB,
    S.SchemeID,
    [Status]
FROM 
    vClients C
INNER JOIN
    vClientSchemes S ON C.ClientID = S.ClientID
INNER JOIN
    vEvents E ON C.ClientId = E.ClientID
WHERE
    (DATEDIFF(yy, C.DateOfBirth, GetDate()) - 
       CASE WHEN((MONTH(DateOfBirth) * 100 + DAY(C.DateOfBirth)) >   (MONTH(GetDate()) * 100 + DAY(GetDate()))) 
              THEN 1 ELSE 0 END) > 80
    AND (DATEDIFF(yy, DateOfBirth, GetDate()) - 
        CASE WHEN((MONTH(DateOfBirth) * 100 + DAY(C.DateOfBirth)) >      
                   (MONTH(GetDate()) * 100 + DAY(GetDate()))) 
             THEN 1 ELSE 0 END) < 100
    AND C.[Status] = 0 
    AND S.SchemeID = 3

However from the result I also need to get the date of when they were last used by the service. 但是,从结果中,我还需要获取服务上次使用它们的日期。 This is achieved by querying the table with the following columns 这是通过查询具有以下列的表来实现的

EventId, EventDate, ClientID

So from the events date using the following query. 因此从事件日期起使用以下查询。 So far I have 到目前为止,我有

SELECT ClientID 
FROM vEvents 
WHERE EventDate > DATEADD(year,-1,GETDATE()) 

But I cant seem to link the two queries 但我似乎无法链接两个查询

Your where predicates are nonSARGable because you have wrapped your columns in functions. 您的where谓词不可SARG,因为您已将列包装在函数中。 You could make this SARGable AND a lot simpler like this. 您可以使此SARGable更加简单。

WHERE DateOfBirth < DATEADD(Year, -80, cast(getdate() as date)
AND DateOfBirth > DATEADD(Year, -100, cast(getdate() as date)
AND C.[Status] = 0 
AND S.SchemeID = 3

For your actual question you could use MAX(EventDate) and group by the rest of the columns, or you could use ROW_NUMBER. 对于您的实际问题,您可以使用MAX(EventDate)并按其余列进行分组,也可以使用ROW_NUMBER。

It looks like you already have access to the vEvents table in your query through alias E . 看起来您已经可以通过别名E访问查询中的vEvents表。 You should be able to add the filter you indicated by adding: 您应该能够通过添加以下内容来添加您指定的过滤器:

AND E.EventDate > DATEADD(year, -1, GETDATE())

The ClientID should be the same from the vEvents table as it is from your vClients table, due to the inner join, and you're already selecting it. 由于存在内部vClients ,因此vClients表中的ClientID应该与vEvents表中的ClientID相同,并且您已经选择了它。

Yep marc_2 you were right just needed to add the condition and a distinct as well, but remove the select on the EventDate 是的marc_2您只需要添加条件和一个唯一条件,但是在EventDate上删除选择

SELECT
distinct C.ClientID, 
C.surname  + C.forename As Name,
CONVERT(VARCHAR(10),  C.DateOfBirth, 103) AS DOB
FROM vClients C
inner join vClientSchemes S on C.ClientID = S.ClientID
inner join vEvents E ON C.ClientId = E.ClientID
WHERE
(DATEDIFF(yy,C.DateOfBirth,GetDate()) - 
CASE WHEN((MONTH(DateOfBirth)*100 + DAY(C.DateOfBirth)) > (MONTH(GetDate())*100 + DAY(GetDate()))) THEN 1 ELSE 0 END)> 80
 AND (DATEDIFF(yy,DateOfBirth,GetDate()) - CASE WHEN((MONTH(DateOfBirth)*100 + DAY(C.DateOfBirth)) > (MONTH(GetDate())*100 + DAY(GetDate()))) THEN 1 ELSE 0 END) <100
AND C.[Status] = 0 AND  S.SchemeID = 3 AND EventDate > DATEADD(year,-1,GETDATE()) order by C.clientid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM