首页 > 试题广场 >

商品交易(网易校招笔试真题)

[编程题]商品交易(网易校招笔试真题)
  • 热度指数:32635 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
如下有一张商品表(goods),字段依次为:商品id、商品名、商品质量
id name weight
1 A1 100
2 A2 20
3 B3 29
4 T1 60
5 G2 33
6 C0 55

还有一张交易表(trans),字段依次为:交易id、商品id、这个商品购买个数
id goods_id count
1 3 10
2 1 44
3 6 9
4 1 2
5 2 65
6 5 23
7 3 20
8 2 16
9 4 5
10 1 3

查找购买个数超过20,质量小于50的商品,按照商品id升序排序,如:
id name weight total
2 A2 20 81
3 B3 29 30
5 G2 33 23


示例1

输入

CREATE TABLE `goods` (
  `id` int(11) NOT NULL,
  `name` varchar(10)  DEFAULT NULL,
  `weight` int(11) NOT NULL,
  PRIMARY KEY (`id`)
);
CREATE TABLE `trans` (
  `id` int(11) NOT NULL,
  `goods_id` int(11) NOT NULL,
  `count` int(11) NOT NULL,
  PRIMARY KEY (`id`)
);
insert into goods values(1,'A1',100);
insert into goods values(2,'A2',20);
insert into goods values(3,'B3',29);
insert into goods values(4,'T1',60);
insert into goods values(5,'G2',33);
insert into goods values(6,'C0',55);
insert into trans values(1,3,10);
insert into trans values(2,1,44);
insert into trans values(3,6,9);
insert into trans values(4,1,2);
insert into trans values(5,2,65);
insert into trans values(6,5,23);
insert into trans values(7,3,20);
insert into trans values(8,2,16);
insert into trans values(9,4,5);
insert into trans values(10,1,3);

输出

2|A2|20|81
3|B3|29|30
5|G2|33|23
select aa.id,aa.name,aa.weight,bb.total  from goods aa
left join 
(select goods_id,sum(count) as total from trans group by goods_id) bb 
on bb.goods_id=aa.id
where aa.weight<50 and bb.total>20
order by id

发表于 2022-05-18 16:51:18 回复(0)
select a.id,a.name,a.weight,
sum(b.count) as total
from goods a 
inner join trans b 
on a.id=b.goods_id
group by a.id
having total>20 and weight<50
order by a.id

发表于 2022-03-21 15:28:57 回复(0)


SELECT g.*,SUM(COUNT) AS total
FROM goods g
LEFT JOIN trans t ON g.id=t.goods_id
GROUP BY g.id
HAVING  weight<50
AND total>20
order by  g.id


发表于 2021-10-06 09:04:48 回复(0)
select 
g.id,g.name,g.weight,t.sum_cnt as total
from          
(select 
 t1.goods_id,sum(t1.count) as sum_cnt
from trans t1 
group by  t1.goods_id ) t
join 
goods g 
on t.goods_id=g.id
where t.sum_cnt>20 and g.weight<50
order by g.id
发表于 2021-09-30 11:31:14 回复(0)
这个哪里有问题呀?一直提醒我not yet  supoorted place for UDFA 'sum'
我在hive里面操作的

发表于 2021-09-30 11:07:50 回复(0)
  • 二维码

    扫描二维码,关注牛客网

  • 二维码

    下载牛客APP,随时随地刷题