如何在Sql server 2008中获取Percentile数据

时间:2015-03-04 10:11:39

标签: sql sql-server sql-server-2008 sql-server-2012

我正在做一些数据分析。我只是一个初学者。请帮我解决这个问题。

我有一张下表,我希望在某一天获得DurationMilliseconds的90%。

RowId   Depth   StartMilliseconds   DurationMilliseconds    TxnDate
9869    3       169.8               80056.7                 Jan 19 2015 
9997    3        53                 17593.8                 Jan 19 2015 
10069   3        64.9                63072                  Jan 19 2015 
10950   4        67983.2             2198.9                 Jan 20 2015 
11178   5        2095.7             1886.5                  Jan 20 2015 
11490   3        16.4               818.4                   Jan 20 2015 
11751   5        36                 1955.9                  Jan 20 2015 
11924   5        2078.9             2550.7                  Jan 20 2015 
11998   4        33.4               2071.4                  Jan 20 2015 
12283   3        12                 1241.5                  Jan 20 2015 
12709   5        53                 1480.9                  Jan 21 2015 
12827   3        14.8               501.6                   Jan 21 2015 
13193   5        2667.4             1604.7                  Jan 21 2015 
13315   4         43.1              1646.5                  Jan 21 2015 
13981   4        90                 490.6                   Jan 21 2015 
14050   3        53.2                494                    Jan 21 2015 
14111   4        49                 1464.8                  Jan 21 2015 
14253   4        32                 1576.8                  Jan 22 2015 
14397   3        29.9               484.7                   Jan 22 2015 
14681   5        57.6               1522.6                  Jan 22 2015 
14779   4        45.8               1450.7                  Jan 22 2015 

我想要的输出是,

Jan 19 2015    90% of DurationMiliseconds?
Jan 20 2015    90% of DurationMiliseconds?
Jan 21 2015   90% of DurationMiliseconds?
Jan 22 2015    90% of DurationMiliseconds?

有人可以建议我使用SQL查询吗?

2 个答案:

答案 0 :(得分:1)

这是你需要的吗?

SELECT TxnDate, SUM(DurationMS) AS DurationMS, SUM(DurationMS) * 0.9 AS DurationMS90 
FROM [table] 
GROUP BY TxnDate

输出最后一列是第二列的90%

2015-01-19  160722.5    144650.25
2015-01-20  12723.3     11450.97
2015-01-21  7683.1      6914.79
2015-01-22  5034.8      4531.32

答案 1 :(得分:0)

假设您要输出90%的DurationMilliseconds列值,您可以执行以下操作。您可以修改CAST以根据需要格式化小数位数。

GO
DECLARE @DurationMilliseconds AS DECIMAL(19,2) = 80056.7
SELECT CAST(@DurationMilliseconds * 0.9 As Decimal(19,2)) [DurationMilliseconds ]

-- Other way, its same as above but more readable.
SELECT CAST(@DurationMilliseconds * (90.0/100) As Decimal(19,2)) [DurationMilliseconds]

所以,你需要做类似下面的事情:

SELECT TxnDate, CAST(SUM(ISNULL(DurationMilliseconds, 0.0)) * 0.9 As Decimal(19,2)) [DurationMilliseconds90percent]
FROM your_table
GROUP BY TxnDate