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.
| 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! ThanksQUERYselect TRANSACTION_TYPE, count(*) DOCUMENT_COUNTfrom HBG_KPI_ECOMMERCE_STATISTICSgroup by TRANSACTION_TYPEorder by TRANSACTION_TYPERESULTS FROM QUERYTransaction_TYPE DOCUMENT_COUNT810 2 850 3 852 1 855 1 |
|
|
Dasman
Yak Posting Veteran
79 Posts |
Posted - 2011-06-30 : 14:18:31
|
| Select TOP 2 * FROM TableNameORDER BY Transaction_Type DESCDasman==========================Pain is Weakness Leaving the Body. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-06-30 : 14:27:13
|
select top 2 * from(select TRANSACTION_TYPE, count(*) DOCUMENT_COUNTfrom HBG_KPI_ECOMMERCE_STATISTICSgroup by TRANSACTION_TYPE)dtorder by DOCUMENT_COUNT DESC No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|