计算每个客户 ID 的电子邮件类型

发布于 2024-12-28 07:16:14 字数 657 浏览 1 评论 0原文

我有一个人们可以用来向我们的客户发送电子邮件的表格。用户可以选择在两条预设消息(消息“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞琼窗梦回ˉ 2025-01-04 07:16:14
select
  CUST_ID,
  sum(iif(EMAIL_NUM = 1, 1, 0)) as EMAIL_1_COUNT,
  sum(iif(EMAIL_NUM = 2, 1, 0)) as EMAIL_2_COUNT
from
  RECORDS
group by
  CUST_ID
select
  CUST_ID,
  sum(iif(EMAIL_NUM = 1, 1, 0)) as EMAIL_1_COUNT,
  sum(iif(EMAIL_NUM = 2, 1, 0)) as EMAIL_2_COUNT
from
  RECORDS
group by
  CUST_ID
野侃 2025-01-04 07:16:14

另一个需要考虑的选项是使用带有 TRANSFORM 的数据透视查询,

TRANSFORM NZ(Count(RECORDS.Email_NUm),0) AS CountOfEmail_NUm
SELECT RECORDS.CUST_ID
FROM RECORDS
GROUP BY RECORDS.CUST_ID
PIVOT RECORDS.Email_NUm;

但这会生成 CUST_ID12 列头。但是,如果您有另一个包含电子邮件类型的表,那么它可能是值得的(特别是如果您的电子邮件类型数量超过 2),

SQL 可能如下

TRANSFORM NZ(Count(r.Email_NUm),0) AS CountOfEmail_NUm
SELECT r.CUST_ID
FROM RECORDS r
     INNER JOIN EMAIL_TYPES et
     ON r.Email_NUm = et.Email_NUm
GROUP BY r.CUST_ID
PIVOT et.TYPE_NAME;

所示

   CUST_ID | Work | Home 
   -------   ----   ----
   0000    | 3    | 1
   0001    | 0    | 1
   0002    | 2    | 0
   0003    | 0    | 3

Another option to consider is to use a pivot query with TRANSFORM

TRANSFORM NZ(Count(RECORDS.Email_NUm),0) AS CountOfEmail_NUm
SELECT RECORDS.CUST_ID
FROM RECORDS
GROUP BY RECORDS.CUST_ID
PIVOT RECORDS.Email_NUm;

This would however produce column heads of CUST_ID , 1, and 2. 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

TRANSFORM NZ(Count(r.Email_NUm),0) AS CountOfEmail_NUm
SELECT r.CUST_ID
FROM RECORDS r
     INNER JOIN EMAIL_TYPES et
     ON r.Email_NUm = et.Email_NUm
GROUP BY r.CUST_ID
PIVOT et.TYPE_NAME;

Producing this output

   CUST_ID | Work | Home 
   -------   ----   ----
   0000    | 3    | 1
   0001    | 0    | 1
   0002    | 2    | 0
   0003    | 0    | 3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文