简体   繁体   English

SQL:根据记录创建日期和其他条件过滤记录

[英]SQL: Filter records based on record creation date and other criteria

I am struggling to find a better solution to pick unique records from my user call data table. 我正在努力寻找一种更好的解决方案,以从用户呼叫数据表中选择唯一记录。 My table structure is as follows: 我的表结构如下:

SELECT [MarketName],
       [WebsiteName] ,
       [ID] ,
       [UserID],
       [CreationDate],
       [CallDuration],
       [FromPhone] ,
       [ToPhone],
       [IsAnswered],
       [Source]
FROM [dbo].[UserCallData]

There are multiple entries in this table with different and same ID's. 该表中有多个条目,它们具有不同且相同的ID。 I wanted to check if [FromPhone] and [ToPhone] exists multiple times within last 3 months, if yes, I wanted to pick the first record with all columns based on [CreationDate], count the number of occurrences as TotalCallCount and sum the totalCallDuration as a single record. 我想检查[FromPhone]和[ToPhone]在过去3个月中是否存在多次,如果是,我想根据[CreationDate]来选择所有列的第一条记录,将出现的次数计为TotalCallCount并求和totalCallDuration作为单个记录。 If [FromPhone] and [ToPhone] does not occur multiple times, I wanted to pick all columns as such. 如果[FromPhone]和[ToPhone]没有多次出现,我想这样选择所有列。 I have been able to put up partial query like below. 我已经能够提出如下部分查询。 But this doesn't return all columns without including in group by clause and also it doesn't satisfy my entire criteria. 但这不会返回所有列而不包括group by子句,也不满足我的整个条件。 Any help on this would be highly appreciated. 任何帮助,将不胜感激。

select  [FromPhone], 
        MIN([CreationDate]),
        [ToPhone], 
        marketname, 
        count(*) as TotalCallCount , 
        sum(CallDuration) as TotalCallDuration 
from [dbo].[UserCallData]
where  [CreationDate] >= DATEADD(MONTH, -3, GETDATE())
group by  [FromPhone],[ToPhone], marketname 
having count([FromPhone]) > 1 and count([ToPhone]) >1

Try to use ROW_NUMBER() 尝试使用ROW_NUMBER()

;with cte as
(
    select *, ROW_NUMBER() OVER(PARTITION BY FromPhone, ToPhone ORDER BY CreationDate) as RN
    from UserCallData
    where CreationDate >= DATEADD(MONTH, -3, GETDATE())
),
cte_totals as
(
    select C1.FromPhone, C1.ToPhone, COUNT(*) as TotalCallCount, SUM(CallDuration) as TotalCallDuration
    from cte C1
    where exists(select * from cte C2 where C1.FromPhone = C2.FromPhone and C1.ToPhone = C2.ToPhone and C2.RN > 1)
    group by C1.FromPhone, C1.ToPhone
)
select C1.*, TotalCallCount, TotalCallDuration
from cte C1
    inner join cte_totals C2 on C1.FromPhone = C2.FromPhone and C1.ToPhone = C2.ToPhone
where C1.RN = 1 

I wrote query right in here so it can have some mistakes or mistypes, but the main idea might be clear. 我在此处直接编写了查询,因此它可能会有一些错误或类型错误,但主要思想可能很清楚。

I'm not entirely sure I've understood the question, but if I have the following may be what you want (or be a useful starting point): 我不完全确定我已经理解了这个问题,但是如果我有以下内容,可能就是您想要的(或者是一个有用的起点):

SELECT
       ucd.FromPhone,
       min(ucd.CreationDate) as MinCreationDate,
       ucd.ToPhone,
       ucd.MarketName,
       count(*) as TotalCallCount,
       sum(ucd.CallDuration) as TotalCallDuration,
       case
           when min(ucd.WebsiteName) = max(ucd.WebsiteName) then min(ucd.WebsiteName)
           else '* Various'
       end as WebsiteName,
       case
           when min(ucd.ID) = max(ucd.ID) then min(ucd.ID)
           else '* Various'
       end as ID,
       case
           when min(ucd.UserID) = max(ucd.UserID) then min(ucd.UserID)
           else '* Various'
       end as UserID,
       case
           when min(ucd.IsAnswered) = max(ucd.IsAnswered) then min(ucd.IsAnswered)
           else '* Some'
       end as IsAnswered,
       case
           when min(ucd.Source) = max(ucd.Source) then min(ucd.Source)
           else '* Various'
       end as Source
FROM
    dbo.UserCallData ucd
WHERE
    ucd.CreationDate >= DATEADD(MONTH, -3, GETDATE())
GROUP BY
    ucd.FromPhone,
    ucd.ToPhone,
    ucd.MarketName

Where we are collapsing rows together, if all the rows agree on a given column (so min(Field) = max(Field) ), I return the min(Field) value (which is the same all the others, but avoid problems with needing additional "group by" clauses which would interfere with the other cases). 在将行折叠在一起的情况下,如果所有行都在给定列上一致(所以min(Field) = max(Field) ),我将返回min(Field)值(所有其他值都相同,但避免出现问题)需要其他会干扰其他情况的“分组依据”子句)。 Where they don't all agree, I've returned "* something" . 在他们不同意的地方,我返回了"* something"

The code assumes that all the columns are text type columns (you haven't said), you may get conversion errors. 该代码假定所有列都是文本类型的列(您尚未说过),您可能会遇到转换错误。 It also assumes that none of these fields are null . 它还假定这些字段都不为null You / we can adapt the code if those assumptions aren't correct. 如果这些假设不正确,您/我们可以修改代码。 If you aren't able to do that for yourself, let me know about issues, I'll be happy to do what I can. 如果您无法自己做到这一点,请告诉我有关问题,我们将竭尽所能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据最小日期和其他条件(其中每个事务在SQL中可以具有多个日期)过滤记录? - How to filter records based on minimum date and some other criteria where each transaction can have multiple dates in SQL? 如何在日期范围内过滤 SQL 服务器中的特定记录而不删除其他记录? - How to filter a specific record in SQL Server within in the date range without removing other records? 根据多个过滤条件排除记录-SQL访问 - Excluding Records based on Multiple Filter Criteria- SQL Access SQL:根据条件过滤 - SQL: Filter based on criteria 根据SQL中具有相同ID的其他记录过滤出一条记录 - Filter out a record depending upon other records of the same ID in SQL 根据符合条件的 1 条记录更新多条记录 - Update multiple records based on 1 record meeting a criteria 如何根据每组的条件过滤记录 - How to filter records based on a criteria per group SQL语法根据字段条件和输出总记录计数汇总记录 - SQL Syntax To Aggregate Records Based on Field Criteria and Output Total Record Counts 根据某些条件返回具有最大日期的记录 - Return record with the max date based on certain criteria Select 基于创建日期的不同最新记录 - Select the distinct latest records based on their creation date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM