我尝试同时进行分组、计数和分类。 我的 table 看起来像
Email_ID Email_Address Date
-------- --------------- --------
1000 a@xxx.com 2018-12-01
1001 b@xxx.com 2018-12-01
1002 c@xxx.com 2018-12-02
1003 d@xxx.com 2018-12-02
1003 d@xxx.com 2018-12-03
1001 b@xxx.com 2018-12-04
1000 a@xxx.com 2018-12-05
1004 d@xxx.com 2018-12-05
I categorize my Products by email:
a@xxx.com 和 b@xxx.com -> 预订 c@xxx.com 和 d@xxx.com -> 玩具
I need to find total number of emails for books and for toys without having duplicate IDs
Here is my code:
```` Select
(Select MIN(id) From ticket ti
Where ti.ID = t.ID) As Customer_ID,
Case
When Email in (a@xxx.com and b@xxx.com) Then 'Book'
When Email in (c@xxx.com and d@xxx.com) Then 'Toys'
End As Product
From ticket t
Where
Date > '2018-12-01' And Date < '2018-12-31'
````
How can I do the count here?
最佳答案
只需使用简单的count
语句和distinct
select count(distinct Email_ID) from table1
<小时/>
select ticketId, count(distinct ticket.Email)
from Ticket
group by TicketId
这将为您提供每个 TicketId 的不同电子邮件数量
关于sql - 如何组合 Count 和 Min 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54346368/