SQL查询语句中的除法

发布于 2024-10-12 01:00:43 字数 1248 浏览 0 评论 0原文

我有两个查询返回单列双精度值:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女猫性小仙女 2024-10-19 01:00:43

我认为您想要使用 LEAD 窗口函数,而不是获取两组并尝试将它们连接起来。这允许您引用同一窗口中的另一行(即具有匹配的分区依据)。类似于:

select title, scale / next_scale
from ( select title, scale,
              lead(scale) over(partition by title order by scale asc) as next_scale,
              row_number() over(partition by title order by scale asc) as agg_row
       from signatures
     ) agg
where agg_row = 1;

这里,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 matching partition by). Something like:

select title, scale / next_scale
from ( select title, scale,
              lead(scale) over(partition by title order by scale asc) as next_scale,
              row_number() over(partition by title order by scale asc) as agg_row
       from signatures
     ) agg
where agg_row = 1;

Here, lead(scale) takes the value from the scale column from the next row to be output in the same window, i.e. the next-most scale in order. We still need to project row_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.

马蹄踏│碎落叶 2024-10-19 01:00:43

您需要为 SQL 提供某种方式来了解每列中的哪个数字要除以哪个数字。试试这个:

    SELECT first.title, (first.scale / second.scale) ratio
      FROM 
           (SELECT scale, title
              FROM (SELECT title, scale,
                           dense_rank() OVER 
                             (PARTITION BY title ORDER BY scale ASC) AS r
                      FROM signatures) t
             WHERE r = 1) first
INNER JOIN
           (SELECT scale, title
              FROM (SELECT title, scale,
                           dense_rank() OVER
                             (PARTITION BY title ORDER BY scale ASC) AS r
                      FROM signatures) t
             WHERE r = 2) second
        ON first.title = second.title

正如 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:

    SELECT first.title, (first.scale / second.scale) ratio
      FROM 
           (SELECT scale, title
              FROM (SELECT title, scale,
                           dense_rank() OVER 
                             (PARTITION BY title ORDER BY scale ASC) AS r
                      FROM signatures) t
             WHERE r = 1) first
INNER JOIN
           (SELECT scale, title
              FROM (SELECT title, scale,
                           dense_rank() OVER
                             (PARTITION BY title ORDER BY scale ASC) AS r
                      FROM signatures) t
             WHERE r = 2) second
        ON first.title = second.title

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文