2014-02-28 78 views
0

我正在嘗試編寫一個select語句,該語句確定客戶在過去3個月中是否簽署了多次,同時仍提供表中的所有數據參考同一行中不同列的SQL案例聲明

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 

表有三:從(下面的例子)

我拍攝的情況下查詢應該是這個樣子,但我不知道如何引用我想要的結果來填充行工作colums:

name, email, date 

有什麼幫助嗎?謝謝。

+1

請提供一些示例數據和預期的輸出。 – TechDo

回答

0

如果你的數據庫是SQL Server 2005或以上,那麼你可以編寫一個查詢爲:

create table table1 (name varchar(20), email varchar(50), date datetime); 
insert into table1 values ('Name1','[email protected]',getdate()-30); 
insert into table1 values ('Name1','[email protected]',getdate()-60); 
insert into table1 values ('Name1','[email protected]',getdate()); 
insert into table1 values ('Name2','[email protected]',getdate()-20); 
insert into table1 values ('Name3','[email protected]',getdate()); 
insert into table1 values ('Name4','[email protected]',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