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
Andreas BorchertAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.