由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 请教一个SQL Server的面试题
相关主题
菜鸟问题,急请教一个query 优化的问题(filter keyword)
sort two same tables SQL but different results请教一个SQL的问题
SQL copy a table into a new table and add a new columnSQL select one value column for each distinct value another (转载)
Access 里面两个 column不一样的table 能combine 到一起吗?怎样解决 Index for NULL value
请教一个sql问题求助:如何ColumName 作为变量放入query
一道面试题,求助问个简单的sql语句
sql 题目求助DB2 Stored Procedure Error Handler
谁能帮我看看这个Insert语句要怎么改一下?mySQL 问题 (转载)
相关话题的讨论汇总
话题: col1话题: col2话题: select话题: test话题: count
进入Database版参与讨论
1 (共1页)
j****s
发帖数: 881
1
最近有个面试,其中一题不知什么意思,如何写TSQL,特来请教版上大牛们:
有一个表Table1,有两个列如下:
Column 1 Column2
V1 B
V12 F
V3 F
V2 C
V2 D
V3 E
问题是:
Write a query that will dynamically eliminate the multiple relationships and
resulting in V1, B as the only 1 to 1 Relationship.
敬请大家解答,包子有谢!
g***l
发帖数: 18555
2
你抄对了吗?这题不知所云啊
j****s
发帖数: 881
3
题是抄对了,这是其中的一道,两个小时交卷。我当时云里雾里,就稀里糊涂地写:
Select distinct column1, distinct column2
From Table1
go
知道一定是错的,但是看不懂题啊。
大牛也不知所云,我心稍安。
g***l
发帖数: 18555
4
如果我是你,我就写上,这个题不知所云,根本就不知道是问什么,DBA就是要严谨,
看不懂,就说看不懂。

【在 j****s 的大作中提到】
: 题是抄对了,这是其中的一道,两个小时交卷。我当时云里雾里,就稀里糊涂地写:
: Select distinct column1, distinct column2
: From Table1
: go
: 知道一定是错的,但是看不懂题啊。
: 大牛也不知所云,我心稍安。

