由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - 这道SQL题目怎么解
相关主题
有人给大包子了Re: 数据库真烦人
SQL 请教[转载] 申请Database版版主
Questions about SQL Server Lock timeoutSQL server 和SQL anywhere的区别是什么?
Job opportunity Chesapeake, VAQuestions on SQL
再来一个SQL Server的面试题PB SQL语句的简单问题
Re: [转载] JDBC用完了oracle的large pool (memor能从C++中执行一个SQL文件么?比如createtable.sql
Re: A question for SQL Server初级问题
你们喜欢用哪个SQL Tools?oracle和XML
相关话题的讨论汇总
话题: rating话题: t10话题: 01话题: 2011话题: reviewer
进入Database版参与讨论
1 (共1页)
h****y
发帖数: 33
1
scheme:
Movie
mID title year director
101 Gone with the Wind 1939 Victor Fleming
102 Star Wars 1977 George Lucas
103 The Sound of Music 1965 Robert Wise
...
Reviewer
rID name
201 Sarah Martinez
202 Daniel Lewis
203 Brittany Harris
204 Mike Anderson
...
Rating
rID mID stars ratingDate
201 101 2 2011-01-22
201 101 4 2011-01-27
202 106 4
203 103 2 2011-01-20
203 108 4 2011-01-12
203 108 2 2011-01-30
204 101 3 2011-01-09
205 103 3 2011-01-27
205 104 2 2011-01-22
205 108 4
206 107 3 2011-01-15
206 106 5 2011-01-19
...
Question:
For all cases where the same reviewer rated the same movie twice and gave it
a higher rating the second time, return the reviewer's name and the title
of the movie.
------
怎么判断前后两次rating是否第二次更高?是需要用self join吗?
谢谢啦!
l******b
发帖数: 39
2
这道题可以这样考虑. 在Rating表中找出同一个reviewer对同一部movie评分2次, 而且
第二次比第一次高的所有记录.
等价于, 对rating表自联接后,对同一部movie同一个reviewer只有一条满足条件的记录.
可以这么写:
select rID, mID
from ( select R1.rID, R1.mID, R1.stars,R2.stars
from Rating R1 join Rating R2
where R1.rID = R2.rID
and R1.mID = R2.mID
and R1.ratingDate < R2.ratingDate
and R1.stars < R2.stars)
group by rID, mID
having count(*)=1;
s****a
发帖数: 9912
3
这不又是PARTITION BY的问题吗?
h****y
发帖数: 33
4
非常感谢大牛的指导!
用having count(*) = 1可以检测出rate twice并且递增的reviewer。
如果换下题目,更复杂的情况,问the same reviewer rate exactly three times and
rates are in ascending order又该怎么写呢?
或者问: find out reviewers who rated in ascending order at least three times
for the same movie. (比如reviewer 1 给moive 1的rate按时间排列为: 2,3,4,
1; 虽然最后一次比之前都低,但有已经满足有三次是递增的,这种情况也是我们想要
检测到的)

录.

【在 l******b 的大作中提到】
: 这道题可以这样考虑. 在Rating表中找出同一个reviewer对同一部movie评分2次, 而且
: 第二次比第一次高的所有记录.
: 等价于, 对rating表自联接后,对同一部movie同一个reviewer只有一条满足条件的记录.
: 可以这么写:
: select rID, mID
: from ( select R1.rID, R1.mID, R1.stars,R2.stars
: from Rating R1 join Rating R2
: where R1.rID = R2.rID
: and R1.mID = R2.mID
: and R1.ratingDate < R2.ratingDate

h****y
发帖数: 33
5
好像很多时候(e.g. 面试)不让用partition by,所以只能用self join.

【在 s****a 的大作中提到】
: 这不又是PARTITION BY的问题吗?
N***3
发帖数: 801
6
是不是可以这样,
至少3次
select * from rating s1
where (select count(*)
from rating s2
where s1.rId=s2.rId
and s1.mId=s2.mId
and s2.ratingdate>s1.ratingdate
and s2.stars>s1.stars
) >=2
只有3次再加
and(select count(*)
from rating s2
where s1.rId=s2.rId
and s1.mId=s2.mId
)=3
and
times
B*****g
发帖数: 34098
7
你这题和前一阵DS版一道题类似,你这个更难点
http://www.mitbbs.com/article_t/DataSciences/11633.html
可以试着把题目推到更普遍的情况,连续至少N个递增,N是一个参数,从web app传过
来。我觉得这样思考解题方法反而更容易一些,不会被2,3,4这些具体的数困扰
你这个是面试题还是工作需要?要是面试题不让用partition by有点废人的节奏,当然
我指的是N的情况。
要是有大包子的话一会得空写个解题过程,答案实在不值一提

and
times

【在 h****y 的大作中提到】
: 非常感谢大牛的指导!
: 用having count(*) = 1可以检测出rate twice并且递增的reviewer。
: 如果换下题目,更复杂的情况,问the same reviewer rate exactly three times and
: rates are in ascending order又该怎么写呢?
: 或者问: find out reviewers who rated in ascending order at least three times
: for the same movie. (比如reviewer 1 给moive 1的rate按时间排列为: 2,3,4,
: 1; 虽然最后一次比之前都低,但有已经满足有三次是递增的,这种情况也是我们想要
: 检测到的)
:
: 录.

m******u
发帖数: 12400
8
‘第二次’这是个时间概念吧。要比较rating time。如果第二次比第一次评的低,就
不符合要求吧。
l******b
发帖数: 39
9

and
times
首先,我不是大牛啊.大牛是Beijing ...
但是,我可以跟你探讨一下这个问题的解法.
为了说明我的思路, 我先建个表, 然后往里面插入10条记录.
create table t10(time int, score int);
insert into t10(time, score) values(1,25);
insert into t10(time, score) values(2,16);
insert into t10(time, score) values(3,50);
insert into t10(time, score) values(4,29);
insert into t10(time, score) values(5,45);
insert into t10(time, score) values(6,64);
insert into t10(time, score) values(7,44);
insert into t10(time, score) values(8,62);
insert into t10(time, score) values(9,23);
insert into t10(time, score) values(10,19);
然后查询下, 可得到如下结果:
select * from t10;
Time Score
-----------------
1 25
2 16
3 50
4 29
5 45
6 64
7 44
8 62
9 23
10 19
然后通过下面查询, 可以找到哪一次time它的前后两次评分与它本身一起构成
了与该时间序列相同的评分序列.
select T, S, sum(sign) as Sign
from ( select A.time as T, A.score as S, B.time, B.score
,(case
when A.time > B.time and A.score > B.score then 1
when A.time < B.time and A.score < B.score then 1
else 0
end ) as sign
from t10 A join t10 B
on A.time - B.time = 1 OR A.time - B.time = -1)
group by T,S
order by T,S ;
-------------------------------------------------
T  S Sign
1 25 0
2 16 1
3 50 1
4 29 1
5 45 2
6 64 1
7 44 1
8 62 1
9 23 0
10 19 0
这个查询返回结果中时间序列为5的记录对应标志位为2,表明
它的前后面评分同它一起构成了一个与时间同序的升序序列, 即,
4,5,6三次评分是连续增大的.
//注:上面最后一个order by并非必须的.
另外, 对于同一个评分人只要有一条他所对应的时间序列标识位为2,
那么他就符合标准.
其实用这个思路, 你已经可以提供一个partition by的算法实现.
另外,假定你知道如何用子查询而不是partition by来实现
对任意字段的排序.
上面不知道能否解答你的问题.
和你一样等Beijing大牛给我们提供一种推广到N的算法.

【在 h****y 的大作中提到】
: 非常感谢大牛的指导!
: 用having count(*) = 1可以检测出rate twice并且递增的reviewer。
: 如果换下题目,更复杂的情况,问the same reviewer rate exactly three times and
: rates are in ascending order又该怎么写呢?
: 或者问: find out reviewers who rated in ascending order at least three times
: for the same movie. (比如reviewer 1 给moive 1的rate按时间排列为: 2,3,4,
: 1; 虽然最后一次比之前都低,但有已经满足有三次是递增的,这种情况也是我们想要
: 检测到的)
:
: 录.

B*****g
发帖数: 34098
10
我在等大包子

【在 l******b 的大作中提到】
:
: and
: times
: 首先,我不是大牛啊.大牛是Beijing ...
: 但是,我可以跟你探讨一下这个问题的解法.
: 为了说明我的思路, 我先建个表, 然后往里面插入10条记录.
: create table t10(time int, score int);
: insert into t10(time, score) values(1,25);
: insert into t10(time, score) values(2,16);
: insert into t10(time, score) values(3,50);

l******b
发帖数: 39
11
我太穷了, 嘿嘿。 等lz给你发大包子。
1 (共1页)
进入Database版参与讨论
相关主题
oracle和XML再来一个SQL Server的面试题
[转载] LDAP + SQLRe: [转载] JDBC用完了oracle的large pool (memor
Oracle Lite 4.0 on 95Re: A question for SQL Server
How is DB 2?你们喜欢用哪个SQL Tools?
有人给大包子了Re: 数据库真烦人
SQL 请教[转载] 申请Database版版主
Questions about SQL Server Lock timeoutSQL server 和SQL anywhere的区别是什么?
Job opportunity Chesapeake, VAQuestions on SQL
相关话题的讨论汇总
话题: rating话题: t10话题: 01话题: 2011话题: reviewer