Sql Server按特定顺序显示项目

时间:2010-08-31 18:15:44

标签: sql sql-server sql-server-2005 tsql

我有一个项目列表

ItemName制造商TopSalesUnit

Item1     A             100   
Item2     A             80                      
Item3     A             60
Item4     B             70   
Item5     B             50   
Item6     B             30
Item7     C             10       
Item8     C             05

我希望订购记录,以便首先显示最高的TopSalesUnit项目,然后显示来自不同制造商的下一个最高项目,然后是下一个最高项目 展示了第三家制造商等:

ItemName制造商TopSalesUnit

Item1       A             100    
Item4       B             070
Item7       C             010 
Item2       A             080
Item5       B             050 
Item8       C             005   
Item3       A             060 
Item6       B             030 

如何用T-SQL编写查询来实现它?

3 个答案:

答案 0 :(得分:8)

尝试:

DECLARE @YourTable table (ItemName varchar(10), Manufacturer char(1), TopSalesUnit int)

INSERT @YourTable VALUES ('Item1','A ',100)
INSERT @YourTable VALUES ('Item2','A ',80)                 
INSERT @YourTable VALUES ('Item3','A ',60)
INSERT @YourTable VALUES ('Item4','B ',70)   
INSERT @YourTable VALUES ('Item5','B ',50)   
INSERT @YourTable VALUES ('Item6','B ',30)
INSERT @YourTable VALUES ('Item7','C ',10)       
INSERT @YourTable VALUES ('Item8','C ',05)

SELECT
    dt.ItemName,dt.Manufacturer,dt.TopSalesUnit
    FROM (SELECT
              ItemName,Manufacturer,TopSalesUnit,ROW_NUMBER() OVER(PARTITION BY Manufacturer ORDER BY TopSalesUnit DESC) AS RowNumber
              FROM @YourTable
         ) dt 
    ORDER BY dt.RowNumber,dt.Manufacturer

输出:

ItemName   Manufacturer TopSalesUnit
---------- ------------ ------------
Item1      A            100
Item4      B            70
Item7      C            10
Item2      A            80
Item5      B            50
Item8      C            5
Item3      A            60
Item6      B            30

(8 row(s) affected)

答案 1 :(得分:1)

试试这个:

SELECT *,
    ( SELECT COUNT(*) FROM Items b
    WHERE b.Manufacturer = Items.Manufacturer
    AND b.TopSalesUnit > Items.TopSalesUnit )
    AS RankInManufacturer
FROM Items
ORDER BY RankInManufacturer, TopSalesUnit DESC

这会添加一个新的计算列,对每个“制造商”中的“TopSalesUnit”字段进行排名。

答案 2 :(得分:0)

使用KM的帖子我认为他接近你想要的但是按照正确的顺序

    DECLARE @YourTable table (ItemName varchar(10), Manufacturer char(1), TopSalesUnit int)

INSERT @YourTable VALUES ('Item1','A ',100)
INSERT @YourTable VALUES ('Item2','A ',80)                 
INSERT @YourTable VALUES ('Item3','A ',60)
INSERT @YourTable VALUES ('Item4','B ',70)   
INSERT @YourTable VALUES ('Item5','B ',50)   
INSERT @YourTable VALUES ('Item6','B ',30)
INSERT @YourTable VALUES ('Item7','C ',10)       
INSERT @YourTable VALUES ('Item8','C ',05)

select
    ItemName
    ,Manufacturer
    ,TopSalesUnit
    ,ROW_NUMBER() over (order by TopSalesUnit desc) as rn
    ,rank() over (partition by manufacturer order by TopSalesUnit desc) as rankcost
from
    @YourTable 

order by rankcost, rn

结果:

Item1   A   100 1   1
Item4   B   70  3   1
Item7   C   10  7   1
Item2   A   80  2   2
Item5   B   50  5   2
Item8   C   5   8   2
Item3   A   60  4   3
Item6   B   30  6   3