avatar
i*o
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后,就卡主了。
求牛人解惑。。。
avatar
s*e
2
以前从来没见过,这里很多老房子都这样。Family Room是地上。
avatar
a*c
3
。。。
avatar
j*e
4
不考虑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后,就卡主了。
: 求牛人解惑。。。

avatar
N*p
6
这么厚实的套套,戴上之后肯定是什么感觉都没有了。
avatar
B*1
7
大牛,你拿了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

avatar
s*a
9
飞机上带这个多节省空间啊。以后空姐应该演示怎么吹,然后旅客自己带的机票打折。

【在 N******p 的大作中提到】
: 这么厚实的套套,戴上之后肯定是什么感觉都没有了。
avatar
i*o
10
大牛。瞬间解决掉。。

【在 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

avatar
A*l
11
The lowest level could feel colder, especially the floor.

【在 s********e 的大作中提到】
: 这样的结构有无很不好的地方?就觉得下楼梯容易碰头,
avatar
s*1
12
学术一下,可行么?
avatar
j*e
13
不是什么大牛,我天天写sql,比写代码还多,手熟而已

【在 B*******1 的大作中提到】
: 大牛,你拿了goog的offer以后做题更加猛啊,不歇一下?
avatar
s*e
14
Lower level 是和地面平齐的,Lowest level 就是半地下室了

【在 A*********l 的大作中提到】
: The lowest level could feel colder, especially the floor.
avatar
r*e
15
哈哈,这东西有用阿!!

【在 a*****c 的大作中提到】
: 。。。
avatar
i*o
16
熟练的才是自己的。BTW,能不能加问一道?
What is the best way of loading large amount of data into Oracle? Why?
是用hint --- append 么?怎么解释呢?

【在 j********e 的大作中提到】
: 不是什么大牛,我天天写sql,比写代码还多,手熟而已
avatar
R*n
17

Split level是50,60年代的主要建筑风格。楼上说的family room地板会比较冷算一个
缺点,还有一点是由于family room和客厅厨房都不在同一个level, 会觉得同一层的活
动空间不大,特别是开party的时候。但另一方面,由于层次的错开,privacy比较好。

【在 s********e 的大作中提到】
: Lower level 是和地面平齐的,Lowest level 就是半地下室了
avatar
B*1
18
汗,俺天天写寄存器。

【在 j********e 的大作中提到】
: 不是什么大牛,我天天写sql,比写代码还多,手熟而已
avatar
c*o
19
这种风格已经果实了,木昂吧
avatar
i*e
20
这个行吗?
select supplierID
from table
where productID in (
select distinct productID
from table
where supplierID = x) and
supplierID <> x;
avatar
j*e
21
看了你,才发现我那个应该加上 <> 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;

avatar
h*s
22
这就大牛了,CS的20万真是水

【在 i*********o 的大作中提到】
: 大牛。瞬间解决掉。。
avatar
B*g
23
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后,就卡主了。
: 求牛人解惑。。。

avatar
a*s
24
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后,就卡主了。
: 求牛人解惑。。。

avatar
j*e
25
如果不存在某个supplier能替代,而是要找出最少个supplier的集合
来替换,这不就成了set cover problem了,谁要是能用SQL解NP,
那真是牛了:)

【在 B*****g 的大作中提到】
: 1 如果本题改成找出所有符合条件的供应商怎么办?
: 2 如果现在数据如下:
: sid,pid
: 1,1
: 1,2
: 2,1
: 3,2
: X = 1, 你试一下楼下的答案是否符合你的要求
:
: supplier

avatar
B*g
26
楼上有答案了,基本上就是考group by要用having

【在 j********e 的大作中提到】
: 如果不存在某个supplier能替代,而是要找出最少个supplier的集合
: 来替换,这不就成了set cover problem了,谁要是能用SQL解NP,
: 那真是牛了:)

avatar
m*k
27
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,
: 那真是牛了:)

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。