简体   繁体   English

SQL聚合具有相同id的行,辅助列中具有特定值

[英]SQL aggregate rows with same id , specific value in secondary column

I'm looking to filter out rows in the database (PostgreSQL) if one of the values in the status column occurs. 如果status列中的值之一发生,我想过滤数据库(PostgreSQL)中的行。 The idea is to sum the amount column if the unique reference only has a status equals to 1 . 想法是,如果唯一referencestatus仅等于1则对amount列求和。 The query should not SELECT the reference at all if it has also a status of 2 or any other status for that matter. 如果查询的状态也为2或与此相关的任何其他status ,则查询根本不应SELECTreference status refers to the state of the transaction. status是指交易的状态。

Current data table: 当前数据表:

reference | amount | status
   1         100       1       
   2         120       1
   2        -120       2
   3         200       1
   3        -200       2
   4         450       1

Result: 结果:

amount | status
  550      1

I've simplified the data example but I think it gives a good idea of what I'm looking for. 我简化了数据示例,但我认为它很好地说明了我要查找的内容。 I'm unsuccessful in selecting only references that only have status 1 . 我仅选择状态为1 references失败。 I've tried sub-queries, using the HAVING clause and other methods without success. 我尝试过使用HAVING子句和其他方法的子查询,但HAVING成功。

Thanks 谢谢

Here's a way using not exists to sum all rows where the status is 1 and other rows with the same reference and a non 1 status do not exist. 这是一种使用not exists的方法来对状态为1的所有行求和,而其他具有相同引用和非1状态的行不存在。

select sum(amount) from mytable t1
where status = 1
and not exists (
    select 1 from mytable t2
    where t2.reference = t1.reference
    and t2.status <> 1
)
SELECT SUM(amount)
 FROM table
WHERE reference NOT IN (
 SELECT reference
 FROM table
 WHERE status<>1
)

The subquery SELECTs all reference s that must be excluded, then the main query sums everything except them 子查询选择所有必须排除的reference s,然后主查询将除它们之外的所有内容相加

select sum (amount) as amount
from (
    select sum(amount) as amount
    from t
    group by reference
    having not bool_or(status <> 1)
) s;
 amount 
--------
    550

You could use windowed functions to count occurences of status different than 1 per each group: 您可以使用windowed functions来计算每个组中状态发生的次数不等于1的情况:

SELECT SUM(amount) AS amount
FROM (SELECT *,COUNT(*) FILTER(WHERE status<>1) OVER(PARTITION BY reference) cnt
      FROM tc) AS sub
WHERE cnt = 0;

Rextester Demo Rextester演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server:排除表中的行如果有另一行具有相同的col值和特定的二级列值 - SQL Server : excluding rows from a table IF there is another row with the same col value and a particular secondary column value SQL 计算列中具有相同值的行并按 id 分组? - SQL count rows with same value in column and group by id? MySQL选择具有相同id且在列中具有不同值的不同行,并在另一列中排除特定值 - MySQL Select distinct rows with same id with different value in a column and excluded a specific value in another column SQL - 聚合一些列并将 id 映射到特定列 - SQL - aggregate some columns and map id to specific column SQL - Select 行在一列中具有相同的值,并在另一列中选择具有特定值的行 - SQL - Select rows with same value in one column and choose the one with specific value in other column 需要一个 SQL 选择语句来返回在一个列中具有相同 id 而在另一列中具有不同值的行 - Need a SQL select statement to return rows that have the same id in one column and distinct value in another column SQL查找id-s,其中列的值等于特定行上的值 - SQL Find id-s where value of column equals a value on specific rows 合并特定列中具有相同值的行 - Merge rows with same value in a specific column 将 SQL 行聚合为单列 - Aggregate SQL Rows into single column 使用相同的ID列获取特定的COUNT值 - Get specific COUNT value with same ID column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM