• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 36
  • Last Modified:

sqlite - get certain amount of records of multiple values

Greetings Experts,

I have a sqlite database with a table like this:

_id   name  type
01     A_0    a
02     A_1    a
03     A_2    a
04     A_3    a
05     B_0    b
06     B_1    b
07     B_2    b
08     B_3    b
09     C_0    c
10     C_1    c
11     C_2    c
12     C_3    c

Open in new window


And I want a result where only 2 rows of each type will be displayed like this:

_id   name  type
01     A_0    a
02     A_1    a
05     B_0    b
06     B_1    b
09     C_0    c
10     C_1    c

Open in new window


My query so far:

SELECT * FROM TheTable  X WHERE _id IN 
(
      SELECT Y._id FROM TheTable  Y
      WHERE X.type = Y.type
      ORDER BY Y._id ASC
      LIMIT 2
) ORDER BY _id;

Open in new window


But this will take like forever since I have a table with ~ 200k entries.

Does anyone happen to have a solution on this?

Thanks in advance!

Best regards
0
Andreas Borchert
Asked:
Andreas Borchert
  • 8
  • 6
2 Solutions
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
I didn't have to test but see if this works:
SELECT _id, name, type, rank() over(partition by type, order by name) as rank
FROM TheTable
HAVING rank <= 2;

Open in new window

0
 
Andreas BorchertAuthor Commented:
Hey, this doesn't seem to work in SQLITE.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
I see. I don't have SQLite to test. Only SQL Server.
What's the error?
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Andreas BorchertAuthor Commented:
near "(": syntax error: select _id, name, type, rank() over (
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Looks like it doesn't recognize the RANK function.
Try this version (I didn't test it):
SELECT t._id, t.name, t.type
FROM TheTable t
	INNER JOIN (SELECT TOP 2 t2.name, t2.type
		FROM TheTable t2
		WHERE t2.type = t.type
		ORDER BY t2.name) tt ON t.name=tt.name AND t.type=tt.type

Open in new window

0
 
Andreas BorchertAuthor Commented:
And neither the TOP 2:

near "2": syntax error: SELECT t._id, t.name, t.type
FROM TheTable t
      INNER JOIN (SELECT TOP 2
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Should be that LIMIT 2 you have in your original post?
SELECT t._id, t.name, t.type
FROM TheTable t
	INNER JOIN (SELECT t2.name, t2.type
		FROM TheTable t2
		WHERE t2.type = t.type
		ORDER BY t2.name
		LIMIT 2) tt ON t.name=tt.name AND t.type=tt.type

Open in new window

0
 
Andreas BorchertAuthor Commented:
Error: no such column: t.type

...but there is ofc a column "type" - strange...

I just built the schema here:

https://www.db-fiddle.com/f/oZ5cUfyjpGtLYmuiK1JH89/0
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Thank you for the fiddle so I could test. For me this worked:
SELECT t._id, t.name, t.type
FROM TheTable t
WHERE t.name IN (SELECT t2.name
		FROM TheTable t2
		WHERE t2.type = t.type
		ORDER BY t2.name
		LIMIT 2) 

Open in new window

https://www.db-fiddle.com/f/oZ5cUfyjpGtLYmuiK1JH89/1
0
 
Andreas BorchertAuthor Commented:
Yeah it is. But with a table that contains ~ 200k rows it takes more than 10 minutes.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
It has indexes?
If not you need to create indexes. At least you'll need an index on name and type columns.
0
 
Andreas BorchertAuthor Commented:
There are 3 indexes _id, name and type.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
I don't know SQLite but with those indexes your query should run faster.
Perhaps what's taking time is presenting the results and not the query execution at all. Do you have an option to verify the query execution time in SQLite?
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Solution provided.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now