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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to get sample data of each Group By type?

Author  Topic 

lprassanth
Starting Member

3 Posts

Posted - 2009-02-19 : 04:21:49
Hi,
This is the problem I face. Any help greatly appreciated :)
Table T:
Column A Column B
1 1
1 2
1 3
2 4
2 5
2 6
3 5
3 4
3 3

Let's say that the above is what i have in my table. This is a simplified version and the real table has millions of records. Now I need to get 2 sample records from the table for every value of column A. For more clarity...

Eg:
select A, B, Count(*)
From T
Where A = '1'
Group By 1, 2
Sample 2;

This will only get me 2 samples from the table, when A = 1.
I would need 2 samples for each value of A (i.e. for value 1, 2, and 3) - which translates into returning 6 rows.
Could this be done in a single query?

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-19 : 04:24:09
http://sqlblogcasts.com/blogs/madhivanan/archive/2008/09/12/return-top-n-rows.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-19 : 04:26:03
select * from (select row_number(partition by columna order by columna)as rid,* from tablet)t where rid <=2
Go to Top of Page

lprassanth
Starting Member

3 Posts

Posted - 2009-02-19 : 04:58:15
@ the above 2 replies - will it work in Teradata?
Thanks both of you..


Go to Top of Page

lprassanth
Starting Member

3 Posts

Posted - 2009-02-19 : 05:20:08
@ Madhivanan

Thanks a ton for the link..
i still don't quite get the req. result in Teradata..
for solution 1) Teradata throws an error that "Top N" is not supported in a sub query
and as for solution 3)i dont think row_number is supported in Teradata either :(

for once i despise Teradata :)
any other alternatives...?
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-02-19 : 05:42:45
Doesn't Teradata have a sql engine of its own ?? The queries here might/might not work there. Might as well post this in a Teradata forum.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-20 : 02:47:18
quote:
Originally posted by lprassanth

@ the above 2 replies - will it work in Teradata?
Thanks both of you..





Post your question at Teradata forum

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-20 : 02:55:53
quote:
Originally posted by bklr

select * from (select row_number(partition by columna order by columna)as rid,* from tablet)t where rid <=2


It is point 3 on the link

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-20 : 03:29:56
quote:
Originally posted by madhivanan

quote:
Originally posted by bklr

select * from (select row_number(partition by columna order by columna)as rid,* from tablet)t where rid <=2


It is point 3 on the link

Madhivanan
Failing to plan is Planning to fail


hai madhivanan,
i haven't seen the link
i just given the query .....
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-20 : 04:55:15
quote:
Originally posted by bklr

quote:
Originally posted by madhivanan

quote:
Originally posted by bklr

select * from (select row_number(partition by columna order by columna)as rid,* from tablet)t where rid <=2


It is point 3 on the link

Madhivanan
Failing to plan is Planning to fail


hai madhivanan,
i haven't seen the link
i just given the query .....



No problem

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -