34,838
社区成员




create table tb1(id int, comname varchar(10) )
insert into tb1 values(1, 'moto')
insert into tb1 values(2, 'nokia')
insert into tb1 values(3, 'ibm')
create table tb2 (id int, itemname varchar(10), parentid int, rooms int)
insert into tb2 values(1, '银行' , 1 , 60 )
insert into tb2 values(2, '电信' , 1 , 80 )
insert into tb2 values(3, '公路' , 1 , 45 )
insert into tb2 values(4, '银行1', 2 , 35 )
insert into tb2 values(5, '电信1', 2 , 75 )
insert into tb2 values(6, '公路1', 2 , 86)
insert into tb2 values(7, '公路2', 3 , 25 )
insert into tb2 values(8, '银行2', 3 , 65 )
insert into tb2 values(9, '电信4', 3 , 25 )
create table tb3 (id int, pnum int, itemid int)
insert into tb3 values(1, 20 , 2 )
insert into tb3 values(2, 40 , 3 )
insert into tb3 values(3, 30 , 1 )
insert into tb3 values(4, 180 , 6 )
insert into tb3 values(5, 20 , 2 )
insert into tb3 values(6, 40 , 5 )
insert into tb3 values(7, 30 , 1 )
insert into tb3 values(8, 180 , 7 )
go
select tb1.id , isnull(m.pnum,0) pnum , isnull(n.rooms,0) rooms from tb1
left join
(select parentid , sum(rooms) rooms from tb2 group by parentid) n
on tb1.id = n.parentid
left join
(select parentid , sum(pnum) pnum from tb3 , tb2 where tb3.itemid = tb2.id group by parentid) m
on tb1.id = m.parentid
order by pnum
drop table tb1,tb2,tb3
/*
id pnum rooms
----------- ----------- -----------
1 140 185
3 180 115
2 220 196
(所影响的行数为 3 行)
*/
create table tb1(id int, comname varchar(10) )
insert into tb1 values(1, 'moto')
insert into tb1 values(2, 'nokia')
insert into tb1 values(3, 'ibm')
create table tb2 (id int, itemname varchar(10), parentid int, rooms int)
insert into tb2 values(1, '银行' , 1 , 60 )
insert into tb2 values(2, '电信' , 1 , 80 )
insert into tb2 values(3, '公路' , 1 , 45 )
insert into tb2 values(4, '银行1', 2 , 35 )
insert into tb2 values(5, '电信1', 2 , 75 )
insert into tb2 values(6, '公路1', 2 , 86)
insert into tb2 values(7, '公路2', 3 , 25 )
insert into tb2 values(8, '银行2', 3 , 65 )
insert into tb2 values(9, '电信4', 3 , 25 )
create table tb3 (id int, pnum int, itemid int)
insert into tb3 values(1, 20 , 2 )
insert into tb3 values(2, 40 , 3 )
insert into tb3 values(3, 30 , 1 )
insert into tb3 values(4, 180 , 6 )
insert into tb3 values(5, 20 , 2 )
insert into tb3 values(6, 40 , 5 )
insert into tb3 values(7, 30 , 1 )
insert into tb3 values(8, 180 , 7 )
go
select tb1.id , m.pnum , n.rooms from tb1,
(select parentid , sum(rooms) rooms from tb2 group by parentid) n,
(select parentid , sum(pnum) pnum from tb3 , tb2 where tb3.itemid = tb2.id group by parentid) m
where tb1.id = n.parentid and tb1.id = m.parentid
order by pnum
drop table tb1,tb2,tb3
/*
id pnum rooms
----------- ----------- -----------
1 140 185
3 180 115
2 220 196
(所影响的行数为 3 行)
*/
create table tb1(id int, comname varchar(10) )
insert into tb1 values(1, 'moto')
insert into tb1 values(2, 'nokia')
insert into tb1 values(3, 'ibm')
create table tb2 (id int, itemname varchar(10), parentid int, rooms int)
insert into tb2 values(1, '银行' , 1 , 60 )
insert into tb2 values(2, '电信' , 1 , 80 )
insert into tb2 values(3, '公路' , 1 , 45 )
insert into tb2 values(4, '银行1', 2 , 35 )
insert into tb2 values(5, '电信1', 2 , 75 )
insert into tb2 values(6, '公路1', 2 , 86)
insert into tb2 values(7, '公路2', 3 , 25 )
insert into tb2 values(8, '银行2', 3 , 65 )
insert into tb2 values(9, '电信4', 3 , 25 )
create table tb3 (id int, pnum int, itemid int)
insert into tb3 values(1, 20 , 2 )
insert into tb3 values(2, 40 , 3 )
insert into tb3 values(3, 30 , 1 )
insert into tb3 values(4, 180 , 6 )
insert into tb3 values(5, 20 , 2 )
insert into tb3 values(6, 40 , 5 )
insert into tb3 values(7, 30 , 1 )
insert into tb3 values(8, 180 , 7 )
go
select tb1.id , sum(tb3.pnum) pnum , sum(tb2.rooms) rooms from tb1,tb2,tb3
where tb1.id = tb2.parentid and tb2.id = tb3.itemid
group by tb1.id
order by pnum
drop table tb1,tb2,tb3
/*
id pnum rooms
----------- ----------- -----------
1 140 325
3 180 25
2 220 161
(所影响的行数为 3 行)
*/
create table tb1(id int, comname varchar(10) )
insert into tb1 values(1, 'moto')
insert into tb1 values(2, 'nokia')
insert into tb1 values(3, 'ibm')
create table tb2 (id int, itemname varchar(10), parentid int, rooms int)
insert into tb2 values(1, '银行' , 1 , 60 )
insert into tb2 values(2, '电信' , 1 , 80 )
insert into tb2 values(3, '公路' , 1 , 45 )
insert into tb2 values(4, '银行1', 2 , 35 )
insert into tb2 values(5, '电信1', 2 , 75 )
insert into tb2 values(6, '公路1', 2 , 86)
insert into tb2 values(7, '公路2', 3 , 25 )
insert into tb2 values(8, '银行2', 3 , 65 )
insert into tb2 values(9, '电信4', 3 , 25 )
create table tb3 (id int, pnum int, itemid int)
insert into tb3 values(1, 20 , 2 )
insert into tb3 values(2, 40 , 3 )
insert into tb3 values(3, 30 , 1 )
insert into tb3 values(4, 180 , 6 )
insert into tb3 values(5, 20 , 2 )
insert into tb3 values(6, 40 , 5 )
insert into tb3 values(7, 30 , 1 )
insert into tb3 values(8, 180 , 7 )
go
select tb1.id , t.pnum , t.rooms from tb1,
(select m.parentid , sum(pnum) pnum , sum(rooms) rooms from tb2 m , tb3 n where m.id = n.itemid group by m.parentid) t
where tb1.id = t.parentid
order by t.pnum
drop table tb1,tb2,tb3
/*
id pnum rooms
----------- ----------- -----------
1 140 325
3 180 25
2 220 161
(所影响的行数为 3 行)
*/
select a.comname , isnull(m.rooms,0) rooms , isnull(c.人数,0) 人数 from tb1 a
left join
(select parentid , sum(rooms) rooms from tb2 group by parentid) m on a.id = m.parentid
left join
(select itemid , count(1) 人数 from tb3 group by itemid) n on m.id = n.itemid
order by n.人数 desc
select a.comname , isnull(m.rooms,0) rooms , isnull(c.人数,0) 人数 from tb1 a
left join
(select parentid , sum(rooms) rooms from tb2 group by parentid) m on a.id = m.parentid
left join
(select itemid , count(*) 人数 from tb3 group by itemid) n on m.id = n.itemid
order by n.人数
select a.comname,sum(c.pnum) as pnum
from 单位表 a,项目表 b,人数表 c
where a.id=b.parentid
and b.id=c.itemid
group by a.comname
order by sum(c.pnum) desc
没测试 你参考下吧
[code=SQL]
select comname,sum(pnum) from taba a left join tabb b on a.id=b.parentid left join tabc c on b.id=c.itemid group by comname order by sum(pnum) desc
[/code]select a.comname,sum(c.pnum) as pnum
from 单位表 a,项目表 b,人数表 c
where a.id=b.parentid
and b.id=c.itemid
group by a.comname
select a.comname,sum(c.pnum) as pnum
from 单位表 a,项目表 b,人数表 c
where a.id=b.parentid
and b.id=c.itemid
group by a.comname
select a.comname,sum(c.pnum) as pnum
from 单位表 a,项目表 b,人数表 c
where a.id=b.parentid
and b.id=c.itemid
group by a.comname
select comname,sum(pnum) from taba a left join tabb b on a.id=b.parentid left join tabc c on b.id=c.itemid group by comname group by sum(pnum) desc