row_number by several columns

三世轮回 提交于 2019-12-12 05:28:02

问题


I have the following data:

type   id    date1   date2   diff
-----------------------------------    
blue   1      x1     xxx      18
blue   1      x2     -         -
red    1      x1     -         -
blue   2      x1     xx       15
blue   2      x2     xx       18
blue   2      x3     -        -

And I want to add a row_number to get the data like this:

type   id    date1   date2   diff  row_number
---------------------------------------------
blue   1      x1     xxx      18      1
blue   1      x2     -         -      2
red    1      x1     -         -      1
blue   2      x1     xx       15      1
blue   2      x2     xx       18      2
blue   2      x3     -        -       3

I.e. first sort by type, then id and last date.

I have tried the following syntax:

Create table t(type char(7), id int(13), date1 date, date2 date, diff int, row_number int) ;

Insert into t(type, id, date1, date2, diff, row_number)

   (SELECT a.type, a.id, a.date1, a.date2, a.diff
    FROM 
       (Select 
            type, id, date1, date2, diff, row_number() over (order by type, id, date1) as r 
        from table) a 
    order by 
       a.type, a.id, a.date1;

The syntax above doesn't work and I get the error message:

You have an error in your SQL syntax; check the manual that corresponds to your MYSQL version....

I tried an easier syntax just to see if the command work like:

SELECT 
   type,    
   ROW_NUMBER() OVER (PARTITION BY type, id, date1 ORDER By type, lpnr, date1) as t,
   id,
   date1
FROM table;

or

select 
    row_number() over(order by id), 
    id
from table;

and still get the same error message.

Can you please tell me what I am doing wrong or if row_number doesn't work in the MYSQL versions (I have heidi and workbench)? If the command doesn't work is there any other way to do what I want to do?

Thank you so much for your help!

Linda


回答1:


Unfortunately I don't believe that MySQL provides the analytical functions that you are trying to use i.e ROWNUMBER() OVER PARTITION;

However, that does not mean that it can't be derived using others means. Give this a go:

create table myTable (type varchar(50) not null,id int(10) unsigned not null,
date1 varchar(10) default null,date2 varchar(10) default null,diff int unsigned default null
);

insert into myTable (type,id,date1,date2,diff) values ('blue',1,'x1','xxx',18);
insert into myTable (type,id,date1,date2,diff) values ('blue',1,'x2',null,null);
insert into myTable (type,id,date1,date2,diff) values ('red',1,'x1',null,null);
insert into myTable (type,id,date1,date2,diff) values ('blue',2,'x1','xx',15);
insert into myTable (type,id,date1,date2,diff) values ('blue',2,'x2','xx',18);
insert into myTable (type,id,date1,date2,diff) values ('blue',2,'x3',null,null);

select t.type,t.id,t.date1,t.date2,t.rownum
from
(
select mt.type,mt.id,mt.date1,mt.date2,mt.diff,
case 
when mt.id = @curId and mt.type = @curType then @curRow := @curRow + 1 
     else @curRow := 1
END as rownum,
@curId := mt.id,
@curType := mt.type
from myTable mt
join (select @curRow := 0, @curId := -1,@curType="") r
order by mt.id,mt.type
) t;


来源:https://stackoverflow.com/questions/14998877/row-number-by-several-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!