-3

I have the following data

IDX DATA
1 a
2 a
3 a
4 b
5 b
6 b
7 b
8 b
9 c
10 c

How can I, using Sqlite3, get the row count as such

IDX DATA COUNT
1 a 1 / 3
2 a 2 / 3
3 a 3 / 3
4 b 1 / 5
5 b 2 / 5
6 b 3 / 5
7 b 4 / 5
8 b 5 / 5
9 c 1 / 2
10 c 2 / 2

Thanks for you help

pcurtis
  • 81
  • 5
  • Just to make the results a little more useful would it be possible to add the total number of rows per group? i.e. 1 / 3 2 / 3 3 / 3 1 / 5 2 / 5 3 / 5 4 / 5 5 / 5 1 / 2 2 / 2 – pcurtis Dec 29 '20 at 12:30
  • Welcome to Stackoverflow! It is better to think of this community like you do for Wikipedia. Users come here with a specific question in mind, looking for a specific answer. Therefore, we require a description of the problem _and_ your own attempt at solving the problem. What have you tried? – Aman Dec 29 '20 at 13:02

2 Answers2

2

Use ROW_NUMBER:

SELECT
    IDX,
    DATA,
    ROW_NUMBER() OVER (PARTITION BY DATA ORDER BY IDX) COUNT
FROM yourTable
ORDER BY
    IDX;

screen capture from demo link below

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

You need ROW_NUMBER() window function:

SELECT *, 
       ROW_NUMBER() OVER (PARTITION BY DATA ORDER BY IDX) AS COUNT
FROM tablename
forpas
  • 160,666
  • 10
  • 38
  • 76