473,839 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Limit rows returned

7 New Member
Hi all,
My English is not the best so it will be best if I show you an example:
I have a query:
Expand|Select|Wrap|Line Numbers
  1.  SELECT Make as [MAKE], Model as [MODEL], DERIVATIVE, [PRICE]
  2. from MY_VIEW
and here is what it returns:
[HTML]
MAKE MODEL DERIVATIVE PRICE
==== ======= ========= =====
FORD MONDEO HATCHBACK SPEC EDS 2.0 Edge 5dr 8250.00
FORD MONDEO HATCHBACK SPEC EDS 2.0 Edge 5dr 8250.00
FORD RANGER DIESEL Pick Up Double Cab TD 4WD 6168.75
FORD RANGER DIESEL Pick Up XLT Super TD 4WD 7696.25
FORD RANGER DIESEL Pick Up Double Cab TD 4WD 6462.50
FORD RANGER DIESEL Pick Up Double Cab TD 4WD 7167.50
FORD RANGER DIESEL Pick Up Double Cab TD 4W 6374.38
FORD TRANSIT 260 SWB DIESEL FWD Low Roof Van TDi 85ps 2.2 6521.25
FORD TRANSIT 260 SWB DIESEL FWD Low Roof Van TDi 85ps 2.2 7784.38
FORD TRANSIT 260 SWB DIESEL FWD Low Roof Van TDi 85ps 2.2 7872.50
FORD TRANSIT 260 SWB DIESEL FWD Low Roof Van TDi 85ps 2.2 6961.88
FORD TRANSIT 260 SWB DIESEL FWD Low Roof Van TDi 85ps 2.2 6315.63
FORD TRANSIT 280 LWB DIESEL RWD Medium Roof Van TDi 90ps 6345.00
FORD TRANSIT 280 LWB DIESEL RWD Medium Roof Van TDi 90ps 9517.50
FORD TRANSIT 350 LWB DIESEL RWD Medium Roof Van TDCi 100ps 13512.50
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 10398.75
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 9165.00
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 8577.50
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 7138.13
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 8783.13
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 7020.63
[/HTML]
Can any of you guys let me know what would be the easiest way to limit the results returned to display only say top 3 cars of the same model. ie: only 3 of TRANSIT 350 LWB DIESEL RWD models?

Thanks in advance.
Apr 9 '08 #1
14 2463
ck9663
2,878 Recognized Expert Specialist
Hi all,
My English is not the best so it will be best if I show you an example:
I have a query:
Expand|Select|Wrap|Line Numbers
  1.  SELECT Make as [MAKE], Model as [MODEL], DERIVATIVE, [PRICE]
  2. from MY_VIEW
and here is what it returns:
[HTML]
MAKE MODEL DERIVATIVE PRICE
==== ======= ========= =====
FORD MONDEO HATCHBACK SPEC EDS 2.0 Edge 5dr 8250.00
FORD MONDEO HATCHBACK SPEC EDS 2.0 Edge 5dr 8250.00
FORD RANGER DIESEL Pick Up Double Cab TD 4WD 6168.75
FORD RANGER DIESEL Pick Up XLT Super TD 4WD 7696.25
FORD RANGER DIESEL Pick Up Double Cab TD 4WD 6462.50
FORD RANGER DIESEL Pick Up Double Cab TD 4WD 7167.50
FORD RANGER DIESEL Pick Up Double Cab TD 4W 6374.38
FORD TRANSIT 260 SWB DIESEL FWD Low Roof Van TDi 85ps 2.2 6521.25
FORD TRANSIT 260 SWB DIESEL FWD Low Roof Van TDi 85ps 2.2 7784.38
FORD TRANSIT 260 SWB DIESEL FWD Low Roof Van TDi 85ps 2.2 7872.50
FORD TRANSIT 260 SWB DIESEL FWD Low Roof Van TDi 85ps 2.2 6961.88
FORD TRANSIT 260 SWB DIESEL FWD Low Roof Van TDi 85ps 2.2 6315.63
FORD TRANSIT 280 LWB DIESEL RWD Medium Roof Van TDi 90ps 6345.00
FORD TRANSIT 280 LWB DIESEL RWD Medium Roof Van TDi 90ps 9517.50
FORD TRANSIT 350 LWB DIESEL RWD Medium Roof Van TDCi 100ps 13512.50
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 10398.75
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 9165.00
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 8577.50
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 7138.13
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 8783.13
FORD TRANSIT 350 LWB DIESEL RWD High Roof Van TDi 90ps 7020.63
[/HTML]
Can any of you guys let me know what would be the easiest way to limit the results returned to display only say top 3 cars of the same model. ie: only 3 of TRANSIT 350 LWB DIESEL RWD models?

