Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL Row_Number group by ID

Posted on 2016-08-22
5
Medium Priority
?
73 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 31

Expert Comment

by:MlandaT
ID: 41766238
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 41

Expert Comment

by:Sharath
ID: 41766240
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
ID: 41766269
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 22

Expert Comment

by:Kim Walker
ID: 41766303
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 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 41766517
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

783 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