SQL Server函数ROW_NUMBER()OVER(PARTITION BY not calc

时间:2017-08-08 08:19:10

标签: sql sql-server database sql-server-2008

我的ROW_NUMBER() OVER(PARTITION BY ....)功能有问题。

这是我与之合作的原始表格:

PRODUCT    VERSION   Colour
------------------------------
Apple        7.1      Black
Apple        7.1      Blue
Apple        7.1      Red
Apple        2.0      NULL
Beer         12.8     Blue 
Beer         12.8     NULL
Beer         12.8     NULL
Bread        14.87    Blue  
Bread        14.87    NULL
Bread        14.87    Orange  
Notes        100      Red 
Notes        100      NULL
Car          NULL     Green
Car          NULL     Blue
Car          NULL     Red
Car          NULL     Red     

我针对此表运行此脚本:

SELECT 
    [product], [version], [colour],
    ROW_NUMBER() OVER (PARTITION BY [product], [Version] ORDER BY [product] DESC) AS COUNT2  
FROM 
    TABLE1

然后我得到这样的输出:

PRODUCT    VERSION   Colour  COUNT2
-----------------------------------
Apple        7.1      Black   1
Apple        7.1      Blue    2
Apple        7.1      Red     3
Apple        2.0      NULL    4
Beer         12.8     Blue    1
Beer         12.8     NULL    2
Beer         12.8     NULL    3
Bread        14.87    Blue    1
Bread        14.87    NULL    2
Bread        14.87    Orange  3
Notes        100      Red     1   
Notes        100      NULL    2
Car          NULL     Green   1
Car          NULL     Blue    2
Car          NULL     Red     3
Car          NULL     Red     4

然后我选择具有最大值的行

SELECT 
    [Product], [Version], MAX(COUNT2)
FROM 
    [dbo].[TABLE1] 
GROUP BY 
    Product, Version

但是我有问题。当我运行函数ROW_NUMBER() OVER(PARTITION BY ...)仅依赖于1列

时,我无法获得相同的数字

如果我计算回来的话,我无法计算回来并得到正确的结果。

PRODUCT   VERSION    COUNT2
----------------------------
Apple       7.1       6
Apple       2.0       7
Beer        12.8      5
Bread       14.87     1 
Notes       100       6
Car         NULL      8

如果我在没有版本的情况下运行脚本,那么当我进行计算时,我会得到不同的数字

SELECT 
    [product], [version], [colour],
    ROW_NUMBER() OVER(PARTITION BY [product], ORDER BY [product] DESC) AS COUNT2  
FROM 
    TABLE1

SELECT 
    [Product], MAX(COUNT2)
FROM 
    [dbo].[TABLE1] 
GROUP BY 
    Product

结果与我的期望不同:

PRODUCT    COUNT
----------------
Apple        4
Beer         3
Bread        3
Notes        2
Car          4

我期待这样的结果:

PRODUCT   VERSION    COUNT2
----------------------------
Apple       7.1       3
Apple       2.0       1
Beer        12.8      3
Bread       14.87     3 
Notes       100       2
Car         NULL      4

感谢您帮我设置功能ROW_NUMBER() OVER(PARTITION BY....)

原始表有超过10000行..

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT 
    [product], [version], COUNT(*)  
FROM 
    TABLE1
GROUP BY [product], [version]

并告诉我它是否能满足您的需求

P.S。你肯定有复制/粘贴的问题:这段代码工作正常:enter image description here

答案 1 :(得分:0)

只需为查询添加count()和分区

Select  PRODUCT,[VERSION], COUNT2 from  (
Select DISTINCT 
        PRODUCT, 
        VERSION,
        Count(COUNT2)OVER(PARTITION BY version )COUNT2 from (
SELECT 
    [product], 
    [version], 
    [colour],
    ROW_NUMBER() OVER (PARTITION BY [product], [Version] ORDER BY [product] DESC) AS COUNT2  
FROM 
    @Table1 )T
    GROup BY  PRODUCT, VERSION,COUNT2 )TT


    ORDER BY CASE WHEN VERSION IS NOT NULL then 0 else 1 END