-3

基本上,我想知道 Excel 的 COUNTIF(Range,Criteria) 的 SQL 等效项

我想要这个输出

CALL ID |  Name  | Phone Number | # of Occurrences    
  0001  | Name1  |  0000000000  |        2    
  0002  | Name2  |  1111111111  |        1    
  0003  | Name1  |  0000000000  |        2    
  0004  | Name3  |  2222222222  |        2    
  0005  | Name5  |  4444444444  |        1    
  0006  | Name4  |  3333333333  |        1    
  0007  | Name3  |  2222222222  |        2

不是这个输出

CALL ID |  Name  | Phone Number | # of Occurrences    
  0001  | Name1  |  0000000000  |        2    
  0002  | Name2  |  1111111111  |        1    
  0004  | Name3  |  2222222222  |        2    
  0005  | Name5  |  4444444444  |        1    
  0006  | Name4  |  3333333333  |        1    

这是一个简化的表格。这是我的复杂表,这是结果 结果

我想要的是避免有 2 个约会,而是将它们列在单独的行中,以便约会只有 1

这是查询

SELECT
a."Call ID" as "Call ID",
CONCAT(a.Campaign, '-', a."CONTACT ID") as IDENTIFIER,
REPLACE(a."Campaign",'CW-','') as "Campaign",
a."DNIS" as "DNIS",
a."CONTACT ID" as "CONTACT ID",
a."first_name" as "first_name",
a."last_name" as "last_name",
a."city" as "city",
a."street" as "street",
a."zip" as "zip",
a."state" as "state",
a."number2" as "number2",
a."number1" as "number1",
a."Customer ID - real" as "Customer ID - real",
COUNT(a."Call ID") as "Dial Attempts"
FROM  "Five9 Calls" a 
group by 
CONCAT(a.Campaign, '-', a."CONTACT ID"),
REPLACE(a."Campaign",'CW-',''),
a."CONTACT ID",
a."first_name",
a."last_name",
a."city",
a."street",
a."zip",
a."state",
a."number2",
a."number1"
4

3 回答 3

1

Declan 的回答为您提供了与每个电话号码关联的姓名数量(每个电话号码一行)。

如果保持相同数量的行对您很重要(即您在问题中描述的相同布局),您可以使用OVER子句。

SELECT NAME, [PHONE NUMBER], COUNT(NAME) OVER (PARTITION BY [PHONE NUMBER]) AS OCCUR
FROM YOURTABLE

With OVER there's no need for a GROUP BY.

于 2013-08-19T19:55:08.007 回答
0

如果您需要计算每个电话号码的出现次数,这是我可以从您的数据中收集到的,您可以使用以下方法。

SELECT [PHONE NUMBER],COUNT(NAME) AS OCCUR
FROM YOURTABLE
GROUP BY [PHONE NUMBER]
于 2013-08-19T19:50:05.713 回答
0
select call_id, name, phone_number, count() over (partition by name, phone_id order by name asc, phone_id asc) occur
From yourtable
于 2013-08-19T19:56:19.843 回答