繁体   English   中英

SQL Case语句引用同一行中的不同列

[英]SQL Case statement with reference to different column in same row

我正在尝试编写一条select语句,以识别客户在过去3个月中是否多次注册,同时仍在提供我正在使用的表格中的所有数据(以下示例)

我正在想象案例查询应该看起来像这样,但我不知道如何引用我要在其中填充结果的行:

case when name in
(select name
from table1
where count(name from this row) > 1
and count(email from this row) > 1
and (date from this row) >= getdate()-120
end

该表有3个列:

name, email, date

有什么帮助吗?

如果您的数据库是SQL Server 2005或更高版本,则可以将查询编写为:

create table table1 (name varchar(20), email varchar(50), date datetime);
insert into table1 values ('Name1','email@abc.com',getdate()-30);
insert into table1 values ('Name1','email@abc.com',getdate()-60);
insert into table1 values ('Name1','email@abc.com',getdate());
insert into table1 values ('Name2','email2@abc.com',getdate()-20);
insert into table1 values ('Name3','email3@abc.com',getdate());
insert into table1 values ('Name4','email4@abc.com',getdate()-120);

with CTE as
(
select  
row_number() over (partition by [name],[email] order by [name] asc) as rownum,
[name],
[email],
[date]
from table1
where [date] > = GETDATE() - 120
)   
select * from CTE    
where rownum > = 3

暂无
暂无

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

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