如何在SQL中查找重复的值?

时间:2018-05-25 19:16:20

标签: sql sql-server sql-server-2012

我有一个客户咨询表。每次客户点击Enter,或点击返回页面等......表格会跟踪该信息。

applicationID      answer                        createDate

1602712         StatusCheck.SOFTDENIED        2018-05-22 02:14:56.893

1602712         StatusCheck.SOFTDENIED        2018-05-22 02:14:57.040

第一个条目没问题,但第二个条目是我们需要向客户证明客户多次单击Enter的地方。我们有数以千计的记录。

我如何编写一个脚本来跟踪在一分钟内按输入太多次的客户......

下面的脚本给了我重复的内容,但是我需要缩小那些一直按下Enter键的内容......

SELECT 
ROW_NUMBER() OVER(PARTITION BY CS.applicationid ORDER BY CS.createdDate) dp,
   CS.applicationid,
   CS.answer,
   CS.createdDate,
   CS.platform,
   CS.mobiledevicemanufacturer,
   CS.mobiledevicemodel,
   CS.browsertype,
   CS.ip,
   CS.applicationstatusid,
   CS.customerstatusid 
   INTO #dp
   FROM dbo.CustomerStatusInquiryLog AS CS 
   WHERE MONTH(CS.createdDate)= 05 AND 
   DAY(CS.createdDate) BETWEEN 02 AND 24 
   ORDER BY CS.createdDate DESC

  SELECT * FROM #dp WHERE dp > 1 
  ORDER BY answer, applicationid 

3 个答案:

答案 0 :(得分:3)

您可以尝试使用lead函数在子查询上获取nextTime,然后使用DATEDIFF函数获得不到一分钟的时间。

SELECT DISTINCT ApplicationID,
                Answer 
FROM 
    (
      SELECT *,
             LEAD(CreateDate) OVER (PARTITION BY ApplicationID, Answer ORDER BY CreateDate) NextTime
      FROM T
    ) T
WHERE DATEDIFF(MINUTE, T.CreateDate, T.NextTime) = 0;

sqlfiddle:http://sqlfiddle.com/#!18/f9880a/9

答案 1 :(得分:2)

我建议使用60秒 4:01到5:59仍然是一分钟 5:59到6:01是零分钟。

declare @t table (id int, app varchar(40), dtm datetime);
insert into @t values 
(1602712, 'StatusCheck.SOFTDENIED', '2018-05-22 02:14:56.893'), 
(1602712, 'StatusCheck.SOFTDENIED', '2018-05-22 02:14:57.040'),
(1602712, 'StatusCheck.SOFTDENIED', '2018-05-22 02:15:57.040'),
(1602712, 'StatusCheck.SOFTDENIED', '2018-05-22 02:16:58.040');
select * 
from ( select *
            , DATEdiff(second, lag(dtm) over (partition by id, app order by dtm), dtm) as diffSeconds
         from @t
     ) t
where diffSeconds <= 60
order by id, app, dtm

如果应用程序不尊重多次点击,那就太好了。您可以使用它来禁用按钮,直到返回结果。

private async void startButton(object sender, RoutedEventArgs e)
{
    CancelEnabled = true;
    await start();
}

private async void Button_Click(object sender, RoutedEventArgs e)
{
    btnTest.IsEnabled = false;
    await Dowork();
    btnTest.IsEnabled = true;
}
private async Task Dowork()
{
    await Task.Delay(1000);
}

或者您可以缓存之前的结果,只要点击次数在x秒内即可使用。

答案 2 :(得分:0)

可能是一组包括所有部分到第二部分。

group by
applicationID,
answer,
datepart(year, createdate),
datepart(month, createdate),
datepart(day, createdate),
datepart(hour, createdate),
datepart(minute, createdate)

它不会说明有人在多分钟内(下一分钟开始的一分钟结束时)进入。如果您想要考虑到这一点,您可以将表连接回自身,并使用日期添加一分钟长的间隔,从而删除第二个表中属于该间隔的结果。