How to do "Excel PERCENTRANK.INC Function" type of calculations with SQL Server Queries

Hi

I have an excel sheet that makes use of the Excel PERCENTRANK.INC Function for doing the  relative percentile score calculation on past 5, 10 and 15 days data available inside a Table.


The Excel PERCENTRANK.INC function calculates the relative position, between 0 and 1 (inclusive), of a specified value within a supplied array.

For understanding the basics of this excel function, please check out these 2 links -
http://www.excelfunctions.net/Excel-Percentrank-Inc-Function.html
http://best-excel-tutorial.com/59-tips-and-tricks/219-calculate-percentile

Now, I want to do similar calculation within SQL Server Database Table. Is it possible to write a SQL Query that will give output similar to the result shown in this excel snapshot on the right hand side ?

Here is the script that will create the Sample Database named " TestDB1001 "  and Table named " Table1001 " used in this work.

CREATE DATABASE TestDB1001
GO

USE TestDB1001
GO




CREATE TABLE TestDB1001.dbo.Table1001 (
  xdate DATETIME2(0) NULL,
  sector NVARCHAR(255) NULL,
  symbol NVARCHAR(255) NULL,
  [Close Price ] FLOAT NULL,
  Volume FLOAT NULL,
  [Volume Percent ] FLOAT NULL,
  OpenInterest FLOAT NULL,
  [OpenInterest Percent] FLOAT NULL
) ON [PRIMARY]
GO




SET DATEFORMAT ymd
SET ARITHABORT, ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, QUOTED_IDENTIFIER, ANSI_NULLS, NOCOUNT ON
SET NUMERIC_ROUNDABORT, IMPLICIT_TRANSACTIONS, XACT_ABORT OFF
GO

INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-01 00:00:00.0000000', N'Realty', N'DLF', 171.45, 236.57, 16.5, 657.59007, 6.88)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-01 00:00:00.0000000', N'Realty', N'HDIL', 55.6, 89.4, 61.43, 152.6776, 17.62)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-01 00:00:00.0000000', N'Realty', N'UNITECH', 15.9, 120.13, 45, 282.43848, 6.4)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-02 00:00:00.0000000', N'Realty', N'DLF', 166.45, 420.65, 77.81, 658.68621999999993, 0.17)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-02 00:00:00.0000000', N'Realty', N'HDIL', 51.85, 124.27, 39, 147.08239999999998, -3.67)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-02 00:00:00.0000000', N'Realty', N'UNITECH', 15.35, 119.73, -0.33, 273.67355999999995, -3.1)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-03 00:00:00.0000000', N'Realty', N'DLF', 169, 369.57, -12.14, 707.74436999999989, 7.45)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-03 00:00:00.0000000', N'Realty', N'HDIL', 53.5, 87.35, -29.71, 158.88595999999998, 8.03)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-03 00:00:00.0000000', N'Realty', N'UNITECH', 15.35, 57.75, -51.77, 279.11279999999994, 1.99)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-06 00:00:00.0000000', N'Realty', N'DLF', 167.25, 217.96, -41.02, 722.15043999999989, 2.04)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-06 00:00:00.0000000', N'Realty', N'HDIL', 52.95, 50.37, -42.34, 157.18616000000006, -1.07)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-06 00:00:00.0000000', N'Realty', N'UNITECH', 15.3, 38.19, -33.87, 286.6113, 2.69)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-07 00:00:00.0000000', N'Realty', N'DLF', 164.6, 265.73, 21.92, 710.63243, -1.5899999999999999)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-07 00:00:00.0000000', N'Realty', N'HDIL', 52.15, 108.35, 115.11, 167.99551999999997, 6.87)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-07 00:00:00.0000000', N'Realty', N'UNITECH', 15, 70.69, 85.1, 290.49611999999991, 1.3599999999999999)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-08 00:00:00.0000000', N'Realty', N'DLF', 162.9, 258.36, -2.77, 697.20230999999978, -1.8900000000000001)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-08 00:00:00.0000000', N'Realty', N'HDIL', 53.3, 70.91, -34.55, 174.64184, 3.95)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-08 00:00:00.0000000', N'Realty', N'UNITECH', 14.95, 55.43, -21.59, 291.71525999999994, 0.42)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-09 00:00:00.0000000', N'Realty', N'DLF', 159.5, 186.23, -27.92, 699.0476, 0.27)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-09 00:00:00.0000000', N'Realty', N'HDIL', 50.2, 103.81, 46.4, 182.34456, 4.41)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-09 00:00:00.0000000', N'Realty', N'UNITECH', 14.7, 50.19, -9.45, 294.89627999999988, 1.09)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-10 00:00:00.0000000', N'Realty', N'DLF', 156.85, 200.27, 7.54, 699.8979999999998, 0.12)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-10 00:00:00.0000000', N'Realty', N'HDIL', 49.55, 91.61, -11.75, 180.54768, -0.98)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-10 00:00:00.0000000', N'Realty', N'UNITECH', 14.25, 56.77, 13.11, 296.6907599999999, 0.61)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-13 00:00:00.0000000', N'Realty', N'DLF', 161, 197.51, -1.38, 703.72603999999978, 0.55)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-13 00:00:00.0000000', N'Realty', N'HDIL', 49.75, 72.79, -20.54, 183.90999999999997, 1.8599999999999999)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-13 00:00:00.0000000', N'Realty', N'UNITECH', 13.8, 61.04, 7.52, 300.79445999999996, 1.38)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-14 00:00:00.0000000', N'Realty', N'DLF', 157.8, 188.98, -4.32, 728.44625999999982, 3.51)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-14 00:00:00.0000000', N'Realty', N'HDIL', 49.6, 79.6, 9.36, 203.92476000000008, 10.88)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-14 00:00:00.0000000', N'Realty', N'UNITECH', 13.5, 51.78, -15.17, 305.99279999999982, 1.73)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-15 00:00:00.0000000', N'Realty', N'DLF', 160.5, 197.29, 4.4, 728.82591000000036, 0.05)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-15 00:00:00.0000000', N'Realty', N'HDIL', 51.3, 73.09, -8.18, 208.26824, 2.13)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-15 00:00:00.0000000', N'Realty', N'UNITECH', 14.2, 125.87, 143.09, 317.5926, 3.79)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-16 00:00:00.0000000', N'Realty', N'DLF', 162.4, 208.88, 5.87, 735.66176, 0.94)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-16 00:00:00.0000000', N'Realty', N'HDIL', 50.65, 68.36, -6.47, 208.24432000000002, -0.01)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-16 00:00:00.0000000', N'Realty', N'UNITECH', 13.95, 70.59, -43.92, 314.64828000000006, -0.93)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-17 00:00:00.0000000', N'Realty', N'DLF', 155.6, 284.44, 36.17, 756.6858900000002, 2.86)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-17 00:00:00.0000000', N'Realty', N'HDIL', 48.6, 86.94, 27.18, 210.18943999999993, 0.94)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-17 00:00:00.0000000', N'Realty', N'UNITECH', 13.65, 55.82, -20.92, 313.36584, -0.41)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-20 00:00:00.0000000', N'Realty', N'DLF', 156.1, 238.3, -16.22, 784.67971000000011, 3.7)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-20 00:00:00.0000000', N'Realty', N'HDIL', 49.55, 73.1, -15.92, 213.88799999999998, 1.76)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-20 00:00:00.0000000', N'Realty', N'UNITECH', 13.8, 76.18, 36.47, 318.67127999999997, 1.69)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-21 00:00:00.0000000', N'Realty', N'DLF', 157.8, 201.98, -15.24, 788.57473000000016, 0.5)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-21 00:00:00.0000000', N'Realty', N'HDIL', 50.9, 71.67, -1.96, 218.67824000000007, 2.24)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-21 00:00:00.0000000', N'Realty', N'UNITECH', 13.75, 44.2, -41.98, 318.27546, -0.12)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-22 00:00:00.0000000', N'Realty', N'DLF', 159.1, 194.93, -3.49, 794.0979600000004, 0.7)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-22 00:00:00.0000000', N'Realty', N'HDIL', 50.8, 68.03, -5.08, 217.37912000000003, -0.59)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-22 00:00:00.0000000', N'Realty', N'UNITECH', 13.75, 23.48, -46.88, 323.47625999999991, 1.63)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-23 00:00:00.0000000', N'Realty', N'DLF', 159.05, 199.54, 2.36, 801.66750000000025, 0.95)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-23 00:00:00.0000000', N'Realty', N'HDIL', 50.45, 61.91, -9, 218.71092000000002, 0.61)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-23 00:00:00.0000000', N'Realty', N'UNITECH', 13.7, 31.98, 36.2, 322.67046000000005, -0.25)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-24 00:00:00.0000000', N'Realty', N'DLF', 152.8, 396.39, 98.65, 882.00591000000031, 10.02)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-24 00:00:00.0000000', N'Realty', N'HDIL', 48.1, 105.47, 70.36, 226.20192000000003, 3.43)
INSERT TestDB1001.dbo.Table1001(xdate, sector, symbol, [Close Price], Volume, [Volume Percent], OpenInterest, [OpenInterest Percent]) VALUES ('2014-01-24 00:00:00.0000000', N'Realty', N'UNITECH', 13.5, 89.6, 180.18, 337.96794000000006, 4.74)
GO

Open in new window


I have also attached the Excel Sheet showing the actual data as well as the required output.

How to do get such query output in SQL Server ? Any help is appreciated.

Regards

20150714-143346.png20150714-143420.png
Excel-PERCENTRANK.INC-Function-Example.x
happy 1001Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulfreelancerCommented:
SQL Server 2012 onward has a PERCENT_RANK() function

see: https://msdn.microsoft.com/en-us/library/hh213573.aspx

Does this meet your need?
1
awking00Commented:
Can you post the formulas for the cells G8:I10 in your results so we can see the array and the value  being used to calculate the relative position?
1
happy 1001Author Commented:
Thank you so much for the link Paul. I am newbie to SQL Server, so I am trying to figure out how to use that example in my table.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

happy 1001Author Commented:
asking00, thanks for your reply.

Here are the formulas -

G8 =PERCENTRANK.INC(B2:B6,B2,2)
G9 =PERCENTRANK.INC(B2:B11,B2,2)
G10 =PERCENTRANK.INC(B2:B16,B2,2)

Thanks
0
happy 1001Author Commented:
Is there a way by which I can QUOTE the comment posted by other users "like Paul and awking in this case" ?

I could not find the option for that.

Thanks
0
PortletPaulfreelancerCommented:
see http://sqlfiddle.com/#!6/62a88/4
It is an example of using percent_rank applied to your sample raw data.
Details of that:
    
    CREATE TABLE Table1001 
        ([xdate] varchar(15), [sector] varchar(6), [symbol] varchar(7), [Close Price] numeric, [Volume] int, [Volume Percent] int, [OpenInterest] int, [OpenInterest Percent] int)
    ;
        
    INSERT INTO Table1001 
        ([xdate], [sector], [symbol], [Close Price], [Volume], [Volume Percent], [OpenInterest], [OpenInterest Percent])
    VALUES
        ('1/01/2014 0:00', 'Realty', 'DLF', 171.45, 236.57, 16.50, 657.59, 6.88),
        ('1/01/2014 0:00', 'Realty', 'HDIL', 55.60, 89.40, 61.43, 152.68, 17.62),
        ('1/01/2014 0:00', 'Realty', 'UNITECH', 15.90, 120.13, 45.00, 282.44, 6.40),
        ('2/01/2014 0:00', 'Realty', 'DLF', 166.45, 420.65, 77.81, 658.69, 0.17),
        ('2/01/2014 0:00', 'Realty', 'HDIL', 51.85, 124.27, 39.00, 147.08, -3.67),
        ('2/01/2014 0:00', 'Realty', 'UNITECH', 15.35, 119.73, -0.33, 273.67, -3.10),
        ('3/01/2014 0:00', 'Realty', 'DLF', 169.00, 369.57, -12.14, 707.74, 7.45),
        ('3/01/2014 0:00', 'Realty', 'HDIL', 53.50, 87.35, -29.71, 158.89, 8.03),
        ('3/01/2014 0:00', 'Realty', 'UNITECH', 15.35, 57.75, -51.77, 279.11, 1.99),
        ('6/01/2014 0:00', 'Realty', 'DLF', 167.25, 217.96, -41.02, 722.15, 2.04),
        ('6/01/2014 0:00', 'Realty', 'HDIL', 52.95, 50.37, -42.34, 157.19, -1.07),
        ('6/01/2014 0:00', 'Realty', 'UNITECH', 15.30, 38.19, -33.87, 286.61, 2.69),
        ('7/01/2014 0:00', 'Realty', 'DLF', 164.60, 265.73, 21.92, 710.63, -1.59),
        ('7/01/2014 0:00', 'Realty', 'HDIL', 52.15, 108.35, 115.11, 168.00, 6.87),
        ('7/01/2014 0:00', 'Realty', 'UNITECH', 15.00, 70.69, 85.10, 290.50, 1.36),
        ('8/01/2014 0:00', 'Realty', 'DLF', 162.90, 258.36, -2.77, 697.20, -1.89),
        ('8/01/2014 0:00', 'Realty', 'HDIL', 53.30, 70.91, -34.55, 174.64, 3.95),
        ('8/01/2014 0:00', 'Realty', 'UNITECH', 14.95, 55.43, -21.59, 291.72, 0.42),
        ('9/01/2014 0:00', 'Realty', 'DLF', 159.50, 186.23, -27.92, 699.05, 0.27),
        ('9/01/2014 0:00', 'Realty', 'HDIL', 50.20, 103.81, 46.40, 182.34, 4.41),
        ('9/01/2014 0:00', 'Realty', 'UNITECH', 14.70, 50.19, -9.45, 294.90, 1.09),
        ('10/01/2014 0:00', 'Realty', 'DLF', 156.85, 200.27, 7.54, 699.90, 0.12),
        ('10/01/2014 0:00', 'Realty', 'HDIL', 49.55, 91.61, -11.75, 180.55, -0.98),
        ('10/01/2014 0:00', 'Realty', 'UNITECH', 14.25, 56.77, 13.11, 296.69, 0.61),
        ('13/01/2014 0:00', 'Realty', 'DLF', 161.00, 197.51, -1.38, 703.73, 0.55),
        ('13/01/2014 0:00', 'Realty', 'HDIL', 49.75, 72.79, -20.54, 183.91, 1.86),
        ('13/01/2014 0:00', 'Realty', 'UNITECH', 13.80, 61.04, 7.52, 300.79, 1.38),
        ('14/01/2014 0:00', 'Realty', 'DLF', 157.80, 188.98, -4.32, 728.45, 3.51),
        ('14/01/2014 0:00', 'Realty', 'HDIL', 49.60, 79.60, 9.36, 203.92, 10.88),
        ('14/01/2014 0:00', 'Realty', 'UNITECH', 13.50, 51.78, -15.17, 305.99, 1.73),
        ('15/01/2014 0:00', 'Realty', 'DLF', 160.50, 197.29, 4.40, 728.83, 0.05),
        ('15/01/2014 0:00', 'Realty', 'HDIL', 51.30, 73.09, -8.18, 208.27, 2.13),
        ('15/01/2014 0:00', 'Realty', 'UNITECH', 14.20, 125.87, 143.09, 317.59, 3.79),
        ('16/01/2014 0:00', 'Realty', 'DLF', 162.40, 208.88, 5.87, 735.66, 0.94),
        ('16/01/2014 0:00', 'Realty', 'HDIL', 50.65, 68.36, -6.47, 208.24, -0.01),
        ('16/01/2014 0:00', 'Realty', 'UNITECH', 13.95, 70.59, -43.92, 314.65, -0.93),
        ('17/01/2014 0:00', 'Realty', 'DLF', 155.60, 284.44, 36.17, 756.69, 2.86),
        ('17/01/2014 0:00', 'Realty', 'HDIL', 48.60, 86.94, 27.18, 210.19, 0.94),
        ('17/01/2014 0:00', 'Realty', 'UNITECH', 13.65, 55.82, -20.92, 313.37, -0.41),
        ('20/01/2014 0:00', 'Realty', 'DLF', 156.10, 238.30, -16.22, 784.68, 3.70),
        ('20/01/2014 0:00', 'Realty', 'HDIL', 49.55, 73.10, -15.92, 213.89, 1.76),
        ('20/01/2014 0:00', 'Realty', 'UNITECH', 13.80, 76.18, 36.47, 318.67, 1.69),
        ('21/01/2014 0:00', 'Realty', 'DLF', 157.80, 201.98, -15.24, 788.57, 0.50),
        ('21/01/2014 0:00', 'Realty', 'HDIL', 50.90, 71.67, -1.96, 218.68, 2.24),
        ('21/01/2014 0:00', 'Realty', 'UNITECH', 13.75, 44.20, -41.98, 318.28, -0.12),
        ('22/01/2014 0:00', 'Realty', 'DLF', 159.10, 194.93, -3.49, 794.10, 0.70),
        ('22/01/2014 0:00', 'Realty', 'HDIL', 50.80, 68.03, -5.08, 217.38, -0.59),
        ('22/01/2014 0:00', 'Realty', 'UNITECH', 13.75, 23.48, -46.88, 323.48, 1.63),
        ('23/01/2014 0:00', 'Realty', 'DLF', 159.05, 199.54, 2.36, 801.67, 0.95),
        ('23/01/2014 0:00', 'Realty', 'HDIL', 50.45, 61.91, -9.00, 218.71, 0.61),
        ('23/01/2014 0:00', 'Realty', 'UNITECH', 13.70, 31.98, 36.20, 322.67, -0.25),
        ('24/01/2014 0:00', 'Realty', 'DLF', 152.80, 396.39, 98.65, 882.01, 10.02),
        ('24/01/2014 0:00', 'Realty', 'HDIL', 48.10, 105.47, 70.36, 226.20, 3.43),
        ('24/01/2014 0:00', 'Realty', 'UNITECH', 13.50, 89.60, 180.18, 337.97, 4.74)
    ;
    
**Query 1**:

    select
          xdate
        , [DLF]
        , percent_rank() over(order by xdate) as DLF_pct
        , [HDIL]
        , percent_rank() over(order by xdate) as HDIL_pct
        , [UNITECH]
        , percent_rank() over(order by xdate) as UNITECH_pct
    from (
          select
                  xdate
                , max(case when [symbol] = 'DLF'     then [volume] end) as DLF
                , max(case when [symbol] = 'HDIL'    then [volume] end) as HDIL
                , max(case when [symbol] = 'UNITECH' then [volume] end) as UNITECH
          from Table1001
          group by xdate
         ) as x
         

**[Results][2]**:
    |           xdate | DLF |              DLF_pct | HDIL |             HDIL_pct | UNITECH |          UNITECH_pct |
    |-----------------|-----|----------------------|------|----------------------|---------|----------------------|
    |  1/01/2014 0:00 | 236 |                    0 |   89 |                    0 |     120 |                    0 |
    | 10/01/2014 0:00 | 200 | 0.058823529411764705 |   91 | 0.058823529411764705 |      56 | 0.058823529411764705 |
    | 13/01/2014 0:00 | 197 |  0.11764705882352941 |   72 |  0.11764705882352941 |      61 |  0.11764705882352941 |
    | 14/01/2014 0:00 | 188 |  0.17647058823529413 |   79 |  0.17647058823529413 |      51 |  0.17647058823529413 |
    | 15/01/2014 0:00 | 197 |  0.23529411764705882 |   73 |  0.23529411764705882 |     125 |  0.23529411764705882 |
    | 16/01/2014 0:00 | 208 |  0.29411764705882354 |   68 |  0.29411764705882354 |      70 |  0.29411764705882354 |
    | 17/01/2014 0:00 | 284 |  0.35294117647058826 |   86 |  0.35294117647058826 |      55 |  0.35294117647058826 |
    |  2/01/2014 0:00 | 420 |   0.4117647058823529 |  124 |   0.4117647058823529 |     119 |   0.4117647058823529 |
    | 20/01/2014 0:00 | 238 |  0.47058823529411764 |   73 |  0.47058823529411764 |      76 |  0.47058823529411764 |
    | 21/01/2014 0:00 | 201 |   0.5294117647058824 |   71 |   0.5294117647058824 |      44 |   0.5294117647058824 |
    | 22/01/2014 0:00 | 194 |   0.5882352941176471 |   68 |   0.5882352941176471 |      23 |   0.5882352941176471 |
    | 23/01/2014 0:00 | 199 |   0.6470588235294118 |   61 |   0.6470588235294118 |      31 |   0.6470588235294118 |
    | 24/01/2014 0:00 | 396 |   0.7058823529411765 |  105 |   0.7058823529411765 |      89 |   0.7058823529411765 |
    |  3/01/2014 0:00 | 369 |   0.7647058823529411 |   87 |   0.7647058823529411 |      57 |   0.7647058823529411 |
    |  6/01/2014 0:00 | 217 |   0.8235294117647058 |   50 |   0.8235294117647058 |      38 |   0.8235294117647058 |
    |  7/01/2014 0:00 | 265 |   0.8823529411764706 |  108 |   0.8823529411764706 |      70 |   0.8823529411764706 |
    |  8/01/2014 0:00 | 258 |   0.9411764705882353 |   70 |   0.9411764705882353 |      55 |   0.9411764705882353 |
    |  9/01/2014 0:00 | 186 |                    1 |  103 |                    1 |      50 |                    1 |

  [1]: http://sqlfiddle.com/#!6/62a88/4
  [2]: http://sqlfiddle.com/#!6/62a88/4/0

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Commented:
The following will produce results like this (you may want to use the pivot function to create columns fro the rows -
PCT_DURATION SYMBOL           PCT
----------------------- --------------------- ----------
5 Day                    DLF                     25
5 Day                    HDIL                    50
5 Day                    UNITECH          100
10 Day                  DLF                     55
10 Day                  HDIL                    55
10 Day                  UNITECH          100
15 Day                  DLF                     57
15 Day                  HDIL                    71
15 Day                  UNITECH            92
with cte as
(select xdate, symbol, volume,
 row_number() over (partition by symbol order by xdate) rn
 from table1001)
select pct_duration, symbol, floor(100*(rnk - 1)/4) pct from
(select '5 Day' as pct_duration, symbol, volume, rank() over (partition by symbol order by volume) rnk, rn
 from cte where rn <= 5)
where rn = 1
union all
select pct_duration, symbol, floor(100*(rnk - 1)/9) pct from
(select '10 Day' as pct_duration, symbol, volume, rank() over (partition by symbol order by volume) rnk, rn
 from cte where rn <= 10)
where rn = 1
union all
select  pct_duration, symbol, floor(100*(rnk - 1)/14) pct from
(select '15 Day' as pct_duration, symbol, volume, rank() over (partition by symbol order by volume) rnk, rn
 from cte where rn <= 15)
where rn = 1;
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.