order表 GUID projectid Time
aaaa... 项目A组 2012-2-1
zzzz... 项目A组 2012-2-1
bbbb... 项目A组 2012-2-2
cccc.. 项目B组 2012-2-2
dddd.. 项目C组 2012-2-3
eeee... 项目B组 2012-2-1
come表GUID projectid Time
aaaa... 项目A组 2012-2-1
bbbb... 项目A组 2012-2-2
eeee... 项目B组 2012-2-1
ffff... 项目W组 2012-2-5
xxxx... 项目A组 2012-2-1
yyyy... 项目A组 2012-2-1
情况是这样的,两表中有一部分数据时相同的(guid相同就代表两条数据是相同的)。
去掉重复之后两表的总数应该是9条(既aaaa...、zzzz...、bbbb...、cccc...、dddd...、eeee...、ffff...、xxxx...、yyyy...)。
我想要的功能是以Time和projectid进行分组,求出Order表总数、Come表总数、两表之和总数
我最终想看到的效果如下:Time projectid Order表总数 Come表总数 两表之和总数(去重)
2012-2-1 项目A组 2 3 4
2012-2-1 项目B组 1 1 1
2012-2-2 项目A组 1 1 1
2012-2-2 项目B组 1 0 1
2012-2-3 项目C组 1 0 1
2012-2-5 项目W组 0 1 1
aaaa... 项目A组 2012-2-1
zzzz... 项目A组 2012-2-1
bbbb... 项目A组 2012-2-2
cccc.. 项目B组 2012-2-2
dddd.. 项目C组 2012-2-3
eeee... 项目B组 2012-2-1
come表GUID projectid Time
aaaa... 项目A组 2012-2-1
bbbb... 项目A组 2012-2-2
eeee... 项目B组 2012-2-1
ffff... 项目W组 2012-2-5
xxxx... 项目A组 2012-2-1
yyyy... 项目A组 2012-2-1
情况是这样的,两表中有一部分数据时相同的(guid相同就代表两条数据是相同的)。
去掉重复之后两表的总数应该是9条(既aaaa...、zzzz...、bbbb...、cccc...、dddd...、eeee...、ffff...、xxxx...、yyyy...)。
我想要的功能是以Time和projectid进行分组,求出Order表总数、Come表总数、两表之和总数
我最终想看到的效果如下:Time projectid Order表总数 Come表总数 两表之和总数(去重)
2012-2-1 项目A组 2 3 4
2012-2-1 项目B组 1 1 1
2012-2-2 项目A组 1 1 1
2012-2-2 项目B组 1 0 1
2012-2-3 项目C组 1 0 1
2012-2-5 项目W组 0 1 1
解决方案 »
- 这样的语句错误在哪里啊?请帮忙指点更正!
- 求一SQL
- 在sql里面当读到'0'的时候显示'男'读到'1'的时候显示女可以吗?
- 多表表拼成视图
- 为什么删除链接服务器上的数据会出错?
- 怎么看一条sql语句是哪部分语句比较耗时间?
- select body,posttime,RootTitleID from title如何选择body的前100个字节或自?
- sqlserver为什么写表的全称。
- 我的问题很简单吗?为啥没有人帮我呢?http://expert.csdn.net/Expert/topic/2082/2082581.xml?temp=.8763697
- 关于SQLServer通用分页的讨论
- SQL语句执行计划求解惑
- 求助 有关数据库架构设计
Order表总数=sum(case when type='Order' then 1 else 0 end),
Come表总数=sum(case when type='Come' then 1 else 0 end),
两表之和总数=count(distinct GUID)
from (select *,type='Order' from [Order]
union all
select *,type='Come' from Come) t
group by Time,projectid
,count(v.*) as 两表之和总数(去重)from order as o, Come as c,(select * from order as o unionselect * from Come as c) as vgroup by Time,projectid
--> 测试数据: @order表
declare @order表 table (GUID varchar(4),projectid varchar(7),Time datetime)
insert into @order表
select 'aaaa','项目A组','2012-2-1' union all
select 'zzzz','项目A组','2012-2-1' union all
select 'bbbb','项目A组','2012-2-2' union all
select 'cccc','项目B组','2012-2-2' union all
select 'dddd','项目C组','2012-2-3' union all
select 'eeee','项目B组','2012-2-1'--> 测试数据: @come表
declare @come表 table (GUID varchar(4),projectid varchar(7),Time datetime)
insert into @come表
select 'aaaa','项目A组','2012-2-1' union all
select 'bbbb','项目A组','2012-2-2' union all
select 'eeee','项目B组','2012-2-1' union all
select 'ffff','项目W组','2012-2-5' union all
select 'xxxx','项目A组','2012-2-1' union all
select 'yyyy','项目A组','2012-2-1'select
Time=
convert(varchar(10),Time,120),projectid,
[Order表总数]=sum(case when c=1 then 1 else 0 end),
[Come表总数]=sum(case when c=2 then 1 else 0 end),
[两表之和总数(去重)]=count(1)
from
(
select *,1 as c from @order表
union all
select *,2 from @come表
) a
group by Time,projectid/*
Time projectid Order表总数 Come表总数 两表之和总数(去重)
---------- --------- ----------- ----------- -----------
2012-02-01 项目A组 2 3 5
2012-02-02 项目A组 1 1 2
2012-02-01 项目B组 1 1 2
2012-02-02 项目B组 1 0 1
2012-02-03 项目C组 1 0 1
2012-02-05 项目W组 0 1 1
*/--去重的问题我再处理一下
--> 测试数据: @order表
declare @order表 table (GUID varchar(4),projectid varchar(7),Time datetime)
insert into @order表
select 'aaaa','项目A组','2012-2-1' union all
select 'zzzz','项目A组','2012-2-1' union all
select 'bbbb','项目A组','2012-2-2' union all
select 'cccc','项目B组','2012-2-2' union all
select 'dddd','项目C组','2012-2-3' union all
select 'eeee','项目B组','2012-2-1'--> 测试数据: @come表
declare @come表 table (GUID varchar(4),projectid varchar(7),Time datetime)
insert into @come表
select 'aaaa','项目A组','2012-2-1' union all
select 'bbbb','项目A组','2012-2-2' union all
select 'eeee','项目B组','2012-2-1' union all
select 'ffff','项目W组','2012-2-5' union all
select 'xxxx','项目A组','2012-2-1' union all
select 'yyyy','项目A组','2012-2-1'select
Time=convert(varchar(10),Time,120),projectid,
[Order表总数]=sum(case when c=1 then 1 else 0 end),
[Come表总数]=sum(case when c=2 then 1 else 0 end),
[总数]=count(distinct GUID)
from(select *,1 as c from @order表 union all select *,2 from @come表) a
group by Time,projectid/*
Time projectid Order表总数 Come表总数 总数
---------- --------- ----------- ----------- -----------
2012-02-01 项目A组 2 3 4
2012-02-01 项目B组 1 1 1
2012-02-02 项目A组 1 1 1
2012-02-02 项目B组 1 0 1
2012-02-03 项目C组 1 0 1
2012-02-05 项目W组 0 1 1
*/
但是如果 order表中的列名为OrderTime Come表中的列名为ComeTime
这种情况下,应该如何改写这条语句呢?