34,836
社区成员




--建cat表
create table cat(cat_id int,cat_name varchar(15),prd_id varchar(15),type_id smallint)
insert into cat select 1,'cat_name_1','prd_id_1' ,1
union all select 2,'cat_name_2','prd_id_2' ,2
union all select 3,'cat_name_3','prd_id_3' ,3
union all select 4,'cat_name_4','prd_id_4' ,4
union all select 5,'cat_name_5','prd_id_5' ,5
--建class表
create table class(class_id int,class_name varchar(15),prd_id varchar(15),cat_prd_id varchar(15))
insert into class select 1,'class_name_1','prd_id_6','prd_id_2'
union all select 2,'class_name_2','prd_id_7' , 'prd_id_3'
union all select 3,'class_name_3','prd_id_8' ,'prd_id_4'
--建other表
create table other(other_id int,other_name varchar(15),prd_id varchar(15),cat_prd_id varchar(15))
insert into other select 1,'other_name_1','prd_id_9','prd_id_1'
union all select 2,'other_name_2','prd_id_10' , 'prd_id_4'
union all select 3,'other_name_3','prd_id_11' ,'prd_id_5'
--建prd_values表
create table prd_values (values_id int,prd_id varchar(15),prd_values int)
insert into prd_values select 1,'prd_id_1',1
union all select 2,'prd_id_2',2
union all select 3,'prd_id_3',3
union all select 4,'prd_id_4',4
union all select 5,'prd_id_5',5
union all select 6,'prd_id_6',6
union all select 7,'prd_id_7',7
union all select 8,'prd_id_8',8
union all select 9,'prd_id_9',9
union all select 10,'prd_id_10',10
union all select 11,'prd_id_11',11
go
--1,5 取r+l o
--2,3 取r c 取 l c
--4 取r+l o 取 l c
select a.cat_name,sum(prd_values) v from (select cat_name,b.prd_id,b.cat_prd_id from cat a,other b where a.type_id in(1,4,5) and a.prd_id = b.cat_prd_id) a
,prd_values b where a.prd_id=b.prd_id or a.cat_prd_id = b.prd_id group by a.cat_name -- 1(o)、4(o)、5(o) r+l
union
select a.cat_name,sum(prd_values) v from (select cat_name,b.cat_prd_id from cat a,class b where a.type_id in(2,3) and a.prd_id = b.cat_prd_id) a
,prd_values b where a.cat_prd_id=b.prd_id group by a.cat_name -- 2(c)、3(c) r
union
select a.cat_name+'xxx',sum(prd_values) v from (select cat_name,b.prd_id from cat a,class b where a.type_id in(2,3,4) and a.prd_id = b.cat_prd_id) a
,prd_values b where a.prd_id = b.prd_id group by a.cat_name+'xxx' -- 2(c)、3(c) l
/*
cat_name v
------------------ -----------
cat_name_1 10
cat_name_2 2
cat_name_2xxx 6
cat_name_3 3
cat_name_3xxx 7
cat_name_4 14
cat_name_4xxx 8
cat_name_5 16
*/
go
drop table cat, class,other,prd_values
--我将你的临时表改为实际表.
--建cat表
create table cat(cat_id int,cat_name varchar(15),prd_id varchar(15),type_id smallint)
insert into cat select 1,'cat_name_1','prd_id_1' ,1
union all select 2,'cat_name_2','prd_id_2' ,2
union all select 3,'cat_name_3','prd_id_3' ,3
union all select 4,'cat_name_4','prd_id_4' ,4
union all select 5,'cat_name_5','prd_id_5' ,5
--建class表
create table class(class_id int,class_name varchar(15),prd_id varchar(15),cat_prd_id varchar(15))
insert into class select 1,'class_name_1','prd_id_6','prd_id_2'
union all select 2,'class_name_2','prd_id_7' , 'prd_id_3'
union all select 3,'class_name_3','prd_id_8' ,'prd_id_4'
--建other表
create table other(other_id int,other_name varchar(15),prd_id varchar(15),cat_prd_id varchar(15))
insert into other select 1,'other_name_1','prd_id_9','prd_id_1'
union all select 2,'other_name_2','prd_id_10' , 'prd_id_4'
union all select 3,'other_name_3','prd_id_11' ,'prd_id_5'
--建prd_values表
create table prd_values (values_id int,prd_id varchar(15),prd_values int)
insert into prd_values select 1,'prd_id_1',1
union all select 2,'prd_id_2',2
union all select 3,'prd_id_3',3
union all select 4,'prd_id_4',4
union all select 5,'prd_id_5',5
union all select 6,'prd_id_6',6
union all select 7,'prd_id_7',7
union all select 8,'prd_id_8',8
union all select 9,'prd_id_9',9
union all select 10,'prd_id_10',10
union all select 11,'prd_id_11',11
--1、查找cat中prd_id不在class中的cat_prd_id的prd_id,为1,5。
select m1.cat_name , prd_values = (select prd_values from prd_values where prd_id = m2.prd_id) + (select prd_values from prd_values where prd_id = m2.cat_prd_id) from (select a.cat_name , a.prd_id from cat a where prd_id not in (select cat_prd_id from class)) m1,other m2 where m1.prd_id = m2.cat_prd_id
union all
--2、查找class中cat_prd_id不在other中cat_prd_id的cat_prd_id,为2,3,先算。。。不知道怎么解释.
select m1.cat_name , prd_values from cat m1 , (select cat_prd_id from class where cat_prd_id not in (select cat_prd_id from other)) m2,prd_values m3 where m1.prd_id = m2.cat_prd_id and m2.cat_prd_id = m3.prd_id
union all
--3、查找class中cat_prd_id不在other中cat_prd_id的cat_prd_id,为2,3,后算。。。不知道怎么解释.
select cat_name = m1.cat_name + '_xxx' , prd_values from cat m1 , (select prd_id , cat_prd_id from class where cat_prd_id not in (select cat_prd_id from other)) m2,prd_values m3 where m1.prd_id = m2.cat_prd_id and m2.prd_id = m3.prd_id
union all
--4、查找class中cat_prd_id在other中cat_prd_id的cat_prd_id,为4,先算。。。不知道怎么解释.
select m1.cat_name , prd_values = (select prd_values from prd_values where prd_id = m2.prd_id) + (select prd_values from prd_values where prd_id = m2.cat_prd_id) from cat m1 , (select prd_id , cat_prd_id from other where cat_prd_id in (select cat_prd_id from class)) m2 where m1.prd_id = m2.cat_prd_id
union all
--5、查找class中cat_prd_id在other中cat_prd_id的cat_prd_id,为4,后算。。。不知道怎么解释.
select cat_name = m1.cat_name + '_xxx' , prd_values from cat m1 , (select prd_id,cat_prd_id from class where cat_prd_id in (select cat_prd_id from other)) m2,prd_values m3 where m1.prd_id = m2.cat_prd_id and m2.prd_id = m3.prd_id
order by cat_name
drop table cat,class,other,prd_values
/*
cat_name prd_values
------------------- -----------
cat_name_1 10
cat_name_2 2
cat_name_2_xxx 6
cat_name_3 3
cat_name_3_xxx 7
cat_name_4 14
cat_name_4_xxx 8
cat_name_5 16
(所影响的行数为 8 行)
*/