[英]How can I find records where a field appears once in a column or multiple times in the column but only once between a set of dates?
I am trying to write a query that brings up the details (invoiceNumber, name, phone & email) from a table but only where the entries match these specific requirements: 我正在尝试编写一个查询,该查询从表中调出详细信息(发票编号,姓名,电话和电子邮件),但仅在条目符合以下特定要求的情况下:
Return the rows where the email address appears only once in the whole table from any date AND Return the rows where the email address multiple times WHERE the email has only been entered ONCE within the last 3 months. 从任何日期返回整个表中电子邮件地址仅出现一次的行,并返回过去三个月仅一次输入电子邮件的位置多次发送电子邮件地址的行。
I am fairly sure that this requires nested statements but I have no idea how to go about setting it up. 我相当确定这需要嵌套语句,但是我不知道如何进行设置。
Any help would be greatly appreciated! 任何帮助将不胜感激!
Approach this by thinking about the email addresses. 通过考虑电子邮件地址来解决此问题。 You can determine the frequency of appearance by doing a group by:
您可以通过以下方式进行分组来确定出现频率:
select emailaddress, count(*) as TotalCnt,
sum(case when invoicedate >= sysdate - 90 then 1 else 0 end) as LastThreeMonths
from t
group by emailaddress;
This gives you the information you need. 这会为您提供所需的信息。 The following query joins this back to the original table to get the rows you are looking for:
以下查询将其联接回原始表以获取您要查找的行:
select t.*
from t join
(select emailaddress, count(*) as TotalCnt,
sum(case when invoicedate >= sysdate - 90 then 1 else 0 end) as LastThreeMonths
from t
group by emailaddress
) e
on t.emailaddress = e.emailaddress
where e.TotalCnt = 1 or e.LastThreeMonths = 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.