一张表 A    id    shijian 
             1     2009-12-17   (本周)
             2     2009-12-16  (本周)
             3     2009-12-12  (上个星期的)    得到的应该是2个
  

解决方案 »

  1.   

    select count(*)
    from 一张表
    where shijian>getdate()-7
      

  2.   

    select count(distinct k) as 个数
    from (
      select datepart(week,shijian) as k from a 
    )l
      

  3.   

    create table A (id int,   shijian datetime)
    insert into a values(1 ,   '2009-12-17')
    insert into a values(2 ,   '2009-12-16')
    insert into a values(3 ,   '2009-12-12')
    goselect count(distinct datepart(week , shijian)) from adrop table a  /*
                
    ----------- 
    2(所影响的行数为 1 行)
    */
      

  4.   

    select count(distinct datepart(wk , shijian)) from a
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-12-17 16:53:51
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[a]
    if object_id('[a]') is not null drop table [a]
    go 
    create table [a]([id] int,[shijian] datetime)
    insert [a]
    select 1,'2009-12-17' union all
    select 2,'2009-12-16' union all
    select 3,'2009-12-12'
    --------------开始查询--------------------------
    select count(distinct datepart(wk , shijian)) from a
    ----------------结果----------------------------
    /* -----------
    2(1 行受影响)*/
      

  6.   

       select count(distinct k) as 个数
    from (
      select datepart(week,shijian) as k from 
      (
      select 
               '2009-12-17'   AS shijian union all select 
                '2009-12-16'   union all select 
            '2009-12-12'  
      )a
    )l
      

  7.   

    /*====================================================*/
    -- Author: Ken Wong
    -- Create date: 2009-12-17 18:46:54
    -- Description:
    /*====================================================*/
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    create table [tb]([id] int,[shijian] datetime)
    insert [tb]
    select 1,'2009-12-17' union all
    select 2,'2009-12-16' union all
    select 3,'2009-12-12' union all
    select 4,'2009-12-21'select count(1) as nums 
    from [tb]
    where datepart(week,[shijian])=datepart(week,getdate())----------------------
    2