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

Getting the Non Aggregate value in A Group By TSQL with ou..

 
   Database Help (Home) -> Programming RSS
Next:  determining or producing field names & defini..  
Author Message
sp

External


Since: Apr 29, 2008
Posts: 27



(Msg. 1) Posted: Thu Feb 12, 2009 3:14 pm
Post subject: Getting the Non Aggregate value in A Group By TSQL with out Aggr F
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Hi,

Please help me how to get the Non Aggregate value in A Group By TSQL with
out Aggr Function.

Eg:

create table #temp(rng int,marketdate int,price float)
go
insert #temp values(0,20060821, 79.87999)
insert #temp values(0,20060822, 79.549988)
insert #temp values(0,20060823, 79.469986)
insert #temp values(0,20060824, 79.5)
insert #temp values(0,20060825, 80)

select max(marketdate) from #temp group by rng

I need the corresponding Price for that marketdate. I have millions of
records in this table. Please help me this query.

One way i can get is by using a derrived/temp table for above and join it
with #temp and get the corresponding price. Please let me know , if there is
any other way to do it.

Appreciate you help!!!

 >> Stay informed about: Getting the Non Aggregate value in A Group By TSQL with ou.. 
Back to top
Login to vote
Erland Sommarskog2

External


Since: May 30, 2004
Posts: 2060



(Msg. 2) Posted: Thu Feb 12, 2009 3:33 pm
Post subject: Re: Getting the Non Aggregate value in A Group By TSQL with out Aggr F [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

sp (sp@discussions.microsoft.com) writes:
> Please help me how to get the Non Aggregate value in A Group By TSQL with
> out Aggr Function.
>
> Eg:
>
> create table #temp(rng int,marketdate int,price float)
> go
> insert #temp values(0,20060821, 79.87999)
> insert #temp values(0,20060822, 79.549988)
> insert #temp values(0,20060823, 79.469986)
> insert #temp values(0,20060824, 79.5)
> insert #temp values(0,20060825, 80)
>
> select max(marketdate) from #temp group by rng
>
> I need the corresponding Price for that marketdate. I have millions of
> records in this table. Please help me this query.
>
> One way i can get is by using a derrived/temp table for above and join
> it with #temp and get the corresponding price. Please let me know , if
> there is any other way to do it.

The most efficient way to do it, is likely to be this:

WITH numbereddates AS (
SELECT rng, marketdate, price,
rowno = row_number() OVER(PARTITION BY rng ORDER BY marketdate
DESC)
FROM #temp
)
SELECT rng, marketdate, price
FROM numbereddates
WHERE rowno = 1


But you need SQL 2005 for this; the query dows not run on SQL 2000.



--
Erland Sommarskog, SQL Server MVP, esquel.RemoveThis@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 >> Stay informed about: Getting the Non Aggregate value in A Group By TSQL with ou.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
not contained in the aggregate or GROUP BY..problem with D.. - If someone could help, I'd appreciate it. I do not know my SQL programming all that well, so I'll try to explain. Below is a query (a view actually) that is to pull up data on inventory items. Now, I thought that this would work but, when I run it, I...

Column X is invalid in the select list ...not contained in.. -

Error: is invalid in the select list because it is not con.. - Hi, I have a stored procedure and when I execute it, I always get the following error: "Column 'DistributionPrintReceiptInstance.DistributionCommonInstanceID' is invalid in the select list because it is not contained in either an aggregate functio...

select/isolate group from... where max(date in group) = so.. - 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...

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..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
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 ]