/* Distribution Value:
A distribution value indicates the possibility of occurance of that value among a group of values.
For Example, when a coin is thrown, it can be head or tail. Distribution value of heads is 1/2 = 0.5 , distribution value of tails is 1/2 =0.5
For example, when a dice is thrown, it can be any number among 1 to 6. So, distribution value of 1 is 1/6 = 0.1666, distribution value of 2 is 1/6 = 0.666.....
Cumulative Distribution Value:
Cumulative distribution value will tells you the possibility of occurance of that value or lesser than that among a group of values.
Suppose, when a dice is thrown, possibility that it can be a number less than or equal to 1 is, 1/6 = 0.1666
Suppose, when a dice is thrown, possibility that it can be a number less than or equal to 2 is, 2/6 = 0.3333
Suppose, when a dice is thrown, possibility that it can be a number less than or equal to 3 is, 3/6 = 0.5000
*/
Use [ExploringSQL]
GO
-- Creating table
CREATE TABLE [Results]
(
[Subject] varchar(10),
[Student] varchar(50),
[Marks] int
)
-- Inserting sample records
INSERT INTO [Results] (Subject,Student,Marks)
VALUES
('Maths','Student1',45),
('Physics','Student2',45),
('Physics','Student1',50),
('Chemistry','Student3',20),
('Physics','Student3',35),
('Biology','Student1',20),
('Biology','Student2',60),
('Biology','Student3',65),
('Chemistry','Student1',75),
('Biology','Student4',30)
GO
select * from [Results] order by Student , Marks
-- Querying with CUME_DIST()
SELECT [Student],[Subject],[Marks],
CUME_DIST() OVER(PARTITION BY [Subject] ORDER BY [Marks]) as CD
FROM [Results]
ORDER BY [Subject],[Marks]
GO
DROP TABLE [Results]
No comments:
Post a Comment