我创建了以下查询来制作一个“频率图”,显示特定值之间的行数:
SELECT CONCAT('0 - ', M.MaxField1) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1<M.MaxField1
UNION ALL
SELECT CONCAT((M.MaxField1), ' - ', (M.MaxField1*2)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1) AND P.Field1<(M.MaxField1*2)
UNION ALL
SELECT CONCAT((M.MaxField1*2), ' - ', (M.MaxField1*3)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*2) AND P.Field1<(M.MaxField1*3)
UNION ALL
SELECT CONCAT((M.MaxField1*3), ' - ', (M.MaxField1*4)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*3) AND P.Field1<(M.MaxField1*4)
UNION ALL
SELECT CONCAT((M.MaxField1*4), ' - ', (M.MaxField1*5)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*4) AND P.Field1<(M.MaxField1*5)
UNION ALL
SELECT CONCAT((M.MaxField1*5), ' - ', (M.MaxField1*6)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*5) AND P.Field1<(M.MaxField1*6)
UNION ALL
SELECT CONCAT((M.MaxField1*6), ' - ', (M.MaxField1*7)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*6) AND P.Field1<(M.MaxField1*7)
UNION ALL
SELECT CONCAT((M.MaxField1*7), ' - ', (M.MaxField1*8)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*7) AND P.Field1<(M.MaxField1*8)
UNION ALL
SELECT CONCAT((M.MaxField1*8), ' - ', (M.MaxField1*9)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*8) AND P.Field1<(M.MaxField1*9)
UNION ALL
SELECT CONCAT((M.MaxField1*9), ' - ', (M.MaxField1*10)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M
WHERE P.Field1>=(M.MaxField1*9)
任何优化此脚本的建议都非常耗时吗?
答案 0 :(得分:1)
试试这个(未经测试):
WITH cte(MaxField1) AS (
SELECT (MAX(Field1)/10) AS MaxField1 FROM TABLE1
)
SELECT
'Part',
COUNT(*)
FROM (
SELECT
CONCAT((MaxField1*M.bucket), ' - ', (MaxField1*(M.bucket+1))) AS 'Part',
Field1 / cte.MaxField1 AS bucket
FROM TABLE1, cte
) AS M
GROUP BY 'Part'
对于没有CTE表达式的MySQL用户,请尝试以下方法:
SELECT
'Part',
COUNT(*)
FROM (
SELECT
CONCAT((MaxField1*M.bucket), ' - ', (MaxField1*(M.bucket+1))) AS 'Part',
Field1 / cte.MaxField1 AS bucket
FROM TABLE1,
(SELECT (MAX(Field1)/10) AS MaxField1 FROM TABLE1)cte
) AS M
GROUP BY 'Part'
荷兰语:
SELECT
'Part',
COUNT(*)
FROM (
SELECT
CONCAT((MaxPercbeheer*M.bucket), ' - ', (MaxPercbeheer*(M.bucket+1))) AS 'Part',
(Percbeheer/cte.MaxPercbeheer) AS bucket,
MaxPercbeheer
FROM POLIS, (SELECT (MAX(Percbeheer)/10) AS MaxPercbeheer FROM POLIS) cte
) AS M
GROUP BY 'Part'
答案 1 :(得分:0)
您可以将其转换为一个查询,因为联合部分基本上是相同的查询:
create temporary table range_tmp (start int, end int);
insert into range_tmp values (0,1),(1,2),(2,3),(3,4),(4,5),(5,6),(7,8),(9,10);
SELECT CONCAT((M.MaxField1*t.start), ' - ', (M.MaxField1*t.end)) AS 'Part',COUNT(*)
FROM TABLE1 P, (SELECT (MAX(Field1)*(1/10)) AS MaxField1 FROM TABLE1) AS M, range_tmp t
WHERE (P.Field1>=(M.MaxField1*t.start) or t.start=1) AND (P.Field1<(M.MaxField1*t.end) or t.end=10)