拼多多筆試題


據說是拼多多的面試題,剛幫人解決的。。。 題目一:

今天幫人看的sql題目,要求見上圖:

;with tb_info(CLASS,TEACHER,COURSE) as (

	select '1','A','MATH'

	UNION ALL

	select '1','A','SPORTS'

	UNION ALL

	SELECT '1','B','LANGUAGE'

	UNION ALL

	SELECT '2','A','MATH'

	UNION ALL

	SELECT '3','B','MATH'

	UNION ALL

	SELECT '4','B','LANGUAGE'

	UNION ALL

	SELECT '5','A','SPORTS'

	UNION ALL

	SELECT '6','B','LANGUAGE'

	UNION ALL

	SELECT '6','C','MATH'

)

select t.teacher

       ,t.classNum

       ,count(distinct i.course) as courseNum

       from tb_info i join (

							select teacher,count(*) as classNum from (

												select  teacher,class 

															from tb_info 

																	group by teacher,class

																			) temp 

																			  group by teacher having count(class) > 3

																			) t 

							on i.teacher = t.teacher

							group by t.teacher,t.classNum

							

題目二 求中位數:

declare @freq_num int

;with tb_record(num,freq) as(

	select 1,9

	union ALL

	select 2,2

	union ALL

	select 4,3

	union ALL

	select 6,1

	union ALL

	select 7,2

	union ALL

	select 9,1

)

, temp as (

select num,freq

      ,(select sum(freq)  from tb_record sub where sub.num<=t.num) as ord_num 

      from tb_record t 

)

select  @freq_num = sum(freq) from tb_record

declare @middle_digit int

declare @sql_cmd varchar(max)

set @middle_digit = @freq_num /2

if @freq_num %2 =0

BEGIN

set @sql_cmd = '

;with tb_record(num,freq) as(

	select 1,9

	union ALL

	select 2,2

	union ALL

	select 4,3

	union ALL

	select 6,1

	union ALL

	select 7,2

	union ALL

	select 9,1

)

, temp as (

select num,freq

      ,(select sum(freq)  from tb_record sub where sub.num<=t.num) as ord_num 

      from tb_record t 

)

select sum(num)*1.0/2 from temp where temp.ord_num between '+cast(@middle_digit as varchar)+' and (select top 1 ord_num from temp t where t.ord_num>'+cast(@middle_digit as varchar)+')'

print @sql_cmd

exec(@sql_cmd)

END

;with tb_record(num,freq) as(
	select 1,9
	union ALL
	select 2,2
	union ALL
	select 4,3
	union ALL
	select 6,1
	union ALL
	select 7,2
	union ALL
	select 9,1
)
, temp as (
select num,freq
      ,(select sum(freq)  from tb_record sub where sub.num<=t.num) as ord_num 
      from tb_record t 
)
select sum(num)*1.0/2 from temp 
                      where ord_num between (select max(ord_num)/2 from temp) 
                                    and (select min(ord_num) from temp where ord_num>(select max(ord_num)/2 from temp))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM