SQLite:如何创建两个查询的不相关元素的组合?
SQLite: How to create a combination of unrelated elements of two queries?
我有一个 table 需要从中获取一些指标。
例如我有以下 table:
meas_count
skippings
links
extra
10
8
4.2
some
10
9
5.8
some
10
9
5.8
some_2
11
8
4.2
some
11
8
5.8
some
11
9
5.9
some
我需要在以下表单中获取现有table的视图,以便进一步工作:
meas_count
skippings
links_min
links_max
10
8
0
4
10
8
4
5
10
8
5
6
10
9
0
4
10
9
4
5
10
9
5
6
11
8
0
4
11
8
4
5
11
8
5
6
11
9
0
4
11
9
4
5
11
9
5
6
目前我有 2 个查询,我需要合并它们的结果以获得我需要的结果。
第一个请求:
SELECT meas_count,skippings FROM current_stats GROUP BY meas_count,skippings
创建以下内容:
meas_count
skippings
10
8
10
9
11
8
11
9
第二个请求:
SELECT
LAG(rounded) OVER (ORDER BY rounded) as links_min,
rounded as links_max FROM
(SELECT * FROM
(SELECT ROUND(links, 1) as rounded FROM current_stats)
GROUP BY rounded ORDER BY rounded)
创建以下内容:
links_min
links_max
NULL
4
4
5
5
6
我需要类似集合乘法的结果...
应该执行什么查询来获得我需要的视图的 table 结果?
我还有一个附加问题:第二个查询的执行速度是否由于内部的多个 SELECT 而变慢?
您可以通过对两个表执行 INNER JOIN
而无需指定连接条件来实现。这将为您提供两组行的所有组合。
SELECT * FROM
(
SELECT meas_count,skippings
FROM current_stats
GROUP BY meas_count,skippings)
AS one
INNER JOIN
(
SELECT LAG(rounded) OVER (ORDER BY rounded) as links_min,
rounded as links_max FROM
(SELECT * FROM
(SELECT ROUND(links, 1) as rounded FROM current_stats)
GROUP BY rounded
ORDER BY rounded
)
) AS two;
至于性能,只有在有更好的方法的情况下,这才是真正的问题。当然,嵌套 SELECT 需要时间,但当今 SQL 引擎中的查询优化器非常擅长根据您所说的来确定您的意思。
我有一个 table 需要从中获取一些指标。
例如我有以下 table:
meas_count | skippings | links | extra |
---|---|---|---|
10 | 8 | 4.2 | some |
10 | 9 | 5.8 | some |
10 | 9 | 5.8 | some_2 |
11 | 8 | 4.2 | some |
11 | 8 | 5.8 | some |
11 | 9 | 5.9 | some |
我需要在以下表单中获取现有table的视图,以便进一步工作:
meas_count | skippings | links_min | links_max |
---|---|---|---|
10 | 8 | 0 | 4 |
10 | 8 | 4 | 5 |
10 | 8 | 5 | 6 |
10 | 9 | 0 | 4 |
10 | 9 | 4 | 5 |
10 | 9 | 5 | 6 |
11 | 8 | 0 | 4 |
11 | 8 | 4 | 5 |
11 | 8 | 5 | 6 |
11 | 9 | 0 | 4 |
11 | 9 | 4 | 5 |
11 | 9 | 5 | 6 |
目前我有 2 个查询,我需要合并它们的结果以获得我需要的结果。
第一个请求:
SELECT meas_count,skippings FROM current_stats GROUP BY meas_count,skippings
创建以下内容:
meas_count | skippings |
---|---|
10 | 8 |
10 | 9 |
11 | 8 |
11 | 9 |
第二个请求:
SELECT
LAG(rounded) OVER (ORDER BY rounded) as links_min,
rounded as links_max FROM
(SELECT * FROM
(SELECT ROUND(links, 1) as rounded FROM current_stats)
GROUP BY rounded ORDER BY rounded)
创建以下内容:
links_min | links_max |
---|---|
NULL | 4 |
4 | 5 |
5 | 6 |
我需要类似集合乘法的结果...
应该执行什么查询来获得我需要的视图的 table 结果?
我还有一个附加问题:第二个查询的执行速度是否由于内部的多个 SELECT 而变慢?
您可以通过对两个表执行 INNER JOIN
而无需指定连接条件来实现。这将为您提供两组行的所有组合。
SELECT * FROM
(
SELECT meas_count,skippings
FROM current_stats
GROUP BY meas_count,skippings)
AS one
INNER JOIN
(
SELECT LAG(rounded) OVER (ORDER BY rounded) as links_min,
rounded as links_max FROM
(SELECT * FROM
(SELECT ROUND(links, 1) as rounded FROM current_stats)
GROUP BY rounded
ORDER BY rounded
)
) AS two;
至于性能,只有在有更好的方法的情况下,这才是真正的问题。当然,嵌套 SELECT 需要时间,但当今 SQL 引擎中的查询优化器非常擅长根据您所说的来确定您的意思。