ÒýÑÔ£¬»ù´¡£º
TOP ×Ö¾äÔÊÐíÖ¸¶¨ WITH TIES¡££¨¶àÐÐͬ룩
SELECT TOP 5 WITH TIES title_id,price,title_name
FROM title
ORDER BY price DESC
½á¹û£º
title_id price title_name
-------- --------------------- ------------------------------
Tt005 80.0000 Title_Name_5
Tt013 70.0000 Title_Name_13
Tt012 65.0000 Title_Name_12
Tt010 55.0000 Title_Name_10
Tt002 50.0000 Title_Name_2
Tt004 50.0000 Title_Name_4
Tt008 50.0000 Title_Name_8
Tt014 50.0000 Title_Name_14
(8 row(s) affected)
·½·¨Ò»£º±ê×¼µÄ SQL ·½·¨£ºÀûÓÃÊÓͼ
¶ÔÓÚ´ó±íÀ´Ëµ£¬ÐÔÄܽ«ÏÔÖø½µµÍ£¬ÒòΪ¶ÔÿһÐж¼ÒªÉ¨ÃèÒ»´Î¸Ã±í¡£
CREATE VIEW ranked_sales(rank,title_id,price,title_name)
AS
SELECT
(SELECT COUNT(DISTINCT T2.price) FROM title AS T2
WHERE T2.price>=T1.price) AS rank,
title_id,
price,
title_name
FROM title AS T1 WHERE price IS NOT NULL
GO
SELECT * FROM ranked_sales WHERE rank<=10 ORDER BY rank
GO
½á¹û£º
rank title_id price title_name
----------- -------- --------------------- ------------------------------
1 Tt005 80.0000 Title_Name_5
2 Tt013 70.0000 Title_Name_13
3 Tt012 65.0000 Title_Name_12
4 Tt010 55.0000 Title_Name_10
5 Tt002 50.0000 Title_Name_2
5 Tt004 50.0000 Title_Name_4
5 Tt008 50.0000 Title_Name_8
5 Tt014 50.0000 Title_Name_14
6 Tt001 40.0000 Title_Name_1
7 Tt006 39.0000 Title_Name_6
8 Tt007 38.0000 Title_Name_7
9 Tt009 35.0000 Title_Name_9
10 Tt011 33.0000 Title_Name_11