Solved

MySQL Row_Number group by ID

Posted on 2016-08-22
5
42 Views
Last Modified: 2016-08-23
Hello

Does anyone know how I can apply row_num partition by in MySQL?

I want to have another column, called row_gp_id, where V_id's = 13 will have row_gp_id 53 highlighted in yellow, while those circled in red will have row_gp_id = 54

mysql-group.JPG
Im using this code:

SELECT    id
        , b_id
        , v_id
        , g_id
        , @row_gp_id := @row_gp_id  +1 AS row_gp_id
		, @prev_id := v_id
FROM table_a  
, (SELECT @row_gp_id := 52) z  , (SELECT @prev_id := '') a

Open in new window


data set is:

id, b_id, v_id, g_id, group_id,
'9', '3', '13', '1', '53',
'10', '3', '13', '1', '54',
'13', '3', '13', '1', '55',
'16', '4', '7', '1', '56',
'17', '4', '7', '1', '57',
'18', '4', '7', '1', '58',
'19', '4', '7', '1', '59',
'20', '4', '7', '1', '60',
0
Comment
Question by:barkome
5 Comments
 
LVL 30

Expert Comment

by:MlandaT
Comment Utility
Apparently not as easy as it should be. Here are some suggestions (using variables) http://blog.sqlauthority.com/2014/03/09/mysql-reset-row-number-for-each-group-partition-by-row-number/amp/
0
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
I don't see any highlighting. Do you want same row_group_id 53 for v_id = 13 and row_group_id = 54 for v_id = 7?
0
 

Author Comment

by:barkome
Comment Utility
mysql.JPG
@Sharath, that's right where v_id = 13, row_group_id will be 53. Where v_id = 7, then row_group_id will be 54.
0
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
How about using a little logic to determine when to increment row_gp_id?
SELECT    id
        , b_id
        , v_id
        , g_id
        , IF(@prev_id=v_id,@row_gp_id,@row_gp_id := @row_gp_id  +1) AS row_gp_id
		, @prev_id := v_id
FROM table_a  
, (SELECT @row_gp_id := 52) z  , (SELECT @prev_id := '') a

Open in new window

1
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
You can try with variables and logic as Kim suggested.
If your table is not very big, you can also try with sub-queries.
select id, b_id, v_id, g_id 
       ,(select count(distinct v_id) from test t1 where t1.id <= t2.id)+51 group_id 
  from test t2;

Open in new window

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now