create table 客户资料表(客户编号 int,客户名称 nvarchar(100), 所属单位ID int) insert into 客户资料表 select 1 , 'aaa', 1 union all select 2 , 'bbb' , 1 union all select 3 , 'ccc ' , 1create table 合同单位表(单位ID int, 单位名称 nvarchar(100)) insert into 合同单位表 select 1, 'abc单位'
create table 登记表(日期 datetime,客户编号 int, 申请编号 int) insert into 登记表 select '2004-5-1' , 1 , 1 union all select '2004-5-1' , 2 , 1 union all select '2004-5-1' , 3 , 2 union all select '2004-5-2' , 2 , 3 union all select '2004-5-2' , 1 , 4 union all select '2004-5-3' , 1 , 5 union all select '2004-5-3' , 2 , 5 go ---申请编号需要用到一个Function create function Get_Num(@d as datetime) returns nvarchar(1000) as begin declare @i as nvarchar(1000) set @i='' select @i=@i+','+convert(nvarchar, 申请编号) from (select distinct 日期, 申请编号 from 登记表) a where 日期=@d set @i= stuff(@i,1,1,'') return @i end go ----------------------- select t1.日期,max(t3.单位名称) as 单位名称 ,(select count(distinct 客户编号) from 登记表 where 日期=t1.日期) as 申请人数 ,(select count(distinct 申请编号) from 登记表 where 日期=t1.日期 ) as 申请数 ,dbo.Get_Num(t1.日期) as 申请编号 from 登记表 t1 inner join 客户资料表 t2 on t1.客户编号=t2.客户编号 inner join 合同单位表 t3 on t2.所属单位ID=t3.单位ID group by t1.日期drop function Get_Num drop table 合同单位表 drop table 客户资料表 drop table 登记表 /* 测试结果2004-05-01 00:00:00.000 abc单位 3 2 1,2 2004-05-02 00:00:00.000 abc单位 2 2 3,4 2004-05-03 00:00:00.000 abc单位 2 1 5 */
--示例--测试数据 create table 客户资料表(客户编号 int,客户名称 nvarchar(100),所属单位ID int) insert 客户资料表 select 1,'aaa',1 union all select 2,'bbb',1 union all select 3,'ccc',1create table 合同单位表(单位ID int,单位名称 nvarchar(100)) insert 合同单位表 select 1,'abc单位'
create table 登记表(日期 datetime,客户编号 int,申请编号 int) insert 登记表 select '2004-5-1',1,1 union all select '2004-5-1',2,1 union all select '2004-5-1',3,2 union all select '2004-5-2',2,3 union all select '2004-5-2',1,4 union all select '2004-5-3',1,5 union all select '2004-5-3',2,5 go---申请编号需要用到一个Function create function f_str( @dt datetime )returns varchar(8000) as begin declare @r as varchar(8000) set @r='' select @r=@r+','+cast(申请编号 as varchar) from 登记表 where 日期=@dt group by 申请编号 return(stuff(@r,1,1,'')) end go--查询 select 日期=convert(varchar(10),a.日期,120), c.单位名称, 申请人数=count(distinct a.客户编号), 申请数=count(distinct a.申请编号), 申请编号=dbo.f_str(a.日期) from 登记表 a,客户资料表 b,合同单位表 c where a.客户编号=b.客户编号 and b.所属单位ID=c.单位ID group by a.日期,c.单位名称 go --删除测试 drop table 合同单位表,客户资料表,登记表 drop function f_str/*--测试结果日期 单位名称 申请人数 申请数 申请编号 ----------- ---------- ----------- ---------- ------------- 2004-05-01 abc单位 3 2 1,2 2004-05-02 abc单位 2 2 3,4 2004-05-03 abc单位 2 1 5(所影响的行数为 3 行) --*/
insert into 客户资料表 select 1 , 'aaa', 1
union all select 2 , 'bbb' , 1
union all select 3 , 'ccc ' , 1create table 合同单位表(单位ID int, 单位名称 nvarchar(100))
insert into 合同单位表
select 1, 'abc单位'
create table 登记表(日期 datetime,客户编号 int, 申请编号 int)
insert into 登记表 select '2004-5-1' , 1 , 1
union all select '2004-5-1' , 2 , 1
union all select '2004-5-1' , 3 , 2
union all select '2004-5-2' , 2 , 3
union all select '2004-5-2' , 1 , 4
union all select '2004-5-3' , 1 , 5
union all select '2004-5-3' , 2 , 5
go
---申请编号需要用到一个Function
create function Get_Num(@d as datetime)
returns nvarchar(1000)
as
begin
declare @i as nvarchar(1000)
set @i=''
select @i=@i+','+convert(nvarchar, 申请编号)
from (select distinct 日期, 申请编号 from 登记表) a
where 日期=@d
set @i= stuff(@i,1,1,'')
return @i
end
go
-----------------------
select t1.日期,max(t3.单位名称) as 单位名称
,(select count(distinct 客户编号) from 登记表 where 日期=t1.日期) as 申请人数
,(select count(distinct 申请编号) from 登记表 where 日期=t1.日期 ) as 申请数
,dbo.Get_Num(t1.日期) as 申请编号
from 登记表 t1
inner join 客户资料表 t2 on t1.客户编号=t2.客户编号
inner join 合同单位表 t3 on t2.所属单位ID=t3.单位ID
group by t1.日期drop function Get_Num
drop table 合同单位表
drop table 客户资料表
drop table 登记表
/*
测试结果2004-05-01 00:00:00.000 abc单位 3 2 1,2
2004-05-02 00:00:00.000 abc单位 2 2 3,4
2004-05-03 00:00:00.000 abc单位 2 1 5
*/
登记表的日期是
2004-5-1 12:12:12
2004-5-1 20:20:22
2004-5-1 20:20:23
这样的,结果好像不对
create table 客户资料表(客户编号 int,客户名称 nvarchar(100),所属单位ID int)
insert 客户资料表 select 1,'aaa',1
union all select 2,'bbb',1
union all select 3,'ccc',1create table 合同单位表(单位ID int,单位名称 nvarchar(100))
insert 合同单位表 select 1,'abc单位'
create table 登记表(日期 datetime,客户编号 int,申请编号 int)
insert 登记表 select '2004-5-1',1,1
union all select '2004-5-1',2,1
union all select '2004-5-1',3,2
union all select '2004-5-2',2,3
union all select '2004-5-2',1,4
union all select '2004-5-3',1,5
union all select '2004-5-3',2,5
go---申请编号需要用到一个Function
create function f_str(
@dt datetime
)returns varchar(8000)
as
begin
declare @r as varchar(8000)
set @r=''
select @r=@r+','+cast(申请编号 as varchar)
from 登记表
where 日期=@dt
group by 申请编号
return(stuff(@r,1,1,''))
end
go--查询
select 日期=convert(varchar(10),a.日期,120),
c.单位名称,
申请人数=count(distinct a.客户编号),
申请数=count(distinct a.申请编号),
申请编号=dbo.f_str(a.日期)
from 登记表 a,客户资料表 b,合同单位表 c
where a.客户编号=b.客户编号
and b.所属单位ID=c.单位ID
group by a.日期,c.单位名称
go
--删除测试
drop table 合同单位表,客户资料表,登记表
drop function f_str/*--测试结果日期 单位名称 申请人数 申请数 申请编号
----------- ---------- ----------- ---------- -------------
2004-05-01 abc单位 3 2 1,2
2004-05-02 abc单位 2 2 3,4
2004-05-03 abc单位 2 1 5(所影响的行数为 3 行)
--*/
登记表的日期是
2004-5-1 12:12:12
2004-5-1 20:20:22
2004-5-1 20:20:23
这样的,结果好像不对
------------------------------试试将程序中涉及日期的地方,转换一下:)
convert(varchar(10),日期,102)
这样就取出yy.mm.dd,而不涉及具体的hh:mi:ss