select count(*) from 一张表 where shijian>getdate()-7
select count(distinct k) as 个数 from ( select datepart(week,shijian) as k from a )l
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 行) */
select count(distinct datepart(wk , shijian)) from a
---------------------------------------------------------------- -- 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 行受影响)*/
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
/*====================================================*/ -- 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
from 一张表
where shijian>getdate()-7
from (
select datepart(week,shijian) as k from a
)l
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 行)
*/
-- 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 行受影响)*/
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
-- 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