我知道两个queries
知道鱿鱼日志的进出缓存
这是我的第一个查询
select SUBSTRING_INDEX(url, '/', 3),count(resultstatuscode) from squid where resultstatuscode like '%MISS%' group by SUBSTRING_INDEX(url, '/', 3);
这是我的第二个问题
select SUBSTRING_INDEX(url, '/', 3),count(resultstatuscode) from squid group by SUBSTRING_INDEX(url, '/', 3);
我只是想将第二个query
划分为第一个query
有人可以帮我解决这个问题吗?
答案 0 :(得分:0)
最简单的解决方案:使用url子字符串连接两个查询,然后就可以计算比率。
SELECT a.url, a.codecount / b.codecount AS percentage
FROM
(SELECT SUBSTRING_INDEX(url, '/', 3) AS url,
COUNT(resultstatuscode) AS codecount
FROM squid
WHERE resultstatuscode LIKE '%MISS%'
GROUP BY SUBSTRING_INDEX(url, '/', 3)) a
JOIN
(SELECT SUBSTRING_INDEX(url, '/', 3) AS url,
COUNT(resultstatuscode) AS codecount
FROM squid
GROUP BY SUBSTRING_INDEX(url, '/', 3)) b
ON a.url = b.url
只有一个子选择的替代解决方案"准备"百分比计算的数据:
SELECT t.url, SUM(t.miss) / SUM(t.total) AS percentage
FROM (SELECT SUBSTRING_INDEX(url, '/', 3) AS url,
CASE WHEN resultstatuscode LIKE '%MISS%' THEN 1 ELSE 0 END AS miss
1 AS total) t
GROUP BY t.url