[英]Count the number of initial conacts per day SQL
This is from someone with a low level of SQL knowledge using MS Access 这是由使用MS Access的SQL知识较低的人提供的
I have some data that looks like this in a table called T1: 我在名为T1的表中有一些看起来像这样的数据:
Customer ...........Visit
c…………. 01/01/2001
b…………… 01/01/2001
b…………… 01/01/2001
a……….. 01/01/2001
b……….. 02/01/2001
a…….. 02/01/2001
d………… 02/01/2001
e……….. 03/01/2001
d………. 03/01/2001
c……….. 03/01/2001
I've written some SQL to identify the first instance of a customer: 我编写了一些SQL来标识客户的第一个实例:
SELECT Customer , MIN(Visit) as 'First Contact'
FROM T1
GROUP BY cust;
This produces a list of the first day customers contact us 这将产生第一天客户与我们联系的清单
Customer...... 'First Contact'
a…............... 01/01/2001
b……… .... 01/01/2001
c………...... 01/01/2001
d…….......... 02/01/2001
e……...…… 03/01/2001
All fine and dandy but what I need however is a total of first contacts by day ie 一切都很好,但是我需要的是每天的首次接触总数,即
First_Contact_Date… NoOfNewContacts
01/01/2001……... ............ 3
02/01/2001…………........ 1
03/01/2001………............ 1
Basically, you are half-way there. 基本上,您已经中途了。 You just need one more aggregation:
您只需要再进行一次聚合:
SELECT FirstContactDate, COUNT(*)
FROM (SELECT Customer , MIN(Visit) as FirstContactDate
FROM T1
GROUP BY cust
) t
GROUP BY FirstContactDate
ORDER BY FirstContactDate;
A note on data types: Visit
should be stored in the database using a date
or related type. 关于数据类型的注释:
Visit
应该使用date
或相关类型存储在数据库中。 This will ensure that the order by
works correctly. 这将确保
order by
正确运行。 If you have to store a date as a string, then use the ISO standard YYYY-MM-DD format. 如果必须将日期存储为字符串,请使用ISO标准YYYY-MM-DD格式。 Then the
order by
will work correctly. 然后该
order by
将可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.