如何结合计数和最小功能

时间:2019-01-24 12:09:59

标签: sql sql-server

我尝试同时分组,计数和分类。 我的桌子看起来像

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?

2 个答案:

答案 0 :(得分:4)

只需将简单的count语句与distinct一起使用

select count(distinct Email_ID) from table1

select ticketId, count(distinct ticket.Email) 
from Ticket
group by TicketId

这将为您提供每个TicketId的不同电子邮件数量

答案 1 :(得分:2)

这是您想要的吗?

session.enable.phishing.protection = true
session.phishing.protected.attributes = CAS_LOGIN, HTTPS_INITIAL, LAST_PATH, OPEN_ID_CONNECT_SESSION