计算每个客户 ID 的电子邮件类型
我有一个人们可以用来向我们的客户发送电子邮件的表格。用户可以选择在两条预设消息(消息“1”或消息“2”)之间进行选择。在幕后,每次他们点击“发送”按钮时,它都会记录到“记录”表中(只要它通过错误处理程序)。
假设 RECORDS 表有 2 列:
CUST_ID EMAIL_NUM
0000 1
0000 2
0000 1
0000 1
0001 2
0002 1
0002 1
0003 2
0003 2
0003 2
我需要一个查询来计算每个 CUST_ID 的个数和个数。所以结果集应该看起来像这样:
CUST_ID EMAIL_1_COUNT EMAIL_2_COUNT
0000 3 1
0001 0 1
0002 2 0
0003 0 3
我使用了 count、group by、havings、while、union、nested select,但就像我说的,我可能把相对简单的事情复杂化了。
I have a form that people can use to send emails to our clients. The user has an option to select between two canned messages (Message "1" or Message "2"). Behind the scenes, every time they hit the "SEND" button it logs into a "RECORDS" table (so long as it makes it through the error handlers).
Let's pretend the RECORDS table has 2 columns:
CUST_ID EMAIL_NUM
0000 1
0000 2
0000 1
0000 1
0001 2
0002 1
0002 1
0003 2
0003 2
0003 2
I need a query that counts the ones and twos for each CUST_ID. So the result set should look something like this:
CUST_ID EMAIL_1_COUNT EMAIL_2_COUNT
0000 3 1
0001 0 1
0002 2 0
0003 0 3
I've used count, group bys, havings, while, union, nested selects, but like I said, I'm probably over complicating something that is relatively easy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另一个需要考虑的选项是使用带有 TRANSFORM 的数据透视查询,
但这会生成
CUST_ID
、1
和2
列头。但是,如果您有另一个包含电子邮件类型的表,那么它可能是值得的(特别是如果您的电子邮件类型数量超过 2),SQL 可能如下
所示
Another option to consider is to use a pivot query with TRANSFORM
This would however produce column heads of
CUST_ID
,1
, and2
. However if you had another table with the email types then it might be worthwhile (especially if you had a large number of email types than 2)The SQL might look like this
Producing this output