Thanks in advance.
use filter conditions

WHERE MODEL = 'TRANSIT 350 LWB DIESEL RWD' to filter out your result. Here's the full WHERE syntax. And here's more about search condition.

To reduce the number of rows returned, use the TOP clause. Here's more about the TOP clause.

-- CK
Apr 9 '08 #2
Delerna
1,134 Recognized Expert Top Contributor
Try this. So long as your table dosn't hold huge amounts of data it should run reasonably well for you

Expand|Select|Wrap|Line Numbers
  1. SELECT Make,Model,Derivative,Price
  2. FROM
  3. (   SELECT a.Make,a.Model,a.Derivative,a.Price,
  4.              (   SELECT Count(Price) 
  5.                  FROM YourTable b 
  6.                  WHERE a.Make=b.make 
  7.                    AND a.model=b.model 
  8.                    AND a.derivative=b.derivative 
  9.                    AND a.price>b.price
  10.              )+1 as Pos
  11.     FROM YourTable a
  12. )a
  13. where pos<4
  14.  
Apr 10 '08 #3
ck9663
2,878 Recognized Expert Specialist
I missed that part. My bad.

-- CK
Apr 10 '08 #4
adrianga
7 New Member
Thanks guys,
I do have a basic idea on how to use filter conditions and 'top' clause but I couldn't figure out how to filter the results in the way described by me earlier.
@Delerna doesn't seem to be doing exactly what I would want - it still displays more than 3 same models of cars. It does seem to be limiting the results returned though. I don't understand why are you counting the price in one of the subselects? Would you be able to explain that to me in some simple way ? Thanks!
Apr 10 '08 #5
Delerna
1,134 Recognized Expert Top Contributor
The intent is to return a number that identifies the rank of a price within a set of prices for a given make model and derivative.

What it does is count the number of prices that are less than the price that a record has. So
for the higest price the count will be 0 prices that are higher
for the second highest price the count will be 1 price that is higher
for the third higest price the count will be 2 prices that are higher
....etc

try running a query from the subquery part and you will see what it does
Expand|Select|Wrap|Line Numbers
  1.   SELECT a.Make,a.Model,a.Derivative,a.Price,
  2.              (   SELECT Count(Price) 
  3.                  FROM YourTable b 
  4.                  WHERE a.Make=b.make 
  5.                    AND a.model=b.model 
  6.                    AND a.derivative=b.derivative 
  7.                    AND a.price>b.price
  8.              )+1 AS Pos
  9.     FROM YourTable a
  10.  
Apr 11 '08 #6
adrianga
7 New Member
The intent is to return a number that identifies the rank of a price within a set of prices for a given make model and derivative.

What it does is count the number of prices that are less than the price that a record has. So
for the higest price the count will be 0 prices that are higher
for the second highest price the count will be 1 price that is higher
for the third higest price the count will be 2 prices that are higher
....etc

try running a query from the subquery part and you will see what it does
Expand|Select|Wrap|Line Numbers
  1.   SELECT a.Make,a.Model,a.Derivative,a.Price,
  2.              (   SELECT Count(Price) 
  3.                  FROM YourTable b 
  4.                  WHERE a.Make=b.make 
  5.                    AND a.model=b.model 
  6.                    AND a.derivative=b.derivative 
  7.                    AND a.price>b.price
  8.              )+1 AS Pos
  9.     FROM YourTable a
  10.  
