国内某知名互联网企业的一道压轴笔试题
以下为一用户兴趣记录表的示例数据,其中User_id为用户编号,Interest_Id为兴趣编号,求出与某用户兴趣完全一致的所有用户(如此示例数据中,与用户编号为1的用户的兴趣完全一致的用户为用户编号为3和8的两位用户),请写出相关的SQL语句(遵循SQL92标准,不得使用T-SQL,PL/SQL等扩展语言)。
User_id Interest_Id
1 1
1 22 1
2 3
3 1
3 24 1
4 2
4 3
5 7
5 8
6 1
7 2
8 1
8 29 1
9 3
9 7
9 8
10 9
10 11
[解决办法]
SELECT * FROM TB T WHERE User_id
IN(
SELECT User_id FROM TB
WHERE Interest_Id IN(SELECT Interest_Id FROM TB WHERE User_id=1)
GROUP BY USER_ID HAVING COUNT(1)=(SELECT COUNT(1) FROM TB WHERE User_id=1)
)
[解决办法]
select id from tb where Interest_Id=(select top 1 Interest_Id from tb where User_id=1) and User_id<>1
[解决办法]
我插,被忽悠了
select id from tb where Interest_Id=(select top 1 Interest_Id from tb where [User_id]=1) and [User_id]<>1
[解决办法]
又被忽悠了
select [User_id] from tbwhere Interest_Id=(select top 1 Interest_Id from tb where [User_id]=1)and [User_id]<>1
[解决办法]
1 1
1 22 1
2 3
3 1
3 24 1
4 2
4 3
5 7
5 8
6 1
7 2
8 1
8 29 1
9 3
9 7
9 8
10 9
10 11
麻烦楼主把红色部分解释一下
[解决办法]
create table abc( userid int, Interestid int);insert into abc values (1,1)insert into abc values (1,22)insert into abc values (2,3)insert into abc values (3,1)insert into abc values (3,24)insert into abc values (4,2)insert into abc values (4,3)insert into abc values (4,4)insert into abc values (4,5)insert into abc values (5,8)insert into abc values (6,1)insert into abc values (7,4)insert into abc values (8,1)insert into abc values (8,29)insert into abc values (9,11)select userid from abc where Interestid in (select count(Interestid)*10+userid+(select top 1 Interestid from abc) as row from abc group by userid)
[解决办法]
/*
M
as
(
select distinct a.*,
case when a.zd1=b.zd1 then null else b.zd1 end as col1,
case when a.zd2=b.zd2 then null else b.zd2 end as col2,
case when a.zd1=b.zd1 and a.zd2=b.zd2 and a.zd3=b.zd3 then null else b.zd3 end as col3,
case when a.zd1=b.zd1 and a.zd2=b.zd2 and a.zd4=b.zd4 then null else b.zd4 end as col4
from T a inner join T b on a.zd1=b.zd1
)
select *from M
*/
select
case when px=1 then zd1 else null end as zd1,
case when px=1 then zd2 else null end as zd2,
case when a.zd3=zd3 then zd3 else null end as zd3,
case when a.zd4=zd4 then zd4 else null end as zd4
from
(select px=row_number()over(partition by zd1 order by getdate()),* from T)a
inner join T on a.zd1=T.zd1
/*
以下为一用户兴趣记录表的示例数据,其中User_id为用户编号,Interest_Id为兴趣编号,
求出与某用户兴趣完全一致的所有用户(如此示例数据中,与用户编号为1的用户的兴趣完
全一致的用户为用户编号为3和8的两位用户),请写出相关的SQL语句
(遵循SQL92标准,不得使用T-SQL,PL/SQL等扩展语言)。
User_id Interest_Id
1 1
1 2
2 1
2 3
3 1
3 2
4 1
4 2
4 3
5 7
5 8
6 1
7 2
8 1
8 2
9 1
9 3
9 7
9 8
10 9
10 11
*/
if object_id('tbl')is not null
drop table tbl
create table tbl(
[user_id] int,
Interest_Id int
)
insert tbl
select 1,1 union all
select 1,2 union all
select 2,1 union all
select 2,3 union all
select 3,1 union all
select 3,2 union all
select 4,1 union all
select 4,2 union all
select 4,3 union all
select 5,7 union all
select 5,8 union all
select 6,1 union all
select 7,2 union all
select 8,1 union all
select 8,2 union all
select 9,1 union all
select 9,3 union all
select 9,7 union all
select 9,8 union all
select 10,9 union all
select 10,11
drop proc p_proc
select *from tbl
go
create proc p_proc @user_id int
as
declare @count int
create table #t(
num int,
[user_id] int,
Interest_Id int,
[count] int
)
insert #t
select ROW_NUMBER()OVER(partition by a.[user_id] order by getdate()) as num,
a.*,b.[count] from tbl a
inner join (select [user_id],count(*) as [count] from tbl group by [user_id])b
on a.[user_id]=b.[user_id]
select distinct @count=[count] from #t where [user_id]=@user_id
select c.[user_id],count(c.Interest_Id) as InterestNum from
(select distinct [user_id],Interest_Id from #t
where Interest_Id in (select Interest_Id from #t where [user_id]=@user_id) and [count]=@count
and [user_id]<>@user_id)c group by c.[user_id] having count(c.Interest_Id)=@count
exec p_proc
结果:
user_idInterestNum
32
82
这个能行么?不知道是不是巧合
[解决办法]
declare @t table ([User_id] int,Interest_Id int)insert into @tselect 1,1 union all select 1,2 union all select 2,1 union allselect 2,3 union all select 3,1 union all select 3,2 union allselect 4,1 union all select 4,2 union all select 4,3 union allselect 5,7 union all select 5,8 union all select 6,1 union allselect 7,2 union all select 8,1 union all select 8,2 union allselect 9,1 union all select 9,3 union all select 9,7 union allselect 9,8 union all select 10,9 union all select 10,11SELECT b.[User_id] FROM @t a,@t bWHERE a.[User_id] = 1 AND a.Interest_Id = b.Interest_IdGROUP BY b.[User_id]HAVING (COUNT(1) = (SELECT COUNT(1) FROM @t WHERE [User_id] = 1) AND COUNT(1) = (SELECT COUNT(1) FROM @t WHERE [User_id] = b.[User_id]))/*User_id-----------138*/
[解决办法]
DECLARE @T TABLE (User_id INT, Interest_Id INT)INSERT @TSELECT 1, 1 UNION ALLSELECT 1, 2 UNION ALLSELECT 2, 1 UNION ALLSELECT 2, 3 UNION ALLSELECT 3, 1 UNION ALLSELECT 3, 2 UNION ALLSELECT 4, 1 UNION ALLSELECT 4, 2 UNION ALLSELECT 4, 3 UNION ALLSELECT 5, 7 UNION ALLSELECT 5, 8 UNION ALLSELECT 6, 1 UNION ALLSELECT 7, 2 UNION ALLSELECT 8, 1 UNION ALLSELECT 8, 2 UNION ALLSELECT 9, 1 UNION ALLSELECT 9, 3 UNION ALLSELECT 9, 7 UNION ALLSELECT 9, 8 UNION ALLSELECT 10, 9 UNION ALLSELECT 10, 11SELECT * FROM @T T WHERE User_idIN( SELECT User_id FROM @T T1 WHERE Interest_Id IN(SELECT Interest_Id FROM @T WHERE User_id=1) GROUP BY USER_ID )AND (SELECT COUNT(DISTINCT Interest_Id) FROM @T WHERE USER_ID=T.USER_ID ) =(SELECT COUNT(1) FROM @T WHERE User_id=1)/*(所影响的行数为 21 行)User_id Interest_Id ----------- ----------- 1 11 22 12 33 13 28 18 2(所影响的行数为 8 行)
[解决办法]
SELECT * FROM @T T WHERE EXISTS(SELECT 1 FROM @T WHERE Interest_Id=T.Interest_Id AND User_id=1)AND (SELECT COUNT(Interest_Id) FROM @T WHERE USER_ID=T.USER_ID) =(SELECT COUNT(1) FROM @T WHERE User_id=1)