我对SQL不太熟悉,因为我正在寻求专业人士的帮助:)
我有这样的声明,用%来计算交易的不成功比例,但它是整天做的 问题是:如何更改语句,以便每15分钟返回不成功比率的最大值?要查看每15分钟的百分比,以便了解哪个区间是最大的不成功交易。
答案 0 :(得分:0)
我不知道你想要的确切输出是什么,但我想你可以从这开始。
;WITH mCTE AS (
SELECT p.*, CASE WHEN DATEPART(MINUTE, p.server_time) >=0 AND DATEPART(MINUTE, p.server_time) < 15 THEN '0...14'
WHEN DATEPART(MINUTE, p.server_time) >=15 AND DATEPART(MINUTE, p.server_time) < 30 THEN '15...29'
WHEN DATEPART(MINUTE, p.server_time) >=30 AND DATEPART(MINUTE, p.server_time) < 45 THEN '30...44'
WHEN DATEPART(MINUTE, p.server_time) >=45 AND DATEPART(MINUTE, p.server_time) < 60 THEN '45...59'
END AS mRange
FROM payments AS p )
SELECT CONVERT(DATE, GETDATE()) AS ReportDate,
YEAR(p.server_time) AS ServerYear,
MONTH(p.server_time) AS ServerMonth,
DAY(p.server_time) AS ServerDay,
DATEPART(HOUR, p.server_time) AS ServerHour,
p.mRange AS ServerMinuteRange,
COUNT(p2._total) / NULLIF(COUNT(p1._total) / 100.0, 0.0) AS Ratio
FROM mCTE AS p
LEFT OUTER JOIN mCTE AS p1 ON p.row_id = p1.row_id AND (p._state / 1000) <> 0
LEFT OUTER JOIN mCTE AS p2 ON p.row_id = p2.row_id AND (p._state / 1000) = 1 AND p2.row_id IS NOT NULL
WHERE CONVERT(DATE, p.server_time) = CONVERT(DATE, GETDATE())
GROUP BY YEAR(p.server_time), MONTH(p.server_time), DAY(p.server_time), DATEPART(HOUR, p.server_time), p.mRange
结果
ReportDate ServerYear ServerMonth ServerDay ServerHour ServerMinuteRange Ratio
2017-04-27 2017 4 27 13 0...14 0.000000000000000000
2017-04-27 2017 4 27 13 15...29 18.181818181818181818
2017-04-27 2017 4 27 13 30...44 0.000000000000000000
2017-04-27 2017 4 27 13 45...59 0.000000000000000000
2017-04-27 2017 4 27 14 0...14 18.181818181818181818
2017-04-27 2017 4 27 14 15...29 0.000000000000000000
2017-04-27 2017 4 27 14 30...44 0.000000000000000000
2017-04-27 2017 4 27 14 45...59 0.000000000000000000
2017-04-27 2017 4 27 15 0...14 0.000000000000000000
2017-04-27 2017 4 27 15 15...29 0.000000000000000000
2017-04-27 2017 4 27 15 30...44 0.000000000000000000
2017-04-27 2017 4 27 15 45...59 0.000000000000000000
2017-04-27 2017 4 27 16 0...14 0.000000000000000000
2017-04-27 2017 4 27 16 15...29 0.000000000000000000
<强>更新强>
每三个小时,只需更改CTE中的逻辑并从group by子句中删除分钟
;WITH mCTE AS (
SELECT p.*, CASE WHEN DATEPART(HOUR, p.server_time) >=0 AND DATEPART(HOUR, p.server_time) < 3 THEN '0...2'
WHEN DATEPART(HOUR, p.server_time) >= 3 AND DATEPART(HOUR, p.server_time) < 6 THEN '3...5'
WHEN DATEPART(HOUR, p.server_time) >= 6 AND DATEPART(HOUR, p.server_time) < 9 THEN '6...8'
WHEN DATEPART(HOUR, p.server_time) >= 9 AND DATEPART(HOUR, p.server_time) < 12 THEN '9...11'
WHEN DATEPART(HOUR, p.server_time) >= 12 AND DATEPART(HOUR, p.server_time) < 15 THEN '12...14'
WHEN DATEPART(HOUR, p.server_time) >= 15 AND DATEPART(HOUR, p.server_time) < 18 THEN '15...17'
WHEN DATEPART(HOUR, p.server_time) >= 18 AND DATEPART(HOUR, p.server_time) < 21 THEN '18...20'
WHEN DATEPART(HOUR, p.server_time) >= 21 AND DATEPART(HOUR, p.server_time) < 24 THEN '21...23'
END AS mRange
FROM payments AS p )
SELECT CONVERT(DATE, GETDATE()) AS ReportDate,
YEAR(p.server_time) AS ServerYear,
MONTH(p.server_time) AS ServerMonth,
DAY(p.server_time) AS ServerDay,
p.mRange AS ServerHourRange,
COUNT(p2._total) / NULLIF(COUNT(p1._total) / 100.0, 0.0) AS Ratio
FROM mCTE AS p
LEFT OUTER JOIN mCTE AS p1 ON p.row_id = p1.row_id AND (p._state / 1000) <> 0
LEFT OUTER JOIN mCTE AS p2 ON p.row_id = p2.row_id AND (p._state / 1000) = 1 AND p2.row_id IS NOT NULL
WHERE CONVERT(DATE, p.server_time) = CONVERT(DATE, GETDATE())
GROUP BY YEAR(p.server_time), MONTH(p.server_time), DAY(p.server_time), p.mRange
结果
ReportDate ServerYear ServerMonth ServerDay ServerHourRange Ratio
2017-04-28 2017 4 26 18...20 1.754385964912280701
2017-04-28 2017 4 27 12...14 5.479452054794520547
2017-04-28 2017 4 27 15...17 0.000000000000000000
更新2
要仅显示最后3小时的数据,您只需将WHERE子句更改为
WHERE p.server_time >= DATEADD(HOUR, -3, GETDATE())