Solved

MySQL Row_Number group by ID

Posted on 2016-08-22
5
53 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
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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

756 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