Xếp hạng trong SQL Server với DENSE_RANK() – multiple columns ranking

Xem thêm hàm DENSE_RANK dưới đây để xếp hạng trong SQL Server với nhiều cột

— Khai báo biến dữ liệu kiểu table

DECLARE @t1 table(entity char(2) NOT NULL, paymethod varchar(10) NOT NULL, [stock-type] char(4) NOT NULL)

Chèn một số dữ liệu vào bảng

INSERT @t1 (entity, paymethod, [stock-type])
SELECT ‘NA’, ‘Budget’, ‘OPEN’
UNION ALL SELECT ‘NA’, ‘Budget’, ‘OPEN’
UNION ALL SELECT ‘NA’, ‘Budget’, ‘OPEN’
UNION ALL SELECT ‘CE’, ‘XIGN’, ‘OPEN’
UNION ALL SELECT ‘CE’, ‘XIGN’, ‘OPEN’
UNION ALL SELECT ‘OB’, ‘TERMS’, ‘OPEN’
UNION ALL SELECT ‘NA’, ‘OTHER’, ‘OPEN’
UNION ALL SELECT ‘NA’, ‘OTHER’, ‘OPEN’
UNION ALL SELECT  ‘NA’, ‘OTHER’, ‘OPEN’
UNION ALL SELECT ‘NA’, ‘Budget’, ‘PAID’
UNION ALL SELECT ‘NA’, ‘Budget’, ‘OPEN’

Sử dụng hàm dense_rank

SELECT entity, paymethod, [stock-type], DENSE_RANK() OVER (ORDER BY [stock-type], paymethod, entity) FROM @t1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s