前提,我这个是SQL 2000表的内容是1111  2009/08/1
1111  2009/08/2
1111  2009/08/3 的时候,希望得到的结果是
1111  2009/08/1 3
1111  2009/08/2 3
1111  2009/08/3 3表的内容是1111  2009/08/1
1111  2009/08/2
1111  2009/08/3
1111  2009/08/7 的时候,希望得到的结果是
1111  2009/08/1 4
1111  2009/08/2 4
1111  2009/08/3 4
1111  2009/08/7   4总之,就是在追加一个count列,一条sql实现。

解决方案 »

  1.   

    SELECT COL1,COL2,COL3
    ,(SELECT COUNT(1) FROM TB WHERE ...)
    FROM TB 
    WHERE ...
      

  2.   

    select *,cnt=(select count(*) from tb) from tb
      

  3.   

    select 
    *,
    数量=(select COUNT(*) from tb where t.Name=Name )
    from tb t
      

  4.   


    SELECT COL1,COL2,(SELECT COUNT(1) FROM TB WHERE COL1=A.COL1) as cnt
    FROM TB  A
      

  5.   

    SELECT *,数量=(SELECT COUNT(*) FROM TB WHERE COL=T.COL) FROM TB T
      

  6.   

    -- =========================================
    -- -----------t_mac 小编-------------------
       --------------------希望有天成为大虾---- 
    -- =========================================IF OBJECT_ID('tb') IS NOT NULL
    DROP TABLE tb
    GO
    CREATE TABLE tb( name int, datet varchar(10))
    go
    insert tb SELECT 
    '1111','2009/08/1' UNION ALL SELECT 
    '1111','2009/08/2'  UNION ALL SELECT 
    '1111','2009/08/3'  UNION ALL SELECT 
    '1111','2009/08/7'  UNION ALL SELECT 
    '1112','2009/08/3'  UNION ALL SELECT 
    '1112','2009/08/3'  
    go
    select 
    *,
    数量=(select COUNT(*) from tb where t.Name=Name )
    from tb t
    goname        datet      数量
    ----------- ---------- -----------
    1111        2009/08/1  4
    1111        2009/08/2  4
    1111        2009/08/3  4
    1111        2009/08/7  4
    1112        2009/08/3  2
    1112        2009/08/3  2
      

  7.   

    ----------------------------------------------------------------
    -- Author :fredrickhu(小F 向高手学习)
    -- Date   :2009-08-19 16:34:45
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([col1] int,[col2] datetime)
    insert [tb]
    select 1111,'2009/08/1' union all
    select 1111,'2009/08/2' union all
    select 1111,'2009/08/3'
    --------------开始查询--------------------------select col1,col2,(select count(1) from tb) as col3 from [tb] 
    ----------------结果----------------------------
    /*
    col1        col2                                                   col3        
    ----------- ------------------------------------------------------ ----------- 
    1111        2009-08-01 00:00:00.000                                3
    1111        2009-08-02 00:00:00.000                                3
    1111        2009-08-03 00:00:00.000                                3(所影响的行数为 3 行)
    */
      

  8.   

    drop table test
    create table test(id int)
    insert test select 1 union all select 2 union all select 3 go
    SELECT *,col = (select COUNT(id) from test) FROM test group by id
    (所影响的行数为 3 行)id          col         
    ----------- ----------- 
    1           3
    2           3
    3           3(所影响的行数为 3 行)
      

  9.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([p_id] varchar(20),[core] int)
    insert [tb]
    select '2009/08/01',1111 union all
    select '2009/08/02',1111 union all
    select '2009/08/03',1111SELECT CORE,P_ID,(SELECT COUNT(*) FROM TB WHERE CORE=T.CORE) AS NUM FROM TB T(所影响的行数为 3 行)CORE        P_ID                 NUM         
    ----------- -------------------- ----------- 
    1111        2009/08/01           3
    1111        2009/08/02           3
    1111        2009/08/03           3(所影响的行数为 3 行)
      

  10.   


    declare @T TABLE 
      (ColA INT,
       ColB datetime)
    insert into @t (ColA,ColB)
    select 1111,'2009/08/1' union all
    select 1111,'2009/08/2' union all
    select 1111,'2009/08/3' union all 
    select 1111,'2009/08/7'select *,(select count(*) from @t) as ColC from @t
      

  11.   

    补充说明下,,,不要写2个FROM表,因为这个表是一对子查询构建起来的,那样写太罗嗦了在2008里,使用over,在2000里使什么不知道
      

  12.   

    select *,cnt=(select count(*) from tb) from tb是最简单得因为你必须查数量结果作为一个数据列啊