ROW_NUMBER(), RANK(), और DENSE_RANK() की तुलना
Ranking window functions में ROW_NUMBER() सबसे आम है, फिर RANK() और DENSE_RANK() आते हैं। इन प्रत्येक ranking functions (साथ ही NTILE()) से हमें SQL Server में रिकॉर्ड्स को रैंक करने के अलग-अलग तरीके मिलते हैं।
इस अभ्यास में, हम यह जानना चाहते हैं कि हमारे डेटा सेट में incident type 3 कितनी बार आता है। हम incidents की संख्या को descending order में रैंक करना चाहते हैं, ताकि जिस तारीख पर incidents की संख्या सबसे अधिक हो, उसका row number, rank, और dense rank 1 हो, और आगे इसी तरह। इसे फॉलो करना आसान बनाने के लिए, हम केवल वही तारीखें शामिल करेंगे जिनमें कम से कम 8 incidents हों।
यह अभ्यास पाठ्यक्रम का हिस्सा है
SQL Server में Time Series Analysis
अभ्यास निर्देश
- कॉलम alias के आधार पर प्रत्येक window function भरें। आपको
ROW_NUMBER(),RANK(), औरDENSE_RANK()का उपयोग ठीक एक-एक बार करना है। OVERक्लॉज़ मेंir.NumberOfIncidentsके अनुसार descending ऑर्डर में ordering भरें।
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
SELECT
ir.IncidentDate,
ir.NumberOfIncidents,
-- Fill in each window function and ordering
-- Note that all of these are in descending order!
___() OVER (___ ___ ir.NumberOfIncidents ___) AS rownum,
___() OVER (___ ___ ir.NumberOfIncidents ___) AS rk,
___() OVER (___ ___ ir.NumberOfIncidents ___) AS dr
FROM dbo.IncidentRollup ir
WHERE
ir.IncidentTypeID = 3
AND ir.NumberOfIncidents >= 8
ORDER BY
ir.NumberOfIncidents DESC;