SQL Server selecting one record for a day -
i have query selects [orderdate] sites in last week. want able select 1 record given site in day. solution follows:
select sitename , orderdate table1 orderdate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
,but when @ results, notice date 21st site2 gives 2 records. need select one.
sitename..................orderdate site1....................2014-04-21 16:00:37.650 site2....................2014-04-21 16:00:39.697 site2....................2014-04-21 16:00:39.697 site3....................2014-04-21 16:00:35.180 site1....................2014-04-22 16:00:46.113 site2....................2014-04-22 16:00:50.817 site3....................2014-04-22 16:00:53.163 site1....................2014-04-23 16:00:50.993 site2....................2014-04-23 16:00:53.193 site3....................2014-04-23 16:00:55.727
** editing question **
hi, sorry complicated inserting distinct. included below entire query, included part of simplicity sake:
select distinct sitename ,sum(case when orderdate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690) 1 else 0 end ) completed table1 client in ('site1','site2','site3','site4','site5','site6','site7') , sitename != '(site8)' group sitename order sitename
i select 7 sites avoiding site 8 result
sitename..................completed
site1..........................3
site2..........................4
site3..........................3
site4..........................3
site5..........................3
site6..........................2
sit7...........................2
for site 2 count should 3 instead of 4 despite using distinct query still adding 2 records date
select distinct sitename , orderdate table1 orderdate >= dateadd(dd,(datediff(dd,-53690,getdate()-1)/7)*7,-53690)
Comments
Post a Comment