一家小公司的面试题:
id tcode_name num parent_id
1 甲 0 0
2 乙 0 0
3 电视机 3 1
4 冰箱 4 1
5 冰箱 2 2
6 电视机 1 2
7 空调 1 2
使用1条sql语句统计出甲组和乙组各有多少种家电及各种家电的数量
id tcode_name num parent_id
1 甲 0 0
2 乙 0 0
3 电视机 3 1
4 冰箱 4 1
5 冰箱 2 2
6 电视机 1 2
7 空调 1 2
使用1条sql语句统计出甲组和乙组各有多少种家电及各种家电的数量
from
(select * from tb where parent_id=0)a,
(select * from tb where parent_id<>0)b
where a.id=b.parent_id
group by a.tcode_name,b.tcode_name
select b.tcode_name,sum(a.num) from test a join test b on(b.id=a.parent_id) group by b.tcode_name;
from mytt a join mytt b
on(a.parent_id=b.id)
group by b.tcode_name
insert into tb1
select 1,'甲',0,0
union all
select 2,'已',0,0
union all
select 3,'电视机',3,1
union all
select 4,'冰箱',4,1
union all
select 5,'冰箱',2,2
union all
select 6,'电视机',1,2
union all
select 6,'空调',1,2select * from tb1
select * from tb1 t1 where t1.parent_id=0
select * from tb1 t2 where t2.parent_id <>0select a.tcode_name,b.tcode_name, sum(b.num) '数量'
from (select * from tb1 t1 where t1.parent_id=0) a,(select * from tb1 t2 where t2.parent_id <>0) b
where a.id=b.parent_id
group by a.tcode_name,b.tcode_name order by a.tcode_name,b.tcode_name/**
--结果
甲 冰箱 4
甲 电视机 3
已 冰箱 2
已 电视机 1
已 空调 1
*/
from
(select * from tb where parent_id=0)a,
(select * from tb where parent_id <>0)b
where a.id=b.parent_id
group by a.name,b.name
这个结果符合要求:
甲 冰箱 4
乙 冰箱 2
甲 电视机 3
乙 电视机 1
乙 空调 1
select decode(grouping(a.tcode_name),1,'总计电器数',a.tcode_name) 个人,decode(grouping(b.tcode_name),1,'电器总数',b.tcode_name) 电器, sum(b.num) 数量
from (select * from tb1 t1 where t1.parent_id=0) a,(select * from tb1 t2 where t2.parent_id <>0) b
where a.id=b.parent_id
group by rollup(a.tcode_name,b.tcode_name)
WITH a AS
(SELECT 1 ID, '甲' tcode_name, 0 num, 0 parent_id
FROM DUAL
UNION ALL
SELECT 2, '已', 0, 0
FROM DUAL
UNION ALL
SELECT 3, '电视机', 3, 1
FROM DUAL
UNION ALL
SELECT 4, '冰箱', 4, 1
FROM DUAL
UNION ALL
SELECT 5, '冰箱', 2, 2
FROM DUAL
UNION ALL
SELECT 6, '电视机', 1, 2
FROM DUAL
UNION ALL
SELECT 6, '空调', 1, 2
FROM DUAL)
SELECT group_name tcode_name, COUNT (*) 种类数, SUM (num) 数量
FROM (SELECT a.*, CONNECT_BY_ROOT (tcode_name) group_name
FROM a
START WITH parent_id = 0
CONNECT BY parent_id = PRIOR ID) a
WHERE a.parent_id > 0
GROUP BY group_nameTCODE_NAME 家电种类数 家电总数量已 3 4
甲 2 7
1 甲 0 0
2 乙 0 0
3 电视机 3 1
4 冰箱 4 1
5 冰箱 2 2
6 电视机 1 2
7 空调 1 2 使用1条sql语句统计出甲组和乙组各有多少种家电及各种家电的数量select parent_id,tcode_name,sum(num)from table
where parent_id in(
select id from table
where parent_id =0)
group by parent_id,tcode_name
order by parent_id,tcode_name
from
(select * from tb where parent_id=0)a,
(select * from tb where parent_id <>0)b
where a.id=b.parent_id
group by a.name,b.name
这个结果符合要求:
甲 冰箱 4
乙 冰箱 2
甲 电视机 3
乙 电视机 1
乙 空调 1
甲 电器种类数 2
甲 冰箱 4
甲 电视机 3
乙 电器种类数 3
乙 冰箱 2
乙 电视机 1
乙 空调 1
如果这样的,我没别的办法
只能用两个SQL去Unionselect a.tcode_name,b.tcode_name,b.num
from tablename a,
(select parent_id,'电器种类数' tcode_name,count(*) num
from tablename
where parent_id<>0
group by parent_id
union
select parent_id,tcode_name,sum(num) num
from tablename
where parent_id<>0
group by parent_id,tcode_name) b
where a.id=b.parent_id
(SELECT 1 ID, '甲' tcode_name, 0 num, 0 parent_id
FROM DUAL
UNION ALL
SELECT 2, '已', 0, 0
FROM DUAL
UNION ALL
SELECT 3, '电视机', 3, 1
FROM DUAL
UNION ALL
SELECT 4, '冰箱', 4, 1
FROM DUAL
UNION ALL
SELECT 5, '冰箱', 2, 2
FROM DUAL
UNION ALL
SELECT 6, '电视机', 1, 2
FROM DUAL
UNION ALL
SELECT 6, '空调', 1, 2
FROM DUAL)
select substr(tcode_name, 1, 1) as tcode_name, count(*) as cnt, sum(num)
from (
select a.id, a.num, a.parent_id, ltrim(sys_connect_by_path(a.tcode_name, ','), ',') as tcode_name
from a
start with parent_id = 0
connect by parent_id = prior id
) x
where x.parent_id != 0
group by substr(x.tcode_name, 1, 1)