SQL查询语句中的除法
我有两个查询返回单列双精度值:
(SELECT scale
FROM (SELECT title,
scale,
dense_rank() OVER (PARTITION BY title
ORDER BY scale ASC) AS r
FROM signatures) t
WHERE r = 1)
...并且:
(SELECT scale
FROM (SELECT scale,
dense_rank() OVER (PARTITION BY title
ORDER BY scale ASC) AS r
FROM signatures) t
WHERE r = 2)
我试图选择第一个查询 (Q1) 除以第二个查询 (Q2)。即,(来自 Q1 的行 1)/(来自 Q2 的行 1)。并继续沿着其余的行走下去。
我已经尝试过:
SELECT ((SELECT scale
FROM (SELECT title,
scale,
dense_rank() OVER (PARTITION BY title
ORDER BY scale ASC) AS r
FROM signatures) t
WHERE r = 1)
/
(SELECT scale
FROM (SELECT scale,
dense_rank() OVER (PARTITION BY title
ORDER BY scale ASC) AS r
FROM signatures) t
WHERE r = 2)
)
但没有运气。 任何人都可以找到一种方法来做到这一点吗?我可以分别发送两个查询,然后运行一个循环并划分元素,但这不适用于半大型记录集。
另外,这应该不重要,但我正在使用 PostgreSQL。
I have two queries that return a single column of double precision values:
(SELECT scale
FROM (SELECT title,
scale,
dense_rank() OVER (PARTITION BY title
ORDER BY scale ASC) AS r
FROM signatures) t
WHERE r = 1)
...and:
(SELECT scale
FROM (SELECT scale,
dense_rank() OVER (PARTITION BY title
ORDER BY scale ASC) AS r
FROM signatures) t
WHERE r = 2)
I am trying to SELECT the first query (Q1) divided by the second query (Q2). I.e., (Row1 from Q1) / (Row1 from Q2). And continued down the rest of the rows.
I have tried:
SELECT ((SELECT scale
FROM (SELECT title,
scale,
dense_rank() OVER (PARTITION BY title
ORDER BY scale ASC) AS r
FROM signatures) t
WHERE r = 1)
/
(SELECT scale
FROM (SELECT scale,
dense_rank() OVER (PARTITION BY title
ORDER BY scale ASC) AS r
FROM signatures) t
WHERE r = 2)
)
But have had no luck.
Can anyone see a way to do this? I can send the two queries separately, then run through a loop and divide the elements, but that won't work for a semi-large recordset.
Also, it shouldn't matter, but I am using PostgreSQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您想要使用 LEAD 窗口函数,而不是获取两组并尝试将它们连接起来。这允许您引用同一窗口中的另一行(即具有匹配的
分区依据
)。类似于:这里,
lead(scale)
从下一行的scale
列中获取要在同一窗口中输出的值,即按顺序排列的下一个比例。我们仍然需要投影 row_number() 并对其进行过滤,以便我们只获得每个窗口中第一行的输出行,即每个标题具有最小比例的行。I think you want to use the
LEAD
window function rather than getting two sets and trying to join them. This allows you to make a reference to another row in the same window (i.e. with a matchingpartition by
). Something like:Here,
lead(scale)
takes the value from thescale
column from the next row to be output in the same window, i.e. the next-most scale in order. We still need to projectrow_number()
and filter on it so that we just get output rows for the first row in each window, i.e. the row with the least scale for each title.您需要为 SQL 提供某种方式来了解每列中的哪个数字要除以哪个数字。试试这个:
正如 OMG 评论的那样,如果您最终得到几个相同的比例值,DENSE_RANK 可能会给您带来麻烦。如果适合您的逻辑,您可能希望将每个子查询限制为每个标题一行,或者在外部查询中指定 SELECT DISTINCT,因为重复项将是精确的重复项。
You need to give SQL some way of knowing which figure in each column to divide by which. Try this:
As OMG commented, DENSE_RANK may give you trouble if you end up with several identical scale values. You may wish to limit each subquery to one row per title if that is appropriate for your logic, or alternatively specify SELECT DISTINCT in the outer query as the duplicates will be exact duplicates.