基本上,我想知道Excel的COUNTIF的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
这是一张简化的表格。这是我的复杂表格,这就是结果 Result
我想要的是避免使用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"
答案 0 :(得分:1)
Declan的答案为您提供了与每个电话号码相关联的姓名数量(每个电话号码一行)。
如果您保持相同的行数(即您在问题中描述的布局相同)很重要,则可以使用OVER
子句。
SELECT NAME, [PHONE NUMBER], COUNT(NAME) OVER (PARTITION BY [PHONE NUMBER]) AS OCCUR
FROM YOURTABLE
使用OVER
,不需要GROUP BY
。
答案 1 :(得分:0)
如果您需要计算每个电话号码的出现次数(我可以从您的数据中收集),您可以使用以下内容。
SELECT [PHONE NUMBER],COUNT(NAME) AS OCCUR
FROM YOURTABLE
GROUP BY [PHONE NUMBER]
答案 2 :(得分:0)
select call_id, name, phone_number, count() over (partition by name, phone_id order by name asc, phone_id asc) occur
From yourtable