比如说表中有多个字段其中两个字段为
clase    name     mak     date            number
 01        张三     12      2010-09-06       001
 01        张三     10      2010-09-06       002
 02        张三     10      2010-09-07       003
 03        张三     10      2010-09-08       004
 04        张三     10      2010-09-09       005
 05        李四      8      2010-09-05       006
 06        李四      8      2010-09-06       007
 07        王五     9       2010-09-07       008
 08        王五     6       2010-09-08       009
 09        张三     4       2010-09-06       010
 10        张三     4       2010-09-09       011
想要查询重复值结果显示如下
  name     mak       cishu
   张三     12          1
   张三     10          4
   张三      4          2
   李四      8          2
   王五      9          1
   王五      6          1

解决方案 »

  1.   

    select name,mak,count(1)
    from TB
    group by name,mak
      

  2.   

    select cname,mak,cishu=count(*)
    from #table group by cname,mak
      

  3.   

    if object_id('[TB]') is not null drop table [TB]
    go
    create table [TB] (clase nvarchar(4),name nvarchar(4),mak int,date datetime,number nvarchar(6))
    insert into [TB]
    select '01','张三',12,'2010-09-06','001' union all
    select '01','张三',10,'2010-09-06','002' union all
    select '02','张三',10,'2010-09-07','003' union all
    select '03','张三',10,'2010-09-08','004' union all
    select '04','张三',10,'2010-09-09','005' union all
    select '05','李四',8,'2010-09-05','006' union all
    select '06','李四',8,'2010-09-06','007' union all
    select '07','王五',9,'2010-09-07','008' union all
    select '08','王五',6,'2010-09-08','009' union all
    select '09','张三',4,'2010-09-06','010' union all
    select '10','张三',4,'2010-09-09','011'select * from [TB]select name,mak,count(1) AS cishu
    from TB
    group by name,mak/*
    name mak cishu
    张三 4 2
    王五 6 1
    李四 8 2
    王五 9 1
    张三 10 4
    张三 12 1*/
      

  4.   

    不想弄数据测试了,lz自己试试select count(*) 'cishu',name,mak from table group by name,mak
      

  5.   

    是我没有描述清楚...
    得出的结果中应该显示表中的所有字段,然后再提取cishu 进行核对clase    name     mak     date            number
     01        张三     12      2010-09-06       001
     01        张三     10      2010-09-06       001
     02        张三     10      2010-09-07       001
     03        张三     10      2010-09-08       001
     04        张三     10      2010-09-09       001
     05        李四      8      2010-09-05       002
     06        李四      8      2010-09-06       003
     07        王五     9       2010-09-07       004
     08        王五     6       2010-09-08       005
     09        张三     4       2010-09-06       006
     10        张三     4       2010-09-09       007
    想要查询重复值结果显示如下
      name     mak       cishu    number 
       张三     12          1       001
       张三     10          4       001
       张三      4          2       006
       李四      8          2       002
       王五      9          1       004
       王五      6          1       005
      

  6.   

    试试select count(*) 'cishu',name,mak,min(number ) from table group by name,mak
      

  7.   

    不是min 或 max 那么区分number
      

  8.   

    ----------------------------------------------------------------
    -- Author  :DBA_Huanzj(發糞塗牆)-- Version:
    --      Microsoft SQL Server 2012 (SP1) - 11.0.3000.0 (X64) 
    -- Oct 19 2012 13:38:57 
    -- Copyright (c) Microsoft Corporation
    -- Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
    --
    ----------------------------------------------------------------
    --> 测试数据:[huang]
    if object_id('[huang]') is not null drop table [huang]
    go 
    create table [huang]([clase] varchar(2),[name] varchar(4),[mak] int,[date] datetime,[number] varchar(3))
    insert [huang]
    select '01','张三',12,'2010-09-06','001' union all
    select '01','张三',10,'2010-09-06','001' union all
    select '02','张三',10,'2010-09-07','001' union all
    select '03','张三',10,'2010-09-08','001' union all
    select '04','张三',10,'2010-09-09','001' union all
    select '05','李四',8,'2010-09-05','002' union all
    select '06','李四',8,'2010-09-06','003' union all
    select '07','王五',9,'2010-09-07','004' union all
    select '08','王五',6,'2010-09-08','005' union all
    select '09','张三',4,'2010-09-06','006' union all
    select '10','张三',4,'2010-09-09','007'
    --------------开始查询--------------------------select name,mak,count(1) cishu,min(number ) number
    from [huang]
    group by  name,mak
    ----------------结果----------------------------
    /* 
    name mak         cishu       number
    ---- ----------- ----------- ------
    张三   4           2           006
    王五   6           1           005
    李四   8           2           002
    王五   9           1           004
    张三   10          4           001
    张三   12          1           001
    */