Solved

MySQL Row_Number group by ID

Posted on 2016-08-22
5
58 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

759 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