sql server 2008 - Query for only one client entry a day -
i have table definition this:
create table [dbo].[contact] ( clientnumber [numeric] (20, 0) null, contactdate [date] null, name [text] null, additionalinformation [text] null )
this table filled information single month. each client may have multiple entries single day. meaning there might entries this:
42 2015-12-09 felix called today 42 2015-12-09 fexclicx ...
my goal client results table condition there should 1 entry day per client number. not care of (here) 2 entries displayed, top 1
.
so far tried achive distinct:
select distinct clientnumber, contactdate, * contact
unfortunately not limit results show client entry specific date once. still appear.
how achive getting entries 1 entry 1 client per day?
i suggest edit table, has id column ( set primary key). if so, can use query i've posted.
i don't know how desire information group by, can't aggregate text columns.
create table [dbo].[#contact] ( id int identity(1,1), clientnumber [numeric] (20, 0) null, contactdate [date] null, name [text] null, additionalinformation [text] null ) declare @date datetime set @date = getdate() insert #contact values (1,@date,'first client','info 1') go declare @date datetime set @date = getdate() set @date = dateadd(day,rand()*100,@date) insert #contact values (1,@date,'first client','info 2') go declare @date datetime set @date = getdate() set @date = dateadd(day,rand()*100,@date) print @date insert #contact values (2,@date,'2nd client','info 1') go declare @date datetime set @date = getdate() set @date = dateadd(day,rand()*100,@date) insert #contact values (2,@date,'2nd client','info 2') go declare @date datetime set @date = getdate() set @date = dateadd(day,rand()*100,@date) insert #contact values (2,@date,'2nd client','info 3') go declare @date datetime set @date = getdate() set @date = dateadd(day,rand()*100,@date) insert #contact values (3,@date,'3rd client','info 1') select * #contact c1 join ( select clientnumber, max(id) maxid #contact group clientnumber ) c2 on c1.clientnumber = c2.clientnumber , c1.id = c2.maxid
Comments
Post a Comment