i*********o 发帖数: 52 | 1 各位ID轻拍,今天被一个SQL题目卡的泪流满面。
题目如下:
There is a table of suppliers which has supplier id, product id. Each
supplier offers multiple products. Find a replacement supplier for supplier
with ID=X.
本来以为挺简单,但是做起来感觉不是那么回事。我先选出那个id是x的供应商能提供
的商品。
然后跟其他供应商能提供的商品做个集合比较。但是用了group by后,就卡主了。
求牛人解惑。。。 |
j********e 发帖数: 1192 | 2 不考虑performance,可以:
select t1.sid, count(*) n from tbl t1,
(select pid from tbl where sid=x) t2
where t1.pid = t2.pid and t1.sid != x group by t1.sid
order by n desc limit 1;
supplier
【在 i*********o 的大作中提到】 : 各位ID轻拍,今天被一个SQL题目卡的泪流满面。 : 题目如下: : There is a table of suppliers which has supplier id, product id. Each : supplier offers multiple products. Find a replacement supplier for supplier : with ID=X. : 本来以为挺简单,但是做起来感觉不是那么回事。我先选出那个id是x的供应商能提供 : 的商品。 : 然后跟其他供应商能提供的商品做个集合比较。但是用了group by后,就卡主了。 : 求牛人解惑。。。
|
B*******1 发帖数: 2454 | 3 大牛,你拿了goog的offer以后做题更加猛啊,不歇一下?
【在 j********e 的大作中提到】 : 不考虑performance,可以: : select t1.sid, count(*) n from tbl t1, : (select pid from tbl where sid=x) t2 : where t1.pid = t2.pid and t1.sid != x group by t1.sid : order by n desc limit 1; : : supplier
|
i*********o 发帖数: 52 | 4 大牛。瞬间解决掉。。
【在 j********e 的大作中提到】 : 不考虑performance,可以: : select t1.sid, count(*) n from tbl t1, : (select pid from tbl where sid=x) t2 : where t1.pid = t2.pid and t1.sid != x group by t1.sid : order by n desc limit 1; : : supplier
|
j********e 发帖数: 1192 | 5 不是什么大牛,我天天写sql,比写代码还多,手熟而已
【在 B*******1 的大作中提到】 : 大牛,你拿了goog的offer以后做题更加猛啊,不歇一下?
|
i*********o 发帖数: 52 | 6 熟练的才是自己的。BTW,能不能加问一道?
What is the best way of loading large amount of data into Oracle? Why?
是用hint --- append 么?怎么解释呢?
【在 j********e 的大作中提到】 : 不是什么大牛,我天天写sql,比写代码还多,手熟而已
|
B*******1 发帖数: 2454 | 7 汗,俺天天写寄存器。
【在 j********e 的大作中提到】 : 不是什么大牛,我天天写sql,比写代码还多,手熟而已
|
i******e 发帖数: 273 | 8 这个行吗?
select supplierID
from table
where productID in (
select distinct productID
from table
where supplierID = x) and
supplierID <> x; |
j********e 发帖数: 1192 | 9 看了你,才发现我那个应该加上 <> x
我不太喜欢用in,因为一旦用了in,mysql就傻傻的不用index了
你这个会选出来所有的跟x有overlap的supplier,还得再count一下
【在 i******e 的大作中提到】 : 这个行吗? : select supplierID : from table : where productID in ( : select distinct productID : from table : where supplierID = x) and : supplierID <> x;
|
h******s 发帖数: 3420 | 10 这就大牛了,CS的20万真是水
【在 i*********o 的大作中提到】 : 大牛。瞬间解决掉。。
|
|
|
B*****g 发帖数: 34098 | 11 1 如果本题改成找出所有符合条件的供应商怎么办?
2 如果现在数据如下:
sid,pid
1,1
1,2
2,1
3,2
X = 1, 你试一下楼下的答案是否符合你的要求
supplier
【在 i*********o 的大作中提到】 : 各位ID轻拍,今天被一个SQL题目卡的泪流满面。 : 题目如下: : There is a table of suppliers which has supplier id, product id. Each : supplier offers multiple products. Find a replacement supplier for supplier : with ID=X. : 本来以为挺简单,但是做起来感觉不是那么回事。我先选出那个id是x的供应商能提供 : 的商品。 : 然后跟其他供应商能提供的商品做个集合比较。但是用了group by后,就卡主了。 : 求牛人解惑。。。
|
a*******s 发帖数: 324 | 12 select q.sid from (select pid from table1 where sid = X) as p inner join
table1 as q using(pid) where q.sid != X group by 1 having count(*) >=(select
count(*) from table1 where sid = X) limit 1;
MySQL
supplier
【在 i*********o 的大作中提到】 : 各位ID轻拍,今天被一个SQL题目卡的泪流满面。 : 题目如下: : There is a table of suppliers which has supplier id, product id. Each : supplier offers multiple products. Find a replacement supplier for supplier : with ID=X. : 本来以为挺简单,但是做起来感觉不是那么回事。我先选出那个id是x的供应商能提供 : 的商品。 : 然后跟其他供应商能提供的商品做个集合比较。但是用了group by后,就卡主了。 : 求牛人解惑。。。
|
j********e 发帖数: 1192 | 13 如果不存在某个supplier能替代,而是要找出最少个supplier的集合
来替换,这不就成了set cover problem了,谁要是能用SQL解NP,
那真是牛了:)
【在 B*****g 的大作中提到】 : 1 如果本题改成找出所有符合条件的供应商怎么办? : 2 如果现在数据如下: : sid,pid : 1,1 : 1,2 : 2,1 : 3,2 : X = 1, 你试一下楼下的答案是否符合你的要求 : : supplier
|
B*****g 发帖数: 34098 | 14 楼上有答案了,基本上就是考group by要用having
【在 j********e 的大作中提到】 : 如果不存在某个supplier能替代,而是要找出最少个supplier的集合 : 来替换,这不就成了set cover problem了,谁要是能用SQL解NP, : 那真是牛了:)
|
m*****k 发帖数: 731 | 15 this will return the answer if the answer is there, but may return wrong
answer if the ansswer is not there, check this:
CREATE TABLE IF NOT EXISTS suppliers
(
sid TEXT ,
pid INTEGER
);
INSERT INTO suppliers VALUES ( '1', 2);
INSERT INTO suppliers VALUES ( '1', 3);
INSERT INTO suppliers VALUES ( '2', 2);
INSERT INTO suppliers VALUES ( '2', 4);
suppose x = '1'
you sql will return
2|1
but of coz 2 is not the ans.
while this following sql will return the right answer:
select distinct sid from suppliers g1
where
(
select count(*)
from
(select g2.pid from suppliers g2, suppliers g3 where g2.sid = g1.sid and
g2.pid = g3.pid and g3.sid='1') joined
)
=
(select count(*) from suppliers where sid='1')
and sid <> '1';
【在 j********e 的大作中提到】 : 如果不存在某个supplier能替代,而是要找出最少个supplier的集合 : 来替换,这不就成了set cover problem了,谁要是能用SQL解NP, : 那真是牛了:)
|