【问题标题】:Select from same column but multiples values and number of records with one specific value从同一列中选择多个值和具有一个特定值的记录数
【发布时间】:2021-06-17 18:32:51
【问题描述】:
ID  Customer    Status
1   ABC 1
2   ABC 2
3   ABC 3
4   ABC 1
5   PQR 1
6   PQR 2
7   PQR 3
8   XYZ 1
9   XYZ 3

我想选择同时具有值​​“status=1”和“Status=2”的客户,以及 Status=1 的同一客户的总条目数。 所以结果会是,

Customer   totalEntryStatus1
ABC        2
PQR        1

我该怎么做。

谢谢!

【问题讨论】:

  • 同时标记您正在使用的数据库

标签: sql select


【解决方案1】:
select Customer, count(case when status = 1 then 1 end) totalEntryStatus1
from table
where Status in (1,2)
group by Customer 
having count(distinct Status) = 2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多