首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

对从表数据的统计,该如何处理

2012-01-14 
对从表数据的统计两个表,一个是workflow设置外键为workflow的id对应node的wfididnamecontent11文件文件审

对从表数据的统计
两个表,    

一个是workflow    
设置外键为workflow的id对应node的wfid    

id                           name                     content    
11                       文件                       文件审批系统    
12                       订货                       订单购物系统    
13                       公文                       公文系统    

一个是node    

id                       wfid               orederid                     name                     note        
7                       11                       1                       制作文档                       NULL    
8                       11                       2                       上传文档                       NULL    
9                       11                       3                       审批文档                       NULL    
10                       11                       4                       通过备案                       NULL    
11                       11                       5                       通知结果                       NULL    
12                       12                       1                       填写表单                       NULL    

请问如何得到这样的试图。    
id     name     content     nodecount     ordermax    

其中id,name,content对应workflow的相应字段    
nodecount是         node表node.wfid=workflow.id的总行数    
(如果没有node.wfid=workflow.id的行,则显示为0)    


ordermax是         node表node.wfid=workflow.id所有行中,orderid最大的node.orderid    
(如果没有node.wfid=workflow.id的行,则显示为0)

[解决办法]
select id ,name content ,isnull(b.nodecount,0) as nodecount ,isnull(b.ordermax ,0) as ordermax
from workflow w
left join (select wfid ,count(1) as nodecount ,max(orederid) as ordermax from node group by wfid ) b on w.id = b.wfid
[解决办法]
if object_id( 'pubs..workflow ') is not null
drop table workflow
go
create table workflow(id varchar(10),name varchar(10),content varchar(20))
insert into workflow(id,name,content) values( '11 ', '文件 ', '文件审批系统 ')
insert into workflow(id,name,content) values( '12 ', '订货 ', '订单购物系统 ')
insert into workflow(id,name,content) values( '13 ', '公文 ', '公文系统 ')
go

if object_id( 'pubs..node ') is not null
drop table node
go
create table node(id varchar(10),wfid varchar(10),orederid varchar(10),name varchar(10),note varchar(10))
insert into node(id,wfid,orederid,name,note) values( '7 ' , '11 ', '1 ', '制作文档 ',null)
insert into node(id,wfid,orederid,name,note) values( '8 ' , '11 ', '2 ', '上传文档 ',null)
insert into node(id,wfid,orederid,name,note) values( '9 ' , '11 ', '3 ', '审批文档 ',null)
insert into node(id,wfid,orederid,name,note) values( '10 ', '11 ', '4 ', '通过备案 ',null)
insert into node(id,wfid,orederid,name,note) values( '11 ', '11 ', '5 ', '通知结果 ',null)
insert into node(id,wfid,orederid,name,note) values( '12 ', '12 ', '1 ', '填写表单 ',null)
go

select a.* , isnull(b.nodecount,0) nodecount , isnull(orederid, '0 ') orederid from workflow a
left join
(select wfid , count(*) nodecount , max(orederid) orederid from node group by wfid) b
on a.id = b.wfid

drop table workflow,node

/*
id name content nodecount orederid
---------- ---------- -------------------- ----------- ----------
11 文件 文件审批系统 5 5
12 订货 订单购物系统 1 1
13 公文 公文系统 0 0

(所影响的行数为 3 行)
*/

热点排行