0

我有两个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);

我只想把我的第 2 分到query第 1query

有人可以帮我解决这个问题吗?

4

1 回答 1

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
于 2012-08-26T19:26:53.027 回答