Thanks!
This works great..but :)... what do I do when there is few of the same models with the same price? They all get the same position unfortunately :(
Also it will only allow me to limit the results by top x of derivative, I would like to limit it to top x of model - so if there are 5 ford transit LWB and 5 ford transit RWB I would only like to display top 3 of ford (make) transit (model).
I hope you know what I am saying @Delerna?
I am sure we are heading in the right direction though - as this is exactly what I was thinking, just a 'simple' query, no cursors.
But if it can't be done without cursors than maybe you have an advice to me how to do this wiuth cursors than?
Apr 21 '08 #7
Delerna
1,134 Recognized Expert Top Contributor
The model field looks a little ugly to do the top 3 make/model.
There is one hope that I can think of that it can be achieved

In these the model you want to group by is TRANSIT
TRANSIT 350 LWB DIESEL RWD
TRANSIT 260 SWB DIESEL FWD

and in this the model you want to group by is RANGER
RANGER DIESEL

and in this the model you want to group by is MONDEO
MONDEO HATCHBACK SPEC EDS

There is a patern in the above examples that can be used to extract the model for use with the price ordering.

The pattern is, everything to the left of the first space is the model
If that pattern remains true for all records then I can show you how to do it.

As for the situation where there are two or more prices that are the same....hmmm
I'll have to think about that one, not sure if there is a way around it or not.
Apr 23 '08 #8
adrianga
7 New Member
Thanks again for trying. I have a few friends of mine thinking about this as we all believe this is possible to do with a Select statement. But I think here I didn't explain clearly what I want to achieve:
Having table that looks like this:
Expand|Select|Wrap|Line Numbers
  1. MAKE      MODEL                        DERIVATIVE                PRICE
  2. ====      =======                       =========                 =====
  3. FORD    MONDEO HATCHBACK SPEC EDS   2.0 Edge 5dr                 8250.00
  4. FORD    MONDEO HATCHBACK SPEC EDS   2.0 Edge 5dr                 8250.00
  5. FORD    RANGER DIESEL               Pick Up Double Cab TD 4WD  6168.75
  6. FORD    RANGER DIESEL               Pick Up XLT Super TD 4WD  7696.25
  7. FORD    RANGER DIESEL               Pick Up Double Cab TD 4WD  6462.50
  8. FORD    RANGER DIESEL               Pick Up Double Cab TD 4WD  7167.50
  9. FORD    RANGER DIESEL               Pick Up Double Cab TD 4W  6374.38
  10. FORD    TRANSIT 260 SWB DIESEL FWD  Low Roof Van TDi 85ps 2.2 6521.25
  11. FORD    TRANSIT 260 SWB DIESEL FWD  Low Roof Van TDi 85ps 2.2 7784.38
  12. FORD    TRANSIT 260 SWB DIESEL FWD  Low Roof Van TDi 85ps 2.2 7872.50
  13. FORD    TRANSIT 260 SWB DIESEL FWD  Low Roof Van TDi 85ps 2.2 6961.88
  14. FORD    TRANSIT 260 SWB DIESEL FWD  Low Roof Van TDi 85ps 2.2 6315.63
  15. FORD    TRANSIT 280 LWB DIESEL RWD  Medium Roof Van TDi 90ps  6345.00
  16. FORD    TRANSIT 280 LWB DIESEL RWD  Medium Roof Van TDi 90ps  9517.50
  17. FORD    TRANSIT 350 LWB DIESEL RWD  Medium Roof Van TDCi 100ps    13512.50
  18. FORD    TRANSIT 350 LWB DIESEL RWD  High Roof Van TDi 90ps    10398.75
  19. FORD    TRANSIT 350 LWB DIESEL RWD  High Roof Van TDi 90ps    9165.00
  20. FORD    TRANSIT 350 LWB DIESEL RWD  High Roof Van TDi 90ps    8577.50
  21. FORD    TRANSIT 350 LWB DIESEL RWD  High Roof Van TDi 90ps    7138.13
  22. FORD    TRANSIT 350 LWB DIESEL RWD  High Roof Van TDi 90ps    8783.13
  23. FORD    TRANSIT 350 LWB DIESEL RWD  High Roof Van TDi 90ps    7020.63
  24.  
what I want to achieve is results like this:
Expand|Select|Wrap|Line Numbers
  1. MAKE      MODEL                        DERIVATIVE                PRICE
  2. ====      =======                       =========                 =====
  3. FORD    MONDEO HATCHBACK SPEC EDS   2.0 Edge 5dr                 8250.00
  4. FORD    MONDEO HATCHBACK SPEC EDS   2.0 Edge 5dr                 8250.00
  5. FORD    RANGER DIESEL               Pick Up Double Cab TD 4WD  6168.75
  6. FORD    RANGER DIESEL               Pick Up XLT Super TD 4WD  7696.25
  7. FORD    RANGER DIESEL               Pick Up Double Cab TD 4WD  6462.50
  8. FORD    TRANSIT 260 SWB DIESEL FWD  Low Roof Van TDi 85ps 2.2 6521.25
  9. FORD    TRANSIT 260 SWB DIESEL FWD  Low Roof Van TDi 85ps 2.2 7784.38
  10. FORD    TRANSIT 260 SWB DIESEL FWD  Low Roof Van TDi 85ps 2.2 7872.50
  11. FORD    TRANSIT 280 LWB DIESEL RWD  Medium Roof Van TDi 90ps  6345.00
  12. FORD    TRANSIT 280 LWB DIESEL RWD  Medium Roof Van TDi 90ps  9517.50
  13. FORD    TRANSIT 350 LWB DIESEL RWD  Medium Roof Van TDCi 100ps    13512.50
  14. FORD    TRANSIT 350 LWB DIESEL RWD  High Roof Van TDi 90ps    10398.75
  15. FORD    TRANSIT 350 LWB DIESEL RWD  High Roof Van TDi 90ps    9165.00
  16.  
So this is like a filtering by model on results returned. It doesn't mean I only want 3 Ford Transits, it can be 3 ford transit 350 LWB Diesel as this is the full model name. The only flaw with your query was when few models have the same price they will get the same position (POS) and they might be displayed if there are no cars with lower price if you know what I am saying. Example:
Expand|Select|Wrap|Line Numbers
  1. MAKE    MODEL    DERIVATIVE    colour    PRICE    POS
  2. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    CAYMAN BLUE    8025.00    1
  3. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    BLACK    8025.00           1
  4. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    INTENSE BLUE    8025.00     1
  5. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    BLACK    8025.00         1
  6. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    FADED DENIM    8025.00    1
  7. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    BLACK    8025.00      1
  8. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    BLACK    8025.00      1
  9. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    BLADE SILVER    8025.00    1
  10. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    INTENSE BLUE    8025.00    1
  11. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    BLACK    8025.00          1
  12. NISSAN    NOTE HATCHBACK    1.4 Acenta 5dr    BLADE SILVER    8025.00    1
  13.  
So my question is - do you have a suggestion on how can I deal with same priced models? Thanks
Apr 23 '08 #9
adrianga
7 New Member
Sorry for follow up but I finally got it!
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM(SELECT REGNO,Make as [MAKE], Model as [MODEL], DERIVATIVE,colour, PRICE,
  2.         ( SELECT COUNT(PRICE) AS POS
  3.                 from MY_VIEW b
  4.                 WHERE a.make=b.make and a.model = b.model and a.price>b.price
  5.         )+1  AS POS,( SELECT COUNT(PRICE) AS POS
  6.                 from MY_VIEW b
  7.                 WHERE a.make=b.make and a.model = b.model and a.price=b.price and a.regno>b.regno
  8.         )+1  AS POS2
  9. from MY_VIEW a) z 
  10. WHERE POS<4 AND POS2<4
  11.  
I know I should have mention that in my view I also have unique reg number for each car maybe you would come up with solution for me earlier.
But I finally got your logic and used it properly: so what I am doing now is first checking position in results based on price but if there is more than one car with the same price,the same make and same model than I am checking for position based on reg number.
Also in your suggested query you have been counting how many same make model and derivative cars have price higher than ones already returned in the results and that's why I had more than 3 same models in results even if the price was different - I only had to remove 'a.derivative=b .derivative' and it worked.
Well, huge thanks @delerna as I don't think there is many people out there with solution like this one. Few of my colleagues - software developers were helping me to come up with a solution and they couldn't 'hack it' :)
Apr 23 '08 #10

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

