sql - 如何组合 Count 和 Min 函数

标签 sql sql-server

我尝试同时进行分组、计数和分类。 我的 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/

相关文章:

java - SQLException 参数索引超出范围

mysql - 如何获得非重复项?

c# - 保持 SQL Server 表在数据库中排序

sql - 如何针对同一数据库运行应用程序的多个版本?

sql-server - Testcafe - 如何在所有装置运行后运行代码

sql-server - 将旧的 "+"Join 转换为 SQL Server 语法?

sql - Active Record 查询导致 SQLException 接近 ","语法错误

mysql - 正确使用不同的计数来自两个不同表的数据

php - 如何在函数选择中使用IF和ELSE?

c# - 在 Ado.net C# 中动态构建 Where 子句