Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
bobbyGratton
Starting Member
1 Post |
Posted - 2009-06-01 : 13:12:53
|
| Using MS SQL Server 2005.I need help formulating a SQL query,I have a table called "leads" and I need to select a particular group of records from.Here's what i need: -- Select all records that have the value of '5' in the "type" field (int), this is easy even for a noob :) -- Also, I need to grab only uniques by "email" field. (I'm expecting that multiple groups of 2 or 3 records will have the same email address) -- But, I need to grab only the LAST record to go in by email address. I have a date field called "created"Thanks a lot for your help guys! I appreciate it. |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-06-01 : 13:27:52
|
| try thisdeclare @test table ( id int identity(1,1), Name varchar(30),[type] int, email varchar(30),created datetime )insert into @testselect 'Peter',5,'abc@zzz.com','5/1/2009' union allselect 'John',6,'rty@uuu.com','5/1/2009' union allselect 'Kelvin',5,'abc@zzz.com','6/1/2009' select t.name,t.emailfrom (select row_number() over( partition by [type] order by created desc ) as sno ,email,namefrom @testwhere [type] = '5')twhere t.sno = 1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-06-01 : 13:50:21
|
if sql 2000 or earlierSELECT t.*FROM table tINNER JOIN (SELECT email,MAX(created) AS last FROM table WHERE t.type=5 GROUP BY email)t1ON t1.email=t.emailAND t1.last=t.created |
 |
|
|
|
|
|
|
|