Row_number over (Partition by xxx order by yyy) in Linq?

    public static void Main(string[] args)
    {
        var beatles = (new[] { new { id=1 , inst = "guitar" , name="john" },
                new { id=2 , inst = "guitar" , name="george" },
                new { id=3 , inst = "guitar" , name="paul" },
                new { id=4 , inst = "drums" , name="ringo" },
                new { id=5 , inst = "drums" , name="pete" }
                });
        var o = beatles
                .OrderBy(x => x.id) // order by yyy
                .GroupBy(x => x.inst)   // partition by xxx
                .Select(group => new { Group = group, Count = group.Count() })
                .SelectMany(e=>e.Group.Select(v=>v)
                    .Zip(Enumerable.Range(1,e.Count) /*ROW_NUMBER*/, (i,j)=> new {
                        i.inst,i.name, rn = j
                    })
                );

        foreach (var item in o)
        {
            Console.WriteLine("{0}|{1}|{2}",item.inst,item.name, item.rn);
        }

        Console.ReadKey();
    }
时间: 2024-10-14 23:31:22

Row_number over (Partition by xxx order by yyy) in Linq?的相关文章

Row_number() OVER(PARTITION BY xxx ORDER BY XXX)分组排序

--//创建一个信息表 CREATE TABLE user_student(id decimal(18,0) identity(1,1),st_name nvarchar(30),class nvarchar(10),score decimal(18,2)) --//插入测试数据============start=================== insert into user_student(st_name,class,score) values('张三','甲','90') inser

分组 根据某一列进行排序,根据shopid分组,用createTime排序,返回row_number()序号 select no =row_number() over (partition by shopId order by createTime desc), * from Goods_info

over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用.其参数:over(partition by columnname1 order by columnname2)含义:按columname1指定的字段进行分组排序,或者说按字段columnname1的值进行分组排序.例如:employees表中,有两个部门的记录:department_id =10和20select department_id,rank() over(partition

row_number() OVER (PARTITION BY COL1 ORDER BY COL2)

--表示根据COL1分组,在分组内部根据 COL2排序,而此函数返回的值就表示每组内部排序后的顺序编号(组内连续的唯一的) create table student (id int ,classes int ,score int); insert into student values(1,1,89); insert into student values(2,1,90); insert into student values(3,1,76); insert into student value

ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN DESC)函数的使用

ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN DESC)函数的作用是指定COLUMN(列)进行分区,在分区内指定COLUMN(列)进行排序,其中PARTITION BY COLUMN 为分区函数.代码具体实现如下 不进行分区的排序方式--对商品价格的升序排序 SELECT A.*, ROW_NUMBER() OVERORDER BY PRICE ASC) AS ROW_FLG FROM COMMODITY A 进分区的排序方式--对商

row_number() over(partition by a order by b desc) rn 用法

ow_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的) 实际应用 取最新的数据 select * from (select hlpr.emplid, hlpr.deptid, hlpr.create_time, row_number() over (partition by hlpr.emplid order by hlpr.cre

oracle常用分析函数 over(partition by xxx order by xxx)

--over order by 连续累加的意思,把by后面相同的字段,一个组组累加起来SELECT id_,name_,proc_def_id_, count(*) over(order by name_) FROM ACT_RU_TASK --over partition by 分组统计 类似于group by,区别是它会列出重复的项,而group by只列出一条SELECT id_,name_,proc_def_id_, count(*) over(partition by name_) F

去重 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN

关键字  ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN 按照分组字段进行排序并标编号 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN 语法 ROW_NUMBER() OVER(PARTITION BY XMJL_UID ORDER BY XMCREATETIME) RN 实例 SELECT XMJL_UID,XMJL,XMCREATETIME,ROW_NUMBER() OVE

Oracle中的 row_number() over (partition by order by ) 用法

oracle 里面经常这样用 select col1,col2..., row_number() over (partition by colx order by coly) from table_name;; 这句话的意思是把表中的数值按照colx 分组,每一组内部按照coly排序,同时 row_number()返回排序之后该记录在改组内部的序号. 比如我们知道有emp表如下: SQL> SELECT * FROM SCOTT.EMP; EMPNO ENAME JOB MGR HIREDATE

row_number() over (partition by....order by...)用法 分组排序

转载来源:http://www.cnblogs.com/Kazaf/archive/2011/06/30/2094015.html row_number() OVER (PARTITION BY COL1 ORDER BY COL2) 表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的) SELECT G.*, ROW_NUMBER() OVER(PARTITION BY a ,b ORDER BY c DESC) ROWN fr