Postgres不同的行,同时总结

发布于 2025-02-14 01:52:22 字数 1597 浏览 0 评论 0原文

我有一个与此相似的数据集。我需要为客户端挑选最新的元数据(更大的执行时间=更新),包括数量和最新的执行时间和数量> 0,

| Name     | Quantity | Metadata | Execution time |
| -------- | ---------|----------|----------------|
| Neil     | 1        | [1,3]    |  4             |
| James    | 1        | [2,18]   |  5             |
| Neil     | 1        | [4, 1]   |  6             |
| Mike     | 1        | [5, 42]  |  7             |
| James    | -1       | Null     |  8             |
| Neil     | -1       | Null     |  9             |

例如,查询需要返回:

| Name     | Summed Quantity | Metadata | Execution time |
| -------- | ----------------|----------|----------------|
| James    | 0               | [2,18]   |  5             |
| Neil     | 1               | [4, 1]   |  6             |
| Mike     | 1               | [5, 42]  |  7             |

我的查询无法正常工作,因为它无法正确返回数量的总和。

SELECT 

distinct on (name) name, 
(
    SELECT 
      cast(
        sum(quantity) as int
        
      )
  ) as summed_quantity,
     
  meta,
  execution_time  
FROM 
  table 
where 
 quantity > 0
group by 
  name,  
  meta, 
  execution_time 
order by 
  name, 
  execution_time desc;

此查询给出了IE的结果

| Name     | Summed Quantity | Metadata | Execution time |
| -------- | ----------------|----------|----------------|
| James    | 1               | [2,18]   |  5             |
| Neil     | 1               | [4, 1]   |  6             |
| Mike     | 1               | [5, 42]  |  7             |

。 0从何处,不添加子查询中的数量(我假设是由于独特的子句),我不确定如何修复查询以产生所需的输出。

I have a dataset that is similar to this. I need to pick out the most recent metadata (greater execution time = more recent) for a client including the sum of quantities and the latest execution time and meta where the quantity > 0

| Name     | Quantity | Metadata | Execution time |
| -------- | ---------|----------|----------------|
| Neil     | 1        | [1,3]    |  4             |
| James    | 1        | [2,18]   |  5             |
| Neil     | 1        | [4, 1]   |  6             |
| Mike     | 1        | [5, 42]  |  7             |
| James    | -1       | Null     |  8             |
| Neil     | -1       | Null     |  9             |

Eg the query needs to return:

| Name     | Summed Quantity | Metadata | Execution time |
| -------- | ----------------|----------|----------------|
| James    | 0               | [2,18]   |  5             |
| Neil     | 1               | [4, 1]   |  6             |
| Mike     | 1               | [5, 42]  |  7             |

My query doesn't quite work as it's not returning the sum of the quantities correctly.

SELECT 

distinct on (name) name, 
(
    SELECT 
      cast(
        sum(quantity) as int
        
      )
  ) as summed_quantity,
     
  meta,
  execution_time  
FROM 
  table 
where 
 quantity > 0
group by 
  name,  
  meta, 
  execution_time 
order by 
  name, 
  execution_time desc;

This query gives a result of

| Name     | Summed Quantity | Metadata | Execution time |
| -------- | ----------------|----------|----------------|
| James    | 1               | [2,18]   |  5             |
| Neil     | 1               | [4, 1]   |  6             |
| Mike     | 1               | [5, 42]  |  7             |

ie it's just taking the quantity > 0 from the where and not adding up the quantities in the sub query (i assume because of the distinct clause) I'm unsure how to fix my query to produce the desired output.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

杀お生予夺 2025-02-21 01:52:22

这可以使用窗口函数(因此使用单个数据)

select
       name
     , sum_qty
     , metadata
     , execution_time
from (
     select 
            *
          , sum(Quantity) over(partition by name) sum_qty
          , row_number() over(partition by name, case when quantity > 0 then 1 else 0 end
                              order by Execution_time DESC) as rn
     from mytable
     ) d
where rn = 1 and quantity > 0
order by name

结果

+-------+---------+----------+----------------+
| name  | sum_qty | metadata | execution_time |
+-------+---------+----------+----------------+
| James |       0 | [2,18]   |              5 |
| Mike  |       1 | [5,42]   |              7 |
| Neil  |       1 | [4,1]    |              6 |
+-------+---------+----------+----------------+

db<> fiddle 此处

This can be achieved using window functions (hence with a single pass of the data)

select
       name
     , sum_qty
     , metadata
     , execution_time
from (
     select 
            *
          , sum(Quantity) over(partition by name) sum_qty
          , row_number() over(partition by name, case when quantity > 0 then 1 else 0 end
                              order by Execution_time DESC) as rn
     from mytable
     ) d
where rn = 1 and quantity > 0
order by name

result

+-------+---------+----------+----------------+
| name  | sum_qty | metadata | execution_time |
+-------+---------+----------+----------------+
| James |       0 | [2,18]   |              5 |
| Mike  |       1 | [5,42]   |              7 |
| Neil  |       1 | [4,1]    |              6 |
+-------+---------+----------+----------------+

db<>fiddle here

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