473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Limit rows returned

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 2428
ck9663
2,878 Expert 2GB
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 Expert 1GB
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 Expert 2GB
I missed that part. My bad.

-- CK
Apr 10 '08 #4
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 Expert 1GB
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
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 Expert 1GB
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
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
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
Delerna
1,134 Expert 1GB
You are very welcome and I am glad you now have a solution.
Oh and yes, knowing about REGNO could have made a difference.

This technique is very good at doing the sort of thing you were requiring.
I do not recommend doing it with tables that have lots of records however.
This is because the subquery gets executed once for each and every record.
so if you had 10,000,000 records the subquery gets executed 10,000,000 times.
The query is working with something like 100,000,000,000,000 records. As you can imagine, that could become very slow.
Apr 23 '08 #11
You are very welcome and I am glad you now have a solution.
Oh and yes, knowing about REGNO could have made a difference.

This technique is very good at doing the sort of thing you were requiring.
I do not recommend doing it with tables that have lots of records however.
This is because the subquery gets executed once for each and every record.
so if you had 10,000,000 records the subquery gets executed 10,000,000 times.
The query is working with something like 100,000,000,000,000 records. As you can imagine, that could become very slow.
Yes, I have actually notice that execution speed is not the best. I don't have 10.000.000 records but it already takes a while to load results. Is there anything else that you could recommend to achieve what I want?
Apr 29 '08 #12
Delerna
1,134 Expert 1GB
There are 1001 ways to skin a cat.
For example
You could add an extra field to hold the prices relative position to the table and then run an update query on that field before you actually run the query.

That way the subquery that detemines a rows position in the price range would be removed from the selection query and used as the update query.
It would then only be run once insted of once for every row.

If you didn't want to add rows to the table then you could use a table variable with the extra field in that. Then you could do the update and select I mentioned above.
Warning, table variables are like a table and its data that exists totally in system memory so............

there would be other ways.
Apr 29 '08 #13
Delerna
1,134 Expert 1GB
There is a thread in the Access forum Here with a very similar requirement.
This one wants the rows with the shortest 2 distances between points in 12 sided polygons. The technique is reported to be slow there also. I must say, most of my development is with SQLServer and I have used this technique myself on resonably large tables without too much of a speed issue. I do have indexes though.
Apr 30 '08 #14
I will play around with indexes and maybe try adding new field to my table. Thanks a lot for all your help!
Apr 30 '08 #15

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

Similar topics

3
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...
3
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...
1
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...
2
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...
4
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...
0
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 ?...
2
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...
1
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...
7
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. ----------------------------------------...
0
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)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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,...

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.