j****s
发帖数: 881
5
我当时以为是我太弱:-(

【在 g***l 的大作中提到】
: 如果我是你,我就写上,这个题不知所云,根本就不知道是问什么,DBA就是要严谨,
: 看不懂,就说看不懂。

g***l
发帖数: 18555
6
绝不要承认自己弱,弱的地方避开不谈,弱不弱由对方自己判定,找工作的时候谦虚是
不会有任何好处的。

【在 j****s 的大作中提到】
: 我当时以为是我太弱:-(
j****s
发帖数: 881
7
多谢!我的确要提高自信心,希望快点找到。

【在 g***l 的大作中提到】
: 绝不要承认自己弱,弱的地方避开不谈,弱不弱由对方自己判定,找工作的时候谦虚是
: 不会有任何好处的。

B*****g
发帖数: 34098
8
这题其实出的挺好的。
意思就是说把所有导致不是1to1的record都删了,也就是留下那些clo1的值在clo1列只
出现一次而且clo2的值在clo2列也只出现一次。如表中给的例子只有V1,B符合要求。
V12 F中的F和V3 F的F重了
V2 C和V2 D的V2重了
V3 E中的F和V3 F的V3重了

and

【在 j****s 的大作中提到】
: 最近有个面试,其中一题不知什么意思,如何写TSQL,特来请教版上大牛们:
: 有一个表Table1,有两个列如下:
: Column 1 Column2
: V1 B
: V12 F
: V3 F
: V2 C
: V2 D
: V3 E
: 问题是:

g***l
发帖数: 18555
9
我知道这个题的意思,HAVING COUNT(*)>1的都去除,啥叫dynamically eliminate the
multiple relationships,象这个到底留哪一个呢
V2 C
V2 D
这个题实际生活里并没有什么意思,纯是学究意淫。其实应该说ONLY KEEP 1 TO 1 RELATIONSHIP,就明确了,类似的考题我也做过一份。
select * from table
where col1 not in
(select col1 group by col1 having count(*)>1 )
B*****g
发帖数: 34098
10
dynamic的意思是不要写
delete from tab where clo1 <> 'V1'

the
RELATIONSHIP,就明确了,类似的考题我也做过一份。

【在 g***l 的大作中提到】
: 我知道这个题的意思,HAVING COUNT(*)>1的都去除,啥叫dynamically eliminate the
: multiple relationships,象这个到底留哪一个呢
: V2 C
: V2 D
: 这个题实际生活里并没有什么意思,纯是学究意淫。其实应该说ONLY KEEP 1 TO 1 RELATIONSHIP,就明确了,类似的考题我也做过一份。
: select * from table
: where col1 not in
: (select col1 group by col1 having count(*)>1 )

相关主题
一道面试题,求助请教一个query 优化的问题(filter keyword)
sql 题目求助请教一个SQL的问题
谁能帮我看看这个Insert语句要怎么改一下?SQL select one value column for each distinct value another (转载)
进入Database版参与讨论
M*********e
发帖数: 190
11
Don't know TSQL.
In Oracle, use rowid pseudocolumn.
思想就是先找到有discint col1的所有行(得到set 1)和有distict col2的所有行(得
到set 2)。取 set 1和 set 2中rowid相同的行.
不知道有没有漏洞。
#################
create table test(col1 varchar2(5), col2 varchar2(5));
insert into test values ('V1','B');
insert into test values ('V12','F');
insert into test values ('V3','F');
insert into test values ('V2','C');
insert into test values ('V2','D');
insert into test values ('V3','E');
#################
select * from test where rowid in
(
select rowid from test where col1 not in
(select col1 from test group by col1 having count(*)>1 )
intersect
select rowid from test where col2 not in
(select col2 from test group by col2 having count(*)>1 )
);
##################
COL1 COL2
M*********e
发帖数: 190
12
如果没有类似rowid的功能,可以在原table加一列,这一列只要每行unique,not null,就可以控制。

【在 M*********e 的大作中提到】
: Don't know TSQL.
: In Oracle, use rowid pseudocolumn.
: 思想就是先找到有discint col1的所有行(得到set 1)和有distict col2的所有行(得
: 到set 2)。取 set 1和 set 2中rowid相同的行.
: 不知道有没有漏洞。
: #################
: create table test(col1 varchar2(5), col2 varchar2(5));
: insert into test values ('V1','B');
: insert into test values ('V12','F');
: insert into test values ('V3','F');

M*********e
发帖数: 190
13
or use a cross join.
select * from test where (col1, col2) in
(
select * from
(
(select col1 from test where col1 not in
(select col1 from test group by col1 having count(*)>1 ))
cross join
(select col2 from test where col2 not in
(select col2 from test group by col2 having count(*)>1 ))
)
);
COL1 COL2
v*****r
发帖数: 1119
14
嗯,很好,就有点 double-negative,俺加一个茴的写法 (natual join):
select t1.*
from (select * from test where col1 in (select col1 from test group by
col1 having count(*)=1)) t1,
(select * from test where col2 in (select col2 from test group by
col2 having count(*)=1)) t2
where t1.col1 = t2.col1

【在 M*********e 的大作中提到】
: or use a cross join.
: select * from test where (col1, col2) in
: (
: select * from
: (
: (select col1 from test where col1 not in
: (select col1 from test group by col1 having count(*)>1 ))
: cross join
: (select col2 from test where col2 not in
: (select col2 from test group by col2 having count(*)>1 ))

B*****g
发帖数: 34098
15
人家要eliminate,你们几个在这里写select?如果要select,记得我曾经说过,本版
90%的sql问题其实就是partition by。

【在 v*****r 的大作中提到】
: 嗯,很好,就有点 double-negative,俺加一个茴的写法 (natual join):
: select t1.*
: from (select * from test where col1 in (select col1 from test group by
: col1 having count(*)=1)) t1,
: (select * from test where col2 in (select col2 from test group by
: col2 having count(*)=1)) t2
: where t1.col1 = t2.col1

j****s
发帖数: 881
16
多谢上面几位解释,尤其是北京姐姐,把题目的意思讲清楚了。
另一种写法:
select col1, col2 from table
where clo1 not in
(select col1, count(col2) from table
group by clo1
having count(col2)>1
Intersect
select col1, col2 from table
select col2, count(col1) from table
group by clo2
having count(col1)>1)
记下IDs,发包子去了。
B*****g
发帖数: 34098
17
有包子,上partition by solution
SELECT col1, col2
FROM
(SELECT col1,
col2,
COUNT(1) OVER(PARTITION BY col1) AS 'col1count',
COUNT(1) OVER(PARTITION BY col2) AS 'col2count'
FROM table) t
WHERE t.col1count = 1 and t.col2count = 1


98.248.]

【在 j****s 的大作中提到】
: 多谢上面几位解释,尤其是北京姐姐,把题目的意思讲清楚了。
: 另一种写法:
: select col1, col2 from table
: where clo1 not in
: (select col1, count(col2) from table
: group by clo1
: having count(col2)>1
: Intersect
: select col1, col2 from table
: select col2, count(col1) from table

j****s
发帖数: 881
18
学习了。

【在 B*****g 的大作中提到】
: 有包子,上partition by solution
: SELECT col1, col2
: FROM
: (SELECT col1,
: col2,
: COUNT(1) OVER(PARTITION BY col1) AS 'col1count',
: COUNT(1) OVER(PARTITION BY col2) AS 'col2count'
: FROM table) t
: WHERE t.col1count = 1 and t.col2count = 1
:

p*********d
发帖数: 136
19
this way:
select c1, c2 from tab1
where c1 not in (select c1 from tab1 group by c1 having count(c2) > 1)
and c2 not in (select c2 from tab1 group c2 having count(c1) >1);
j****s
发帖数: 881
20
Good!
吃包子。

【在 p*********d 的大作中提到】
: this way:
: select c1, c2 from tab1
: where c1 not in (select c1 from tab1 group by c1 having count(c2) > 1)
: and c2 not in (select c2 from tab1 group c2 having count(c1) >1);

1 (共1页)
进入Database版参与讨论
相关主题
mySQL 问题 (转载)请教一个sql问题
sql query help一道面试题,求助
A rookie's query questionsql 题目求助
问个sql/ ssis的问题 谢谢!谁能帮我看看这个Insert语句要怎么改一下?
菜鸟问题,急请教一个query 优化的问题(filter keyword)
sort two same tables SQL but different results请教一个SQL的问题
SQL copy a table into a new table and add a new columnSQL select one value column for each distinct value another (转载)
Access 里面两个 column不一样的table 能combine 到一起吗?怎样解决 Index for NULL value
相关话题的讨论汇总
话题: col1话题: col2话题: select话题: test话题: count