为所有 PARTITIONED RECORDS 分配唯一的 ROW NUMBER()

Assigning unique ROW NUMBER() for all the PARTITIONED RECORDS

在此查询中,它正在对记录进行分区,但我需要为所有结果集分配行号,行号不应重复,每个 fpr 应该是唯一的 row.Anyone 帮助?提前致谢。

select *
from Store  
order by 
   row_number() over (partition by category order by storename),category

Table SQLFIDDLE 中的列示例: http://sqlfiddle.com/#!6/767ab/86

我希望以 :

格式输出
RowNo     Category  
1         Fruits
2         Chocs
3         Vegetables
4         Fruits
5         Chocs
6         Vegetables
..........

如果您只想提供唯一的行号,那么不要使用分区方式,只使用排序方式 请尝试以下查询。

select *,row_number() over (order by storename) As RowNumber
from Store  
order by 
   row_number() over (order by storename),Category

不要使用 partition by 子句

select *
from Store  
order by 
   row_number() over (order by storename),category

Demo Fiddle

     select * into #temp
     from(
        select *,
        ROW_NUMBER() OVER(Partition by Category ORDER BY Category) AS Row_Number
        from Store) x  



     select ROW_NUMBER,StoreName,Category,Price,Quantity,
     ROW_NUMBER() over (order by ROW_NUMBER,category)  as uniquerownum from #temp
     group by Row_Number,StoreName,Category,Price,Quantity 

样本:-

http://sqlfiddle.com/#!6/767ab/138

放轻松....

select x.*,ROW_NUMBER() over (order by rownum,category)  as uniquerownum
from
      (select *,row_number() over (partition by category order by category) rownum
       from Store) x
order by rownum,category

尝试这样的事情:

WITH data AS (
    SELECT  ROW_NUMBER() OVER (PARTITION BY Category ORDER BY Category) rn, *
    FROM    STORE
)
SELECT  ROW_NUMBER() OVER (ORDER BY rn, Category) rn ,
        StoreId ,
        StoreName ,
        Category ,
        Price ,
        Quantity ,
        Address 
FROM    data
ORDER BY 1

这将产生以下序列:

1   CHOCOS
2   FRUITS
3   ICECREAM
4   VEGETABLES
5   CHOCOS
6   FRUITS
7   ICECREAM
8   VEGETABLES
9   CHOCOS
10  FRUITS
SELECT 
    ROW_NUMBER() OVER (ORDER BY Category) AS Id, -- New Id
    * 
FROM 
    (
        SELECT ROW_NUMBER() OVER (PARTITION BY Category ORDER BY StoreName) PartitionId, * 
        FROM Store
    ) data
WHERE 
    PartitionId = 1 -- Get one record for each category

SQL Fiddle