sql--來自各路面筋

1.pdd

CREATE TABLE pdd(
u_id        VARCHAR(20)   NOT NULL,
ip          INT           NOT NULL,
timestamp   datetime      NOT NULL
);
insert into pdd values('a',124,'2019-08-07 12:09:02');
insert into pdd values('b',124,'2019-08-07 12:09:02');
insert into pdd values('c',124,'2019-08-07 12:09:02');
insert into pdd values('a',174,'2019-08-07 12:09:02');
insert into pdd values('b',174,'2019-08-07 12:09:02');
insert into pdd values('a',194,'2019-08-07 12:09:02');
insert into pdd values('b',194,'2019-08-07 12:09:02');
-- 使用共同使用過的ip數量大於等於3個的用戶名單
-- ab共同使用過124,174,194;輸出uid_1;uid_2(a,b)
-- 一個人開了很多小號,用ip登陸次數識別出來

一開始沒有想起自連接,不知道要groupby哪個,其實兩個表自連接之後,變成ab,ac,ba,bc,ca,cb;cb,ba;ab;ba所以對兩個id進行聚合然後過濾就可以 ;這也是自連接的一種運用方式,注意體會

select 
	a.u_id
    ,b.u_id
    ,count(*)
from pdd as a
join pdd as b
on a.ip = b.ip
where a.u_id <> b.u_id
group by a.u_id, b.u_id ;

select 
	distinct
	a.u_id
	,b.u_id
from pdd as a
join pdd as b
on a.ip = b.ip
where a.u_id <> b.u_id
group by a.u_id, b.u_id 
having count(*)>=3

2.就是一張表裏有 mall_id, gmv

現在把這個總的gmv分均分成10個層級,每個層級的GMV由多少家店鋪構成,輸出的表字段是 layer(layer1,layer2....),  count(前10%GMV由多少家店鋪貢獻,10-20%由多少家貢獻)

作者:跪求pdd給offer
鏈接:https://www.nowcoder.com/discuss/227871
來源:牛客網

select
    concat('layer', c.rk) as layer,
    count(c.rk) as count
from
    (select
        b.mall_id,
        case 
            when b.t2/b.t1 >= 0 and b.t2/b.t1 <= 0.1 then '1'
            when b.t2/b.t1 > 0.1 and b.t2/b.t1 <= 0.2 then '2'
            when b.t2/b.t1 > 0.2 and b.t2/b.t1 <= 0.3 then '3'
            when b.t2/b.t1 > 0.3 and b.t2/b.t1 <= 0.4 then '4'
            when b.t2/b.t1 > 0.4 and b.t2/b.t1 <= 0.5 then '5'
            when b.t2/b.t1 > 0.5 and b.t2/b.t1 <= 0.6 then '6'
            when b.t2/b.t1 > 0.6 and b.t2/b.t1 <= 0.7 then '7'
            when b.t2/b.t1 > 0.7 and b.t2/b.t1 <= 0.8 then '8'
            when b.t2/b.t1 > 0.8 and b.t2/b.t1 <= 0.9 then '9'
            else '10'
        end as rk
    from
        (select
            a.mall_id,
            sum(a.total_gmv) as t1,
            sum(a.total_gmv) over(order by a.total_gmv desc) as t2
        from
            (select
                mall_id,
                sum(gmv) as total_gmv
            from mall_gmv
            group by mall_id) a) b) c
group by c.rk
order by layer;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章