简体   繁体   English

如果行中的其他值满足特定条件,则使用 SQL 聚合数据

[英]Aggregating data with SQL if other values in rows meet certain criteria

I'm pretty new to SQL and am trying to figure out if there's a way to aggregate the date into new column.我对 SQL 很陌生,我想弄清楚是否有办法将日期聚合到新列中。 For instance, below is a sample set of date.例如,下面是一组日期样本。 I want to take all the type '2' and create new columns我想采用所有类型 '2' 并创建新列

Mem   Balance  Type  New
 1     2500     2    0
 2     722      66   1
 3     9422      1   0
 4     122       2   1
 5     788      66   1

So instead of having it like above, it would be like所以不是像上面那样,而是像

Mem   Type2Balance  Type66Balance   Type2New    Type66New
1       2622            0            1             0
2         0            1510          0             2

Is there a way to do this in SQL?有没有办法在 SQL 中做到这一点? I thought maybe using IF statements within a case statement?我想也许在 case 语句中使用 IF 语句? I'm not asking for it to be done for me, more of looking for specifics that I can read about to make this happen.我不是要求为我完成它,更多的是寻找我可以阅读的细节来实现这一点。 Thank you!谢谢!

It seems like you want:看起来你想要:

select
    case when type = 2 then sum(balance) else 0 end type2balance,
    case when type = 66 then sum(balance) else 0 end type66balance,
    case when type = 2 then sum(new) else 0 end type2new,
    case when type = 66 then sum(new) else 0 end type66new
from mytable
where type in (2, 66)
group by type

I am not sure what logic you want for column Mem in the resulset.我不确定结果集中的Mem列需要什么逻辑。 It that's just a row number, then:这只是一个行号,然后:

select
    row_number() over(order by type) mem,
    case when type = 2 then sum(balance) else 0 end type2balance,
    case when type = 66 then sum(balance) else 0 end type66balance,
    case when type = 2 then sum(new) else 0 end type2new,
    case when type = 66 then sum(new) else 0 end type66new
from mytable
where type in (2, 66)
group by type

We could put the conditional expression on type inside the aggregate function, but it does not really make sense since you are grouping by type already.我们可以将条件表达式放在聚合 function 中的type上,但这实际上没有意义,因为您已经按type分组。 In that case, that would be:在这种情况下,这将是:

select
    row_number() over(order by type) mem,
    sum(case when type = 2 then balance else 0 end) type2balance,
    sum(case when type = 66 then balance else 0 end) type66balance,
    sum(case when type = 2 then new else 0 end) type2new,
    sum(case when type = 66 then new else 0 end) type66new
from mytable
where type in (2, 66)
group by type

With conditional aggregation:使用条件聚合:

select min(mem) mem,
  sum(case when type = 2 then balance else 0 end) Type2Balance,
  sum(case when type = 66 then balance else 0 end) Type66Balance,
  sum(case when type = 2 then new else 0 end) Type2New,
  sum(case when type = 66 then new else 0 end) Type66New
from tablename
where type in (2, 66)
group by type

See the demo .请参阅演示
Results:结果:

| mem | Type2Balance | Type66Balance | Type2New | Type66New |
| --- | ------------ | ------------- | -------- | --------- |
| 1   | 2622         | 0             | 1        | 0         |
| 2   | 0            | 1510          | 0        | 2         |

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM