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
 General SQL Server Forums
 New to SQL Server Programming
 Top X values from a Column?

Author  Topic 

npohl
Starting Member

1 Post

Posted - 2011-06-30 : 14:11:47
Hello,

I'm pretty new to sql development and I'm trying to add up all matching rows in a column and then report only the top 10 from the table (In the below example I'll use the top 2).

My column is called TRANSACTION_TYPE and is populated with multiple rows that can include values (850, 810, 855, 852). My initial code below works to count all the individual records and then groups them accordingly. From the results of the query below you can see there are
2 rows containing 810 and 3 rows containing 850. I'm not sure how to go about getting the query to show the top 2 transaction types and omit the remaining. Any help would be appreciated! Thanks

QUERY

select TRANSACTION_TYPE, count(*) DOCUMENT_COUNT
from HBG_KPI_ECOMMERCE_STATISTICS
group by TRANSACTION_TYPE
order by TRANSACTION_TYPE

RESULTS FROM QUERY

Transaction_TYPE DOCUMENT_COUNT
810 2
850 3
852 1
855 1

Dasman
Yak Posting Veteran

79 Posts

Posted - 2011-06-30 : 14:18:31
Select TOP 2 * FROM TableName
ORDER BY Transaction_Type DESC

Dasman

==========================
Pain is Weakness Leaving the Body.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-06-30 : 14:27:13
select top 2 * from
(select TRANSACTION_TYPE, count(*) DOCUMENT_COUNT
from HBG_KPI_ECOMMERCE_STATISTICS
group by TRANSACTION_TYPE
)dt
order by DOCUMENT_COUNT DESC


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -