zoukankan      html  css  js  c++  java
  • 使用SQL查询重复列记录

    表stuinfo,有三个字段recno(自增),stuid,stuname
      
      建该表的Sql语句如下:
      
      Create TABLE [StuInfo] (
       [recno] [int] IDENTITY (1, 1) NOT NULL ,
       [stuid] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
       [stuname] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL
      ) ON [PRIMARY]
      GO
      
      1.--查某一列(或多列)的重复值(只能查出重复记录的值,不能整个记录的信息)
      --如:查找stuid,stuname重复的记录
      select stuid,stuname from stuinfo
      group by stuid,stuname
      having(count(*))>1
      
      2.--查某一列有重复值的记录(这种方法查出的是所有重复的记录,也就是说如果有两条记录重复的,就查出两条)
      --如:查找stuid重复的记录
      select * from stuinfo
      where stuid in (
      select stuid from stuinfo
      group by stuid
      having(count(*))>1
      )
      
      3.--查某一列有重复值的记录(只显示多余的记录,也就是说如果有三条记录重复的,就显示两条)
      --这种方成绩的前提是:需有一个不重复的列,本例中的是recno
      --如:查找stuid重复的记录
      select * from stuinfo s1
      where recno not in (
      select max(recno) from stuinfo s2
      where s1.stuid=s2.stuid
      )

     
    id  datadate    value
    1   2010-10-5   12
    1   2010-10-4   20
    1   2010-10-3   40
    2   2010-10-5   42
    2   2010-10-4   33
    2   2010-10-3   34
    3   2010-10-4   15
    3   2010-10-3   2

    要求用一个sql查出,每个id最近的一条记录,怎么查?

    select *  FROM  test1 t where (t.tdate >= (select Max(tdate) from test1 where id = t.id))


       本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢

  • 相关阅读:
    数组
    灰度转换
    类的定义和头文件
    读文件
    操纵元
    传递引用
    TensorFlow常用函数
    四、Item Pipeline
    三、Scrapy Shell
    二、Scrapy案例入门
  • 原文地址:https://www.cnblogs.com/wzg0319/p/1592327.html
Copyright © 2011-2022 走看看