Ask Your Question
3

What SQL query can be used to identify the customer IDs that are new for each day and were not present in the previous day?

asked 2023-06-25 00:35:27 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-25 00:36:02 +0000

plato gravatar image

It depends on the structure of the database and the data model. However, a possible SQL query to identify new customer IDs for each day could be:

SELECT DISTINCT currentday.customerid FROM customertable AS currentday LEFT JOIN customertable AS previousday ON currentday.customerid = previousday.customerid WHERE currentday.date = 'current date' AND previousday.date = 'previous date' AND previousday.customerid IS NULL;

This query uses a self-join to compare the customertable for the current day with the customertable for the previous day. The LEFT JOIN ensures that all customer IDs for the current day are included in the result, while the WHERE clause filters out those IDs that were already present in the previous day (i.e., where the previousday.customerid value is not null). The DISTINCT keyword eliminates any duplicate customer IDs in the result.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-25 00:35:27 +0000

Seen: 10 times

Last updated: Jun 25 '23