zoukankan      html  css  js  c++  java
  • 根据多字段去重



    有重复数据主要有一下几种情况:

    搜索


    1.存在两条完全相同的纪录
    这是最简单的一种情况,用关键字distinct就可以去掉
    example: select distinct * from table(表名) where (条件)

    2.存在部分字段相同的纪录(有主键id即唯一键)
    如果是这种情况的话用distinct是过滤不了的,这就要用到主键id的唯一性特点及group by分组
    example: select * from table where id in (select max(id) from table group by [去除重复的字段名列表,....])

    eg:

    SELECT count(1)
    FROM
    TCHotelLog.dbo.ELongRequestErrorLog erel WITH (NOLOCK)
    LEFT JOIN TCHotelResource.dbo.HotelInfo hi WITH (NOLOCK)
    ON hi.Id = erel.HotelId
    LEFT JOIN TCHotelResource.dbo.RoomType rt WITH (NOLOCK)
    ON rt.Id = erel.RoomTypeId
    LEFT JOIN TCHotelResource.dbo.HotelPolicy hp WITH (NOLOCK)
    ON hp.Id = erel.PolicyId
    WHERE
    erel.ErrorTime >= '2014-6-13'
    AND erel.RefId IN (9516406, 1646048)
    AND erel.Id IN (SELECT max(Id)
    FROM
    TCHotelLog.dbo.ELongRequestErrorLog WITH (NOLOCK)
    GROUP BY
    MemberId
    , PolicyId)



    3.没有唯一键ID
    这种情况我觉得最复杂,目前我只会一种方法,有那位知道其他方法的可以留言,交流一下:
    example:select identity(int1,1) as id,* into newtable(临时表) from table
    select * from newtable where id in (select max(id) from newtable group by [去除重复的字段名列表,....])
    drop table newtable




    这个也可以

    WITH cte
    AS ( SELECT ROW_NUMBER() OVER ( PARTITION BY t1.ParentId ORDER BY t1.ParentId ) rowNum ,
    *
    FROM [TCHotelResource].dbo.HotelDianping t1
    WHERE t1.HotelId = 135
    AND t1.OrderId != ''
    AND t1.OrderId = '110513000016'
    )
    SELECT *
    FROM cte
    WHERE rowNum = 1





  • 相关阅读:
    Firefox常用插件
    给创业者的提醒
    Visual Studio 2005插件开发官方Demo下载
    Windows Media Player Firefox Plugin支持Server 2003
    【转】C++的64位整数
    【转】C++成员函数的内存分配问题
    【转】C++类的实例分布
    【转】 IntelliJ Idea 常用快捷键列表
    程序员必备神器
    程序员必备神器
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/3791314.html
Copyright © 2011-2022 走看看