Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to get rows from same table with different conditions

Posted on November 11, 2022 By

Solution 1 :

With row_number() window function:

select t.id, t.name, t.role 
from (
  select *, row_number() over (partition by role) rn
  from employeeTable
) t
where t.rn <= 2

You can also define:

partition by role order by name

or:

partition by role order by id

instead of just:

partition by role

to get specific rows in the results.
See the demo.
Results:

| ID  | Name     | Role          |
| --- | -------- | ------------- |
| 7   | Mia      | Clerk         |
| 8   | Amelia   | Clerk         |
| 1   | Emma     | Manager       |
| 2   | Olivia   | Manager       |
| 4   | Isabella | Sales Officer |
| 5   | Sophia   | Sales Officer |

Solution 2 :

Try is query and check if working:

Select * from employeeTable where role = 'Manager' LIMIT 2
UNION ALL
Select * from employeeTable where role = 'Sales officer' LIMIT 2
UNION ALL
Select * from employeeTable where role = 'Clerk' LIMIT 2

Problem :

I want to get data from same table with different conditions. I am using sqlite in android studio.

**ID    Name       Role**
1   Emma       Manager
2   Olivia     Manager
3   Ava        Manager
4   Isabella   Sales officer
5   Sophia     Sales Officer
6   Charlotte  Sales Officer
7   Mia        Clerk
8   Amelia     Clerk

Assume this table, I have different types of roles and different numbers of persons in a role. I want to select 2 persons from each role.

Select * from employeeTable where role = 'Manager' LIMIT 2
Select * from employeeTable where role = 'Sales officer' LIMIT 2
Select * from employeeTable where role = 'Clerk' LIMIT 2

Simply I want to join the result of these three queries.
Sorry if the childish question. And thanks in advance

Comments

Comment posted by stackoverflow.com/questions/32688057/…

Try this

Comment posted by Rashid Jamil

That’s really great! it solved my problem. Now there is a little issue. This query gets static data i.e. always select first 2 from each category. What if I want to select random/dynamic data from each Role / category? I mean to say that it should not get first two every time i run a query. But it should select 1st,3rd or 2nd,3rd or 1st,2nd

READ  [FIXED] java - How to set column and row number programmatically in gridLayout?
Powered by Inline Related Posts

Comment posted by forpas

You can change the WHERE clause to something like:

Comment posted by Amit Jangid

You cannot use limit before union all..this query won’t work…

Comment posted by TripeHound

select * from ( select * from employeeTable where role = 'Manager' LIMIT 2 ) UNION ALL ( select * from ... = 'Sales Officer' LIMIT 2 ) UNION ALL ( select * ... 'Clerk' LIMIT 2 ) ;

Android Tags:android, sqlite

Post navigation

Previous Post: [FIXED] android – how to change notification background color in nougat?
Next Post: [FIXED] android – How to make width ratio equal in XML GridView

Related Posts

[FIXED] android – How to change PageView after change tabs? Android
[FIXED] java – Android getAdapterPosition returns always getItemCount Android
[FIXED] How to make curved edge in a rectangle shape in android Android
[FIXED] java – Android TimerTask shut down when I open the app Android
[FIXED] java – Save Fragment data when switch to another fragment using Navigation Android
[FIXED] Can we debug the APK with ASAN active on Android Studio? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme