Hi,
I am using SQL Server 2008. I want to calculate the 0,1,2,3,4 Quartiles in SQL server. I tried with NTILE function. I am getting the quartile number I am not getting the values of the quartiles. Please see the script below.
Table Script:
CREATE TABLE [dbo].[CFO_WC_CALCULATIONS_B](
[DSO] [bigint] NULL,
[DIO] [bigint] NULL,
[DIO_COGS] [bigint] NULL,
[DPO] [bigint] NULL,
[DPO_COGS] [bigint] NULL,
[DWC] [bigint] NULL,
[FISCAL_YEAR] [int] NULL,
[COMPANY_ID] [int] NULL
) ON [PRIMARY]
INSERT [dbo].[CFO_WC_CALCULATIONS_B] ( [DSO], [DIO], [DIO_COGS], [DPO], [DPO_COGS], [DWC], [FISCAL_YEAR], [COMPANY_ID]) VALUES (112501369, 81005479, 1308000000, 36, 11, 16, 44, 62, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2008, 32411)
INSERT [dbo].[CFO_WC_CALCULATIONS_B] ( [DSO], [DIO], [DIO_COGS], [DPO], [DPO_COGS], [DWC], [FISCAL_YEAR], [COMPANY_ID]) VALUES (94830136, 64128767, 1452000000, 47, 15, 22, 45, 66, 17, 0, 0, 0, 0, 0, 0, 0, 0, 2010, 32411)
INSERT [dbo].[CFO_WC_CALCULATIONS_B] ( [DSO], [DIO], [DIO_COGS], [DPO], [DPO_COGS], [DWC], [FISCAL_YEAR], [COMPANY_ID]) VALUES (112501369, 81005479, 1308000000, 36, 11, 16, 44, 62, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2008, 30625)
INSERT [dbo].[CFO_WC_CALCULATIONS_B] ( [DSO], [DIO], [DIO_COGS], [DPO], [DPO_COGS], [DWC], [FISCAL_YEAR], [COMPANY_ID]) VALUES (94830136, 64128767, 1452000000, 47, 15, 22, 45, 66, 17, 0, 0, 0, 0, 0, 0, 0, 0, 2010, 30625)
INSERT [dbo].[CFO_WC_CALCULATIONS_B] ( [DSO], [DIO], [DIO_COGS], [DPO], [DPO_COGS], [DWC], [FISCAL_YEAR], [COMPANY_ID]) VALUES (81010958, 57427397, 1438000000, 45, 17, 25, 52, 73, 10, 0, 0, 0, 0, 0, 0, 0, 0, 2009, 30625)
INSERT [dbo].[CFO_WC_CALCULATIONS_B] ( [DSO], [DIO], [DIO_COGS], [DPO], [DPO_COGS], [DWC], [FISCAL_YEAR], [COMPANY_ID]) VALUES (81010958, 57427397, 1438000000, 45, 17, 25, 52, 73, 10, 0, 0, 0, 0, 0, 0, 0, 0, 2009, 32411)
Here we are having the fiscal year and company id. I am having another table with the company ID and Industry code relation.
CREATE TABLE [dbo].[CFO_WC_COMPANY_NAICS_CODES](
[COMPANY_ID] [int] NOT NULL,
[SECTOR_NAICS_CODE] [int] NOT NULL
) ON [PRIMARY]
INSERT [dbo].[CFO_WC_COMPANY_NAICS_CODES] ([COMPANY_ID], [SECTOR_NAICS_CODE]) VALUES (30625, 3062)
INSERT [dbo].[CFO_WC_COMPANY_NAICS_CODES] ([COMPANY_ID], [SECTOR_NAICS_CODE] VALUES (32411, 3241)
so I want the different quartile values of the companies under the one sector in different years.
Query that i write is below I am getting the quartile numbers. I want the quartile values.
SELECT
FISCAL_YEAR
,COMPANY_ID
,DSO
,DIO
,DIO_COGS
,DPO
,DPO_COGS
,DWC
--INTO #TEMPMETRICS
FROM CFO_WC_CALCULATIONS_B CALB
SELECT COMPANY_ID
,FISCAL_YEAR
,DSO INTO #TEMPMETRICS from CFO_WC_CALCULATIONS_B
ALTER TABLE #TEMPMETRICS ADD SECTOR_NAICS_CODE INT
update #TEMPMETRICS SET #TEMPMETRICS.SECTOR_NAICS_CODE = CNC.SECTOR_NAICS_CODE
FROM CFO_WC_COMPANY_NAICS_CODES CNC
WHERE CNC.COMPANY_ID = #TEMPMETRICS.COMPANY_ID
--select * from #TEMPMETRICS
SELECT SECTOR_NAICS_CODE,FISCAL_YEAR
,NTILE(5) OVER (ORDER BY DSO) AS 'Quartile'
FROM #TEMPMETRICS
Can any body help me out how to calculate the quartile (0,1,2,3,4) values of these metrics.
Thanks
Dathy