Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

select/isolate group from... where max(date in group) = so..

 
   Database Help (Home) -> Programming RSS
Next:  Change Service Account Password  
Author Message
Rich

External


Since: Feb 07, 2008
Posts: 112



(Msg. 1) Posted: Mon Jul 07, 2008 9:06 am
Post subject: select/isolate group from... where max(date in group) = somedate?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

In the following data I need to select (or isolate) the codes (code groups)
which contain a max codeDate of '12/31/08'. Of the codes a, b, c, d -- only
codes b and c contain a max codeDate of '12/31/08' in their respective
groups. What would be the tsql to isolate these 2 codes (groups)?

create table #tmpA (code varchar(1), codeDate datetime)

insert into #tmpA
select 'a', '12/31/03'
union all select 'a', '12/31/04'
union all select 'a', '12/31/05'
union all select 'b', '12/31/04'
union all select 'b', '12/31/05'
union all select 'b', '12/31/06'
union all select 'b', '12/31/07'
union all select 'b', '12/31/08'
union all select 'c', '12/31/06'
union all select 'c', '12/31/07'
union all select 'c', '12/31/08'
union all select 'd', '12/31/04'
union all select 'd', '12/31/05'
union all select 'd', '12/31/06'
union all select 'd', '12/31/07'

Thanks,
Rich

 >> Stay informed about: select/isolate group from... where max(date in group) = so.. 
Back to top
Login to vote
Rich

External


Since: Feb 07, 2008
Posts: 112



(Msg. 2) Posted: Mon Jul 07, 2008 9:09 am
Post subject: RE: select/isolate group from... where max(date in group) = somedate? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Never mind. I figured it out:

select code from #tmpa
GROUP BY code HAVING max(codedate) = '12/31/08'

I just didn't have my thinking cap on this morning.


"Rich" wrote:

> In the following data I need to select (or isolate) the codes (code groups)
> which contain a max codeDate of '12/31/08'. Of the codes a, b, c, d -- only
> codes b and c contain a max codeDate of '12/31/08' in their respective
> groups. What would be the tsql to isolate these 2 codes (groups)?
>
> create table #tmpA (code varchar(1), codeDate datetime)
>
> insert into #tmpA
> select 'a', '12/31/03'
> union all select 'a', '12/31/04'
> union all select 'a', '12/31/05'
> union all select 'b', '12/31/04'
> union all select 'b', '12/31/05'
> union all select 'b', '12/31/06'
> union all select 'b', '12/31/07'
> union all select 'b', '12/31/08'
> union all select 'c', '12/31/06'
> union all select 'c', '12/31/07'
> union all select 'c', '12/31/08'
> union all select 'd', '12/31/04'
> union all select 'd', '12/31/05'
> union all select 'd', '12/31/06'
> union all select 'd', '12/31/07'
>
> Thanks,
> Rich

 >> Stay informed about: select/isolate group from... where max(date in group) = so.. 
Back to top
Login to vote
Steve

External


Since: Jan 11, 2008
Posts: 36



(Msg. 3) Posted: Mon Jul 07, 2008 9:41 am
Post subject: RE: select/isolate group from... where max(date in group) = someda [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

seems you're making it harder than you have to. There's no need for a group
by or Max() funciont.

SELECT Code
FROM #Tmpa
WHERE CodeDate = '20081231'



"Rich" wrote:

> Never mind. I figured it out:
>
> select code from #tmpa
> GROUP BY code HAVING max(codedate) = '12/31/08'
>
> I just didn't have my thinking cap on this morning.
>
>
> "Rich" wrote:
>
> > In the following data I need to select (or isolate) the codes (code groups)
> > which contain a max codeDate of '12/31/08'. Of the codes a, b, c, d -- only
> > codes b and c contain a max codeDate of '12/31/08' in their respective
> > groups. What would be the tsql to isolate these 2 codes (groups)?
> >
> > create table #tmpA (code varchar(1), codeDate datetime)
> >
> > insert into #tmpA
> > select 'a', '12/31/03'
> > union all select 'a', '12/31/04'
> > union all select 'a', '12/31/05'
> > union all select 'b', '12/31/04'
> > union all select 'b', '12/31/05'
> > union all select 'b', '12/31/06'
> > union all select 'b', '12/31/07'
> > union all select 'b', '12/31/08'
> > union all select 'c', '12/31/06'
> > union all select 'c', '12/31/07'
> > union all select 'c', '12/31/08'
> > union all select 'd', '12/31/04'
> > union all select 'd', '12/31/05'
> > union all select 'd', '12/31/06'
> > union all select 'd', '12/31/07'
> >
> > Thanks,
> > Rich
 >> Stay informed about: select/isolate group from... where max(date in group) = so.. 
Back to top
Login to vote
Eric Isaacs

External


Since: May 13, 2008
Posts: 367



(Msg. 4) Posted: Mon Jul 07, 2008 10:00 am
Post subject: Re: select/isolate group from... where max(date in group) = someda [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Jul 7, 9:41 am, Steve wrote:
> seems you're making it harder than you have to.  There's no need for a group
> by or Max() funciont.

But if you changed the date to '12/31/2007', his logic could find the
ones with a max of '12/31/2007' (code d) whereas your logic would find
any with '12/31/2007'.
 >> Stay informed about: select/isolate group from... where max(date in group) = so.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
select rowcount per group before value in group = null - I need to select the top 3 rows per each group (xID -- order by xID). No problem here. But I need to select the count of rows in each group before a null value is encountered in the stat column -- ordering by xID CREATE TABLE #tmp1(rowID int..

SELECT GROUP BY smalltimedate - Hi, I have an access query that I need to convert to Sql Server. It looks like: "SELECT Top 20 Count(MessageID) AS CountOfMessages, DateValue(datestamp) AS Expr1 From Messages GROUP BY DateValue (datestamp) ORDER BY DateValue(datestamp) DESC;&quo...

Using a datetime value in a select by group by the month - Hi, I have a table with sales data, and each transaction is dated, into a datetime field that is populated with the time of sale. What I want to do though is group all sales for reporting, but where I show the total for by months. How do i select al...

select count of rows per group - I need to select the count of the first 3 rows per each group (xID) up to a count of 3 rows (top 3 rows - order by column x1) where the stat column is not null. if a group contains more than 3 rows where stat is not null - ignore the extra rows - onl...

Group by on date time column - Hi i have a date time column date_read in which date as well as time also stores. iwant to group by on that column but due to date in that column it returns me more rows than it should for each day. how can i group by only on date thanx Jami *** Sent...
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]