由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 被一个面试题卡的泪流满面 SQL
相关主题
error of executing SQL query of string concatenation (转载想问问哪里可以找到DB或者SQL的面试题?
请教SQL问题请教SQL面试题
error of sql query in MS Access databaseSQL find distinct values in large table (转载)
求教一个SQL的问题真诚求建议
SQL combine two tables into one table and add a new columnlinkedin上看到的一个找中国人作sourcing的工作
SQL copy a table into a new table and add a new column (转载)我们公司(制造业)在找以下几个POSITION。地点在ALTOONA, PA
Does this kind of Query Make Sense?T-mobile is hiring
一个hibernate insert db的问题请教 跳槽,jm, 和职业方向 (转载)
相关话题的讨论汇总
话题: suppliers话题: select话题: supplier话题: sql话题: sid
进入JobHunting版参与讨论
1 (共1页)
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 的大作中提到】
: 大牛。瞬间解决掉。。
相关主题
SQL copy a table into a new table and add a new column (转载)想问问哪里可以找到DB或者SQL的面试题?
Does this kind of Query Make Sense?请教SQL面试题
一个hibernate insert db的问题SQL find distinct values in large table (转载)
进入JobHunting版参与讨论
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,
: 那真是牛了:)

1 (共1页)
进入JobHunting版参与讨论
相关主题
请教 跳槽,jm, 和职业方向 (转载)SQL combine two tables into one table and add a new column
芝加哥北部会计工作机会SQL copy a table into a new table and add a new column (转载)
请问,有没有招buyer/supply chain?Does this kind of Query Make Sense?
Job opportunities in Greater Seattle areas (转载)一个hibernate insert db的问题
error of executing SQL query of string concatenation (转载想问问哪里可以找到DB或者SQL的面试题?
请教SQL问题请教SQL面试题
error of sql query in MS Access databaseSQL find distinct values in large table (转载)
求教一个SQL的问题真诚求建议
相关话题的讨论汇总
话题: suppliers话题: select话题: supplier话题: sql话题: sid