473,788 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to select Highest 5 counts

2 New Member
Hi,

I have a table like the following:

TableName = Table1

DX1, DX1_DESC, DRG

The values from the 3 columns looks like the following

011.24 , abc , 79
434.91 , ytw , 79
434.91 , ytw , 79
574.71 , xyw , 79
574.60 , poi , 79
574.70 , ter , 70
011.04 , wsx , 79
011.24 , abc , 79
011.24 , abc , 79
59 , yyy , 79
59 , yyy , 79
574.71 , xyw , 79
574.71 , xyw , 79
574.71 , xyw , 79
574.60 , poi , 79
574.60 , poi , 79
574.60 , poi , 79
574.71 , xyw , 79
574.71 , xyw , 88
59 , yyy , 79
574.71 , xyw , 79
011.24 , abc , 79
011.24 , abc , 79
59 , yyy , 79
59 , yyy , 79

Now, how can I select the highest 5 count from the DX1 field and DRG=79 to get a result like the following below:

DX1 , DX1_DESC , CASES
574.71 ---------- xyw ---------- 6
59 ---------- yyy ---------- 5
011.24 ---------- abc ---------- 4
574.60 ---------- poi ---------- 3
434.91 ---------- ytw ---------- 2

I'm using the "------" to squeeze in space for the columns in this posting.
Thank you very much for your help.

-J
Feb 24 '07 #1
3 2002
jaimedp
2 New Member
Hi,

I just wrote a query that select all the records where DRG=79 and it also lists
the count for each code. Now I need to know how I can select the highest 5 codes and list them in DESC order. Here is the code I have so far:

SELECT A.Dx1, Count(*) AS [counter]
FROM table1 AS A
WHERE A.DRG=79
GROUP BY A.DC_Dx1;
Feb 24 '07 #2
ADezii
8,834 Recognized Expert Expert
Hi,

I have a table like the following:

TableName = Table1

DX1, DX1_DESC, DRG

The values from the 3 columns looks like the following

011.24 , abc , 79
434.91 , ytw , 79
434.91 , ytw , 79
574.71 , xyw , 79
574.60 , poi , 79
574.70 , ter , 70
011.04 , wsx , 79
011.24 , abc , 79
011.24 , abc , 79
59 , yyy , 79
59 , yyy , 79
574.71 , xyw , 79
574.71 , xyw , 79
574.71 , xyw , 79
574.60 , poi , 79
574.60 , poi , 79
574.60 , poi , 79
574.71 , xyw , 79
574.71 , xyw , 88
59 , yyy , 79
574.71 , xyw , 79
011.24 , abc , 79
011.24 , abc , 79
59 , yyy , 79
59 , yyy , 79

Now, how can I select the highest 5 count from the DX1 field and DRG=79 to get a result like the following below:

DX1 , DX1_DESC , CASES
574.71 ---------- xyw ---------- 6
59 ---------- yyy ---------- 5
011.24 ---------- abc ---------- 4
574.60 ---------- poi ---------- 3
434.91 ---------- ytw ---------- 2

I'm using the "------" to squeeze in space for the columns in this posting.
Thank you very much for your help.

-J
Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 5 Table1.DX1, Table1.DX1_DESC, Table1.DRG
  2. FROM Table1
  3. WHERE Table1.DRG=79
  4. ORDER BY Table1.DX1 DESC;
Feb 25 '07 #3
NeoPa
32,579 Recognized Expert Moderator MVP
Try this version :
Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 5 DX1,Counter
  2. FROM (SELECT Dx1,Count(*) AS [Counter]
  3.       FROM Table1
  4.       WHERE DRG=79
  5.       GROUP BY DX1) AS subQ
  6. ORDER BY [Counter] DESC
Feb 26 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
17982
by: Mark | last post by:
good spam subject ;). anyway, i'm alittle stumped. i'm in need of putting together a query that gets the next highest salary ( select max ( sal ) - 1?, from an emp_sal type table. another example is if i have a login_history table, and i'm logged in, but i want to display the date of my previous login. if the login_history table has user_id and login_date which makes up the primary key, how can i get the most recent login (minus the...
3
14562
by: William Wisnieski | last post by:
Hello Again, I'm really stuck on this one.....so I'm going to try a different approach to this problem. I have a query by form that returns a record set in a datasheet. The user double clicks on a row and a main form (pop up) opens bound to a table with a continuous subform bound to another table. On the main form is a field called . It is vital that this is
11
1954
by: Neo Geshel | last post by:
I have an Access DB, from which I am going to pull images. Each image has an associated ID, but the ID's are not necessarily sequential (some images may have been deleted, leaving gaps in the list of ID's). I am looking to be able to call an ID and its Image from the database, but also have returned the previous ID and the next ID, even when those actual ID's are not necessarily +1 and -1 to the ID being called. For example, I want to...
4
4896
by: johnk | last post by:
I have a table of items, with revision numbers. I need to extract the items with highest revision number. The items may be listed several times and I don't know what the highest revision number for each item is. How do I do this?
22
12495
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
6
1991
by: Matt Chwastek | last post by:
Anyone who can help, I am curretnly attempting to write some code that will allow iteration using a vector<intfrom the highest possilbe degree of a combination of ones & zeros (111, 110, 101, 011, 100, 010, 001, 000). The ordering of element containing the same number of ones is not important to the code, just the fact that the highest number of ones are iterated first. I would prefer not to use a binary string as eventually the
2
15952
by: fperri | last post by:
Hello, I have a query that creates a table of duplicates in my database. For one set of duplicates (three records), all the fields & values are the same except for the values in one field. This field is a numeric field. I need to figure out a way to remove all the duplicates except for the one with the highest value in that field. I don't want to have to create a module for this. I want to run it all from a stored query. I was thinking the...
0
1688
by: Carroll | last post by:
I would like to be able to select the maximum (highest ) value for a field, the 2nd highest, 3rd highest, etc. Are there any other functions that I might consider using with SQL? MAX will only pick up the highest and MIN the lowest value, but is there a way to pick up middle values, i.e., the 2nd, 3rd, etc, so I can list them in one row, along with a common key? Thanks, Carroll Rinehart
2
1127
by: runway27 | last post by:
hi i have an enquiry table which collects information about users making an online travel enquiry the fields in the table are = StoryTitle, EndCity, mode, PricedFrom, numAdults, numChildren, numInfants presently the select query is
0
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.