简体   繁体   English

在SQL Server的视图中提供基于日期的活动状态列

[英]Provide status of an Activity based on date column in a view in SQL Server

In the view have to provide Status based on date. 在视图中必须提供基于日期的Status

The condition is: 条件是:

  • If one of the activities doesn't have a date for the person, then Status is Active 如果其中一项活动没有该人的日期,则StatusActive

  • If all the activities have a date for the person, then Status is Not Active 如果所有活动都有该人的日期,则“ Status为“ Not Active

How do I get STATUS for a person? 如何获得一个人的STATUS

For example: 例如:

|PersonId|Name|Activity | Date      | 
+--------+----+---------+-----------+
| 1      |John| a       | 01/01/2015|
| 1      |John| b       | 03/12/2016|
| 1      |John| c       | 02/13/2017|
| 2      |Sam | d       | 06/01/2014|
| 2      |Sam | e       | 05/18/2016|
| 2      |Sam | f       | NULL      |   

Output in the view: 视图中的输出:

|PersonId|Name|Status    | 
+--------+----+----------+
| 1      |John|Not Active| 
| 2      |Sam | Active   | 

You can do this with aggregation: 您可以通过聚合来做到这一点:

select personid, name,
       (case when count(*) = count(date) then 'Not Active' else 'Active' end) as status
from t
group by personid, name;

I think Gordon's answer is better, but since I started typing this out, I figured i'd finish it anyway. 我认为戈登的答案更好,但是自从我开始输入答案以来,我认为我还是会完成它的。 something like 就像是

declare @maxDate date = datefromparts(9999, 12, 31)

select case when max(isnull(Date, @MaxDate)) = @maxDate) then 'Active' else 'inactive'
from ...

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

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