Similar topics

3
2411
by: Rainer Collet | last post by:
hi! i tested several php database abstraction layers (db, mdb(2), creole, adodb, etc), but i always missed one really important feature: i need a method for a limited select which gives me the resultset and the total number of rows the select would have returned without the limit. nativly this is very easy SELECT SQL_CALC_FOUND ROWS * FROM table LIMIT 0,10 in mysql for example.
3
1972
by: Paul Keating | last post by:
I have a very simple COM server written in Python that is trying to return a two-dimensional array 268 x 20. The values it contains are some small integers, some short (<29 character) Unicode strings, and None. To isolate the problem I have taken out the code that builds the matrix from real data, and just substituted a literal tuple of tuples, like this: class AtlasSecurity:
1
3050
by: Steve | last post by:
I've run in to a problem with a query I'm trying to write. I have attached a sample SQL script at the end of this post to show an overview of what I'm working with. I want to be able to use LIMIT to control how many rows from one table are returned, independent of how many rows there are in a second table that is joined to the first. When I execute the following SQL script:
2
18125
by: Urban | last post by:
hi, I have a question. Maybe You know the equivalent to command LIMIT from MySQL I couldn`t find something like this in MS SQL PS I try to display 10 records begining form e.g. 4 sort by id something like: "SELECT * FROM table WHERE name=... LIMIT 4, 10 ORDER BY id" in MySQL
4
10411
by: rod | last post by:
Hello SQL gurus! I am trying to write a query that will return a set of continguous rows from a table, and limit the number of rows returned when a maximum total has been reached by adding a value in one of the columns. For example, the two columns below represent 2 columns in a table. a 2 b 2
0
5805
by: D. Dante Lorenso | last post by:
I need to know that original number of rows that WOULD have been returned by a SELECT statement if the LIMIT / OFFSET where not present in the statement. Is there a way to get this data from PG ? SELECT ... ; ----> returns 100,000 rows
2
2856
by: elein | last post by:
Yes, I vacuumed. Reproduced on both 7.3.2 and 7.5. Brain dead java beans want order by clauses in views that they use. my view is: select .... from bigtable b left join lookuptable l order by bigkey desc;
1
6319
by: C. | last post by:
Hi, First off, apologies for posting here - I can't find a dedicated SQLite newsgroup The other thing I can't find is how to retrieve the total number of rows which would have been returned by a query if it did not have a LIMIT and/or OFFSET clause. (kind of like SQL_CALC_FOUND_ROWS/FOUND_ROWS() in MySQL)
7
4333
by: Garth Wells | last post by:
I use a filtered SELECT to populate the SQLDataReader (rdr) with a filename and a blob (pdf). I then use File.WriteAllBytes to write each pdf to disk. ---------------------------------------- rdr = command.ExecuteReader(); while (rdr.Read()) { byte BinaryImage = (byte)rdr;
0
2542
by: GeniusTse | last post by:
Hi all, I found a big performance difference between two query with different "Limit" and I think it is not make sense Table: person_item PKey: (person_id, time) Index: (time) Query1) SELECT item FROM person_item WHERE person_id = '3000' ORDER BY time DESC OFFSET 0 LIMIT 10; total runtime: 0.224 ms
0
9856
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9698
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
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
10654
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
10297
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...
1
7833
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
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4493
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
3136
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.