27,579
社区成员
发帖
与我相关
我的任务
分享
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-10-25 13:18:42
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86)
-- Apr 22 2011 11:57:00
-- Copyright (c) Microsoft Corporation
-- Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([表单号] varchar(8),[起单时间] datetime)
insert [tb]
select 'AOM_0001','2011-10-1 10:20:00' union all
select 'AOM_0002','2011-10-1 12:30:00' union all
select 'AOM_0003','2011-10-2 13:20:00' union all
select 'AOM_0004','2011-10-3 9:30:00' union all
select 'AOM_0005','2011-10-3 15:25:00' union all
select 'AOM_0006','2011-10-3 16:20:00' union all
select 'AOM_0007','2011-10-5 10:20:00' union all
select 'AOM_0008','2011-10-6 11:20:00' union all
select 'AOM_0009','2011-10-6 12:20:00'
--------------开始查询--------------------------
select distinct convert(varchar(10),起单时间,120) as 日期, COUNT(1) over(partition by convert(varchar(10),起单时间,120)) from tb
----------------结果----------------------------
/* 日期
---------- -----------
2011-10-01 2
2011-10-02 1
2011-10-03 3
2011-10-05 1
2011-10-06 2
(5 行受影响)
*/
select
convert(varchar(10),起单时间,120) as 日期,
count(1) as 起单件数
from
tb
group by
convert(varchar(10),起单时间,120)
CREATE TABLE tb(表单号 varchar(10),起单时间 datetime)
insert into tb select 'AOM_0001','2011-10-1 10:20:00'
insert into tb select 'AOM_0002','2011-10-1 12:30:00'
insert into tb select 'AOM_0003','2011-10-2 13:20:00'
insert into tb select 'AOM_0004','2011-10-3 9:30:00'
insert into tb select 'AOM_0005','2011-10-3 15:25:00'
insert into tb select 'AOM_0006','2011-10-3 16:20:00'
insert into tb select 'AOM_0007','2011-10-5 10:20:00'
insert into tb select 'AOM_0008','2011-10-6 11:20:00'
insert into tb select 'AOM_0009','2011-10-6 12:20:00'
go
select convert(varchar(10),起单时间,120)日期,count(*)起单件数
from tb
group by convert(varchar(10),起单时间,120)
/*
日期 起单件数
---------- -----------
2011-10-01 2
2011-10-02 1
2011-10-03 3
2011-10-05 1
2011-10-06 2
(5 行受影响)
*/
go
drop table tests
select convert(varchar(10),日期,120),
count(*)
from tb
group by convert(varchar(10),日期,120)
select convert(varchar(10),起单时间,120)日期,count(*) from tb
group by convert(varchar(10),起单时间,120)