drkjzk3359 2013-05-18 22:22
浏览 59

选择重复记录,其中一条记录必须包含确切的值

I have a table as below with 3 columns amount, frequency & identifier. All these records are in the same table in the same database.

amount     frequency       identifier
4.75        1                100
4.75        3                101
4.76        2                102
4.76        3                103
4.44        1                104
4.43        1                105
4.75        2                106
4.67        2                107
4.75        3                108
4.64        2                109
4.64        3                110
4.65        4                111

I'm after a sql query that should pickup duplicate records for different frequency but frequency of one of them must be equal to 1. So the output i'm expecting is

amount  frequency   identifier
4.75        1       100
4.75        3       101
4.75        2       106
4.75        3       108

Note that the query is executed every 2 seconds along with 15 other queries that are lightweight. I can do the processing in php too, but prefer a database query so long its not performance intensive. I'm trying different queries, but can't get consistent results. Can someone please help ?

Many thanks

  • 写回答

1条回答 默认 最新

  • doudai3012 2013-05-18 22:26
    关注

    I'm thinking something like this would be the fastest way to get what you want:

    select *
    from t
    where exists (select 1 from t t2 where t2.amount = t.amount and t2.frequency = 1)
    

    This will run fastest with an index on (amount, frequency).

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100