sql - 我怎样才能使这个查询更好?

标签 sql sqlite group-by having-clause

我正在通过 GALAXQL 学习 SQL http://sol.gfxile.net/galaxql.html

我正在学习第 17 课 - GROUP BY/HAVING

这是场景:

Let's look at couple of SELECT operations we haven't covered yet, namely GROUP BY and HAVING.

The syntax for these operations looks like this:

SELECT columns FROM table GROUP BY column HAVING expression

The GROUP BY command in a SELECT causes several output rows to be combined into a single row. This can be very useful if, for example, we wish to generate new statistical data as a table.

For example, to find out the highest intensities from stars for each class, we would do:

Select Class, Max(Intensity) As Brightness From Stars Group By Class Order By Brightness Desc

The HAVING operator works pretty much the same way as WHERE, except that it is applied after the grouping has been done. Thus, we could calculate the sum of brightnesses per class, and crop out the classes where the sum is higher than, say, 150.

SELECT class, SUM(intensity) AS brightness FROM stars GROUP BY class HAVING brightness < 150 ORDER BY brightness DESC

We could refer to columns that are not selected in the HAVING clause, but the results might be difficult to understand. You should be able to use the aggregate functions in the HAVING clause (for example, brightness < MAX(brightness)*0.5, but this seems to crash the current version of SQLite.

When combined with joins, GROUP BY becomes rather handy. To find out the number of planets per star, we can do:

SELECT stars.starid AS starid, COUNT(planets.planetid) AS planet_count FROM planets, stars WHERE stars.starid=planets.starid GROUP BY stars.starid

突出显示轨道最多的恒星(行星和卫星的组合)。 (请注意,验证查询有点重,因此请耐心等待 按“好的,我完成了..”)。


这是我的回答

SELECT stars.starid AS HighStar, 
(COUNT(planets.planetid) + COUNT(moons.moonid)) AS OrbitalsTotal 
FROM stars
LEFT OUTER JOIN planets
ON stars.starid = planets.starid
LEFT OUTER JOIN moons
ON planets.planetid = moons.planetid
GROUP BY stars.starid
ORDER BY OrbitalsTotal DESC;

这个查询显示轨道最多的恒星有 170 个轨道

那么:

INSERT INTO hilight SELECT result.HighStar
FROM result
INNER JOIN stars
ON result.HighStar = stars.starid
WHERE result.OrbitalsTotal = 170

我的问题是如何使这个查询更好?我不想对 170 个轨道进行硬编码,也不想创建第二个查询来插入数据。

最佳答案

SELECT stars.starid AS HighStar, 
       (COUNT(planets.planetid) + COUNT(moons.moonid)) AS OrbitalsTotal 
FROM stars
     LEFT OUTER JOIN
     planets ON stars.starid = planets.starid
     LEFT OUTER JOIN
     moons ON planets.planetid = moons.planetid
GROUP BY stars.starid
HAVING OrbitalsTotal = (SELECT MAX(Orbitals)
                        FROM (SELECT (COUNT(planets.planetid) + COUNT(moons.moonid)) Orbitals
                              FROM stars
                                   LEFT OUTER JOIN
                                   planets ON stars.starid = planets.starid
                                   LEFT OUTER JOIN
                                   moons ON planets.planetid = moons.planetid
                              GROUP BY stars.starid))

关于sql - 我怎样才能使这个查询更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18454344/

相关文章:

sql - 两列SQL的唯一组合值

mysql - 仅当所有状态为 "no"时,如何通过 request_id 选择请求?

android - 如何使 SQLite 选择查询更快

android - Flutter - InkWell 对 Flexible 内部的 onTap 没有反应

mysql - 如何获取按一列过滤的 MySQL 行

mysql - 更新查询失败,错误为 : 1175

mysql - 仅当 s.id = u.summary_id 时返回记录

database - 通过 http 和 json 同步 2 个 sqlite 表的最佳方法是什么?

list - pysparkcollect_set或collect_list与groupby

sql - 如何在不分块的情况下按周分组(查询 _trailing_ 4 周)