Shivsa,
First of all, t-sql does not offer 'limit' in order by clause. Secondly, there are columns not in group by such as task_type and caller_id.
Thanks for your quick response.
GT
Main Topics
Browse All TopicsI have a table with many columns, and here are the relevant ones:
group_id
user_name
task_id
task_type
action_dt_tm
caller_id
...
Each user will perform many tasks in a day. What I need to know is the most recent tasks (the most recent 2, 3, or 4 tasks) performed by each user in each group with all the other information along with the tasks. So what I need to get is top n records sorted by action_dt_tm desc by group_id and user_name.
T-SQL offers 'top n' but does not allow group by. The following SQL statement does not work since many columns are not in group by clauses
Select * from all_action
group by group_id, user_name
order by group_id, user_name, action_dt_tm
I am looking for solution in t-sql and also willing to accept solution in PL/SQL.
Thanks.
GT
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
geotiger,
Given the table T1,
the query will look like this:
select t1.*
from t1 join
(select a.group_id, a.user_name, a.action_dt_tm from
(select group_id, user_name, action_dt_tm from t1) a
join
(select group_id, user_name, action_dt_tm from t1) b
on a.group_id=b.group_id and a.user_name=b.user_name and a.action_dt_tm <= b.action_dt_tm
group by a.group_id, a.user_name, a.action_dt_tm having count(*) <= 1) x
on x.group_id=t1.group_id and x.user_name=t1.user_name and x.action_dt_tm=t1.action_d
order by t1.group_id, t1.user_name, t1.action_dt_tm desc
Rgds.
VC
geotiger,
Sorry pressed 'Send' too fast...
The parameter for the top records should be specified in the 'having count(*)' clause.
E.g. having count(*) <= 4/8/etc. for the first four and eight records. The big subquery determines a combination of (user_name, group_id and action_dt_tm) for the first N records and the result is joined with the original T1 table getting you all the columns from T1. N is the parameter for 'having count(*)<= N.
Rgds.
VC
CORRECTION (in subquery WHERE clause, the first yt2.group_id should be yt1.group_id):
SELECT *
FROM yourTable yt1
WHERE action_dt_tm IN (
SELECT TOP 4 action_dt_tm
FROM yourTable yt2
WHERE yt1.[user_name] = yt2.[user_name]
AND yt1.group_id = yt2.group_id
ORDER BY action_dt_tm DESC )
ORDER BY group_id, user_name, action_dt_tm
VC:
Won't the query above work?
Isn't the "having count(*) <= 1" going to prevent your query from returning results?
geotiger,
Sorry , I reversed the comparison in my secnd version. It should be like this:
select * from (
select x.*,
(select count(*) from t1 where group_id=x.group_id and user_name=x.user_name and action_dt_tm >= x.action_dt_tm) cnt
from t1 x)
where cnt <= 1
@ScottPletcher,
Your query is also fine... It will work.
VC
we could use RANK() Partiion By for Selecting TOP N rows by group..
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.
Definition from Microsoft
Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.
Syntax
CODE
RANK ( ) OVER ( [ < partition_by_clause > ] < order_by_clause > )
Example
CODE
USE AdventureWorks;
GO
SELECT i.ProductID, p.Name, i.LocationID, i.Quantity
,RANK() OVER
(PARTITION BY i.LocationID ORDER BY i.Quantity DESC) AS 'RANK'
FROM Production.ProductInventor
INNER JOIN Production.Product p
ON i.ProductID = p.ProductID
ORDER BY p.Name;
We also have DENSE_RANK method which is almost similar to RANK but it does not provide the GAPS between the Rank whereas other one does.
Example
http://www.sqlservercurry.
One of the best example for Rank is here
http://weblogs.sqlteam.com
http://forum.only4gurus.or
Business Accounts
Answer for Membership
by: shivsaPosted on 2003-12-19 at 08:35:04ID: 9973156
Select * from all_action
group by group_id, user_name
order by group_id, user_name, action_dt_tm limit n
where n could be your number for top record like 10/20..