Custom row numbering - mysql

I would like data rows numbered upon selecting from a table. Problem is that, I need not sequential numbering, but it should be numbered from 1 to 3 and so to the end, like below:
1 | first row
2 | second row
3 | third row
1 | fourth row
2 | and
3 | ....
1
2
3
I'm trying this query, but it does not work correctly:
mysql -> SET #n = 0;
-> SELECT
CASE
WHEN nnn = 3 THEN #n := 0
ELSE nnn
END
FROM (
SELECT #n := #n + 1 AS nnn FROM mytable
) AS t;
How to make it working correctly?

Adapted from this answer: With MySQL, how can I generate a column containing the record index in a table?
SELECT MOD(#curRow := #curRow + 1, 3) AS row_number
FROM mytable m
JOIN (SELECT #curRow := 0) r;

If you only need to select and assuming that there is column, say "id", that is sequential, then following query should help:
SELECT IF ((id%3) = 0, 3 , (id%3)) AS new_id, <other columns> FROM <table_name>;
Hope it helps...

Related

MySQL give a rank to each group

What I'm trying to do is update all rows column with the same id with increasing number and when there is no more of this id I to go on the next one.
Table:
id column
1 0
1 0
2 0
2 0
3 0
3 0
I tried:
SELECT #i:=0;
UPDATE table SET column = #i:=#i+1
This way I updated the column but the column values goes like 1 2 3 4 5 6 and
I must reset the #i back to 0 everytime when id of the row has changed.
OR:
Table:
id column
1 1
1 2
2 1
2 2
3 1
3 2
I guess this should be made by a loop. But can't get it. Plus when I tried to make a loop in the MySQL (PHPMyAdmin), it gave me an error. What I read around is that I can't make loops directly in the PHPMyAdmin or I misunderstood it?
UPDATE table ,(SELECT #curRow := 0, #curCol := '') r SET column=
( CASE id WHEN #curCol THEN #curRow := #curRow + 1 ELSE #curRow := 1 AND #curCol := id END)
Test

How to count rows of different group of values in one SELECT

I have a table with data that has an indicator as a number, it is not an ID or foreign key. This number is repeated several times in the table.
I need to print all the rows with included incremental row number within the same indicator value, so 1 is there 2 times, 2 is there 3 times, 3 is there 1 time.
Desired output:
name | indicator | rownumber
a 1 1
b 1 2
c 2 1
d 2 2
e 2 3
f 3 1
I have found this solution to count the rows but I do not know how to reset the counter if the indicator is changed.
The query I have so far is but this is incrementally counting the rows
SELECT name, indicator,
#rownum := #rownum + 1 as row_number
FROM rownumtable
CROSS JOIN (SELECT #rownum := 0) r
ORDER BY name ASC
BUT it prints the row number - see it on SQL Fiddle
name | indicator | row_number
a 1 1
b 1 2
c 2 3
d 2 4
e 2 5
f 3 6
Is there a way how to reset the row_number counter for specific group of same values in the MySQL query?
SELECT name, indicator,
#rownum := case when #prevIndicator <> indicator then 1 else #rownum + 1 end as rownumber,
#prevIndicator := indicator
FROM rownumtable
CROSS JOIN (SELECT #rownum := 0, #prevIndicator := 0) r
ORDER BY name ASC
SQLFiddle demo

Get row number (like excel)

How would I get the row number of a mysql output, for example:
select * from table
name age
David 12
Frank 13
I want to get the row number, like so:
select *, row_num from table
row name age
1 David 12
2 Frank 13
The row number on the left -- 1 through 7 -- not a part of the data itself.
In mysql, you have to use User-Defined Variables.
SELECT #rowno := #rowno + 1 AS row_no, *
FROM table
CROSS JOIN (SELECT #rowno := 0) t
-- ORDER BY age

Count row number from a mysql table subquery

I have a table department_courses with following structure :
id department_id name
-- ------------- -----
1 11 Abcd
2 11 Bghg
3 11 Lopps
4 13 Abvgf
So from this table I need to count the position of the subquery. I mean to say , The position of the name Lopps for department_id is 3 . How to get this in mysql query?
If you only need to do this for one row, then a single query is simpler:
select count(*)
from department_courses dc
where dc.id <= (select dc2.id
from department_courses dc2
where dc2.name = 'Lopps'
);
If you want to assign a row number to all rows, then variables are probably a better method.
Try:
select row_num
from (
select t.*, #r := #r + 1 row_num
from department_courses t,
(select #r := 0) r
) x
where x.name = 'Lopps'
x.department_id = 3

MySQL return 2 rows per result

Until today i thought i know something about MySQL.
OK lets say we have one table like this:
id | some_name |some_number
-----------------------------
1 | test | 33
2 | test | 34
3 | test | 35
3 | test2 | 36
3 | test2 | 37
and i want to write query to return something like this:
test 33
test 34
test2 36
test2 37
test3 12
test3 34
.
.
.
and so on. I want to return only 2 result per same name. It is easy to use limit and return only one result per name but I'm stuck to return multiple results per same name in this case 2 but might be an n results. Work around is to make some script that will do:
select some_name, some_number from tbl_name limit 2;
and to repeat it for every distinct some_name i have in table.
Is there any elegant solution for MySQL? I would be grateful if you share that with me.
You can use a user variable to add a counter of each row of a name, then just select the rows where the counter is less than or equal to 2 (untested):-
SELECT some_name, some_number
FROM
(
SELECT some_name, some_number, #cnt=(#some_name = some_name, #cnt + 1, 1) AS cnt, #some_name:=some_name
FROM
(
SELECT some_name, some_number
FROM tbl_name
ORDER BY some_name, some_number
) sub0
CROSS JOIN
(
SELECT #some_name:='', #cnt:=0
)
sub1
) sub2
WHERE cnt <= 2
you could try this,
select some_name,some_number from yourTable t
where
(select count(*) from yourTable
where
some_number<=t.some_number and some_name=t.some_name)<3
This partially solved my problem:
set #num := 0, #name := '';
select distinct number, name
from (
select number, name,
#num := if(#name = name, #num +1, 1) as row_number,
#name := name as dummy
from karian2
) as x where x.row_number <= 2;
It fails some time to return 2 results because sub query is not returning distinct values. Example here:
http://sqlfiddle.com/#!2/952ca/23