473,661 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rank within a category (Sql)

I have this sql that give me the sales rank from the highest to the
lowest. I have several categories and need to rank within each
category. h1.Cat is the categoryfield.

SELECT h1.Cat, h1.Oms, (SELECT COUNT(*)
FROM tblOms AS h2
WHERE h2.oms >= h1.oms) AS Rank
FROM tblOms AS h1
ORDER BY h1.Oms DESC;

Would it be like GROUP by h1.Cat some place.

Aug 25 '06 #1
3 5763

Helge's wrote:
I have this sql that give me the sales rank from the highest to the
lowest. I have several categories and need to rank within each
category. h1.Cat is the categoryfield.

SELECT h1.Cat, h1.Oms, (SELECT COUNT(*)
FROM tblOms AS h2
WHERE h2.oms >= h1.oms) AS Rank
FROM tblOms AS h1
ORDER BY h1.Oms DESC;

Would it be like GROUP by h1.Cat some place.
so where's the GROUP BY statement after the FROM clause?

Aug 25 '06 #2
"Helge's" <he************ *@plena.nowrote in message
<11************ *********@i3g20 00cwc.googlegro ups.com>:
I have this sql that give me the sales rank from the highest to the
lowest. I have several categories and need to rank within each
category. h1.Cat is the categoryfield.

SELECT h1.Cat, h1.Oms, (SELECT COUNT(*)
FROM tblOms AS h2
WHERE h2.oms >= h1.oms) AS Rank
FROM tblOms AS h1
ORDER BY h1.Oms DESC;

Would it be like GROUP by h1.Cat some place.
Hei igjen Helge!

Try something like this;

SELECT h1.Cat, h1.Oms, (SELECT COUNT(*)
FROM tblOms AS h2
WHERE h2.oms >= h1.oms AND
h2.Cat = h1.Cat) AS Rank
FROM tblOms AS h1
ORDER BY h1.Oms DESC;

--
Roy-Vidar
Aug 25 '06 #3
Helge's wrote:
I have this sql that give me the sales rank from the highest to the
lowest. I have several categories and need to rank within each
category. h1.Cat is the categoryfield.

SELECT h1.Cat, h1.Oms, (SELECT COUNT(*)
FROM tblOms AS h2
WHERE h2.oms >= h1.oms) AS Rank
FROM tblOms AS h1
ORDER BY h1.Oms DESC;

Would it be like GROUP by h1.Cat some place.
C.f.:

http://groups.google.com/group/comp....b6eb508f22fa9d

James A. Fortune
CD********@Fort uneJames.com

Aug 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4391
by: LRW | last post by:
I'm not exactly sure how to even ask the question, and I know my terminology is not good as I'm a SQL beginner, but, here goes. I need to find a way to make an if statement within an array...or, the "while" portion of a recordset. The best way I can ask is show what I mean. http://oscarguy.mechphisto.net/awardbrowse.php If you go there and select an award (like Best Picture), leave the year field alone, and select YES and submit,...
3
2160
by: bughunter | last post by:
I discover next problem I have view definition with rank() create view vTEST as select c1, c2, c3, ... -- problem area start , rank() over (order by c3) as RNK -- problem area stop from table
13
5036
by: Steve Edwards | last post by:
Hi, Given a map: typedef map<long, string, greater<long> > mapOfFreq; Is there a quicker way to find the rank (i.e. index) of the elememt that has the long value of x? At the moment I'm iterating through the map and keeping count of when I hit it.
3
2172
by: Wayne Wengert | last post by:
I have a VS2005/VB Windows application in which I want to rank the contents of a "total" field in a dataset but I can't seem to figure out how to do this. I've created an SQL DataAdapter and an SQL Dataset and I filled the dataset with the values needed. The dataset has "n" rows and the columns include "total" (a decimal value) and "rank" (an integer initially set to zero). I want to place the ranking value (e.g. 1 through n) for each value...
2
3385
by: LizzyFin | last post by:
Looking for a little assistance in understanding what I am missing! Using PHP/MySql to allow users to browse through items in a gallery. Clicking 'next' and 'previous' links works fine using the item ID number to sequentially see all items. Trouble arises when category and ID variables are passed. I'm thoroughly confused on how to let the user click a 'next' link after they have entered a category page in order to see the next item in that...
3
6557
by: max | last post by:
Let's say I have a table with the following fields. State, Customer, and Total Sales. What I want to do is rank each customer by sales volume by state. i.e. highest sales is ranked 1, second highest ranked 2, etc within each state. I think I could do this in a cumbersome fashion by using the max function, appending, deleting and using the max function again for #2 and so on. I was wondering if there is a built in function or a piece...
1
2561
by: yingwen | last post by:
I have a xml similar like this: <Member ID="123"> <DateBorrowed>11-01-2006</DateBorrowed> <Book ID="222" Title="ABC"> <Category> Fiction</Category> </Book> <Book ID="333" Title="ABu">
6
2556
by: sstidham | last post by:
I have a query which calculates the data from two table and then yields the number of points based on various calculations for each category. Essentially we are trying to rank our Agents based on performance, and each category (Senority, Sales, occurances etc) has a points value. We have a final query that combines all these points values for the different categories to yeild the final total. What I need is a query that will show the...
6
5637
by: vegetable21 | last post by:
Hi All, I'll try and explain as best as possible what I'm trying to do and hopefully someone can tell me the best way to do it! I'm basically trying to rank by 2 columns in mysql/php, by this i mean i have a column called `Turnover` and one of `Cash` I would like to display each column in a table with its ranking in the column to the right. So far i have: include ("database.php");
0
8851
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
8754
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
8542
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
8630
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
7362
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...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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();...
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
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.