简体   繁体   English

如何查找字段在列中一次出现或在列中多次出现但在一组日期之间仅出现一次的记录?

[英]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.

相关问题 如何编写查询以返回某些列值出现多次的记录? - How to write the query that returns records where some column value appears more than once? SQL选择行,其中列值是唯一的(仅出现一次) - SQL select rows, where column value is unique (only appears once) SQL 获取单词仅出现一次的列中的字段 - SQL get fields in a column where a word appears only once 检查值是否仅在列中出现一次 - Check if a value appears only once in a column 如何检查表的一列中的条目至少在另一列中出现一次? - How can I check that an entry in one column of a tables appears at least once in another column? 如何将一组记录汇总到一行中,其中一列中只有一个记录有文本,而所有其他记录都是 null? - How can I summarize a set of records into one line where only one record in a column has text and all others are null? 根据列值仅选择一次MySQL记录 - Select records for MySQL only once based on a column value SQL列出列A出现多次但具有不同列B值的所有行 - SQL list all rows where column A appears more than once but have distinct Column B values 对于在单个语句中多次出现的替换变量,如何让 SQL Developer/SQL+ 只提示一次? - How can I make SQL Developer/SQL+ prompt only once for a substitution variable that occurs multiple times in a single statement? SQL,如何仅检索一次与另一列中具有相同值的多个条目关联的列? - SQL, How to retrieve a column only once associated with multiple entries of same value in another column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM