mysql,力扣,会员要没了,记下题目

https://leetcode-cn.com/problemset/database/

会员只剩一天了,快点将觉得挺好的的题目保存一波

mysql,力扣,会员要没了,记下题目_第1张图片

select 
    date_format(trans_date, '%Y-%m') month, 
    country,
    sum(state = 'approved') approved_count,
    sum(if(state = 'approved', amount, 0)) approved_amount,
    sum(state = 'chargeback') chargeback_count,
    sum(if(state = 'chargeback', amount, 0)) chargeback_amount
from (
    select * from transactions
    union all
    select id, country, 'chargeback' state, amount, c.trans_date
    from chargebacks c left join transactions t 
    on c.trans_id = t.id
) tmp 
group by month, country
having approved_amount or chargeback_amount

mysql,力扣,会员要没了,记下题目_第2张图片
mysql,力扣,会员要没了,记下题目_第3张图片

select t.Request_at Day,round(count(if(t.Status !='completed',t.Status,null))/count(t.Status) ,2) 'Cancellation Rate' 
from Trips t join  Users u1
on t.Client_Id = u1.Users_Id
join Users u2 on t.Client_Id = u2.Users_Id
where u1.Banned = 'No' and u2.Banned = 'No' and t.Request_at >= '2013-10-01' and t.Request_at <= '2013-10-03'

group by t.Request_at
order by t.Request_at

mysql,力扣,会员要没了,记下题目_第4张图片
mysql,力扣,会员要没了,记下题目_第5张图片

select id,name from accounts 
where id in (
    select distinct a.id
    from
    Logins a join Logins b on  datediff(a.login_date,b.login_date)between 0 and 4
    and a.id = b.id
    group by a.login_date,a.id
    having count(distinct b.login_date)=5)
order by id 

mysql,力扣,会员要没了,记下题目_第6张图片

select  a.job Department , a.namex Employee,a.Salary
from 
    (select e.Id,d.Name job, e.Name namex,e.Salary,e.DepartmentId
        from Employee e join Department d on e.DepartmentId = d.Id ) a
where (a.DepartmentId,a.Salary) in (
        select DepartmentId,max(Salary) 
            from Employee group by DepartmentId) 
        

mysql,力扣,会员要没了,记下题目_第7张图片

with 
tmp as (select a.id id from  stadium a join stadium b on a.id-b.id <=2 and a.id-b.id>=0  and  b.people>=100  group by a.id  having count(a.id)=3 )
select s.* from stadium s 
where s.id in (select id from tmp ) 
or s.id+1 in (select id from tmp ) 
or s.id+2 in (select id from tmp )

mysql,力扣,会员要没了,记下题目_第8张图片
mysql,力扣,会员要没了,记下题目_第9张图片

# Write your MySQL query statement below

select a.id,a.month,sum(b.salary)  Salary 
from 
Employee a left join Employee b on a.id = b.id and a. Month-b. Month <=2 and a. Month - b. Month >=0
and (a.id,a.month) not in (
    select tmp.id,tmp.m from (
        select distinct id,max(Month) over(PARTITION BY Id  ) m from Employee)tmp
)
where b.salary is not null
group by a.id,a.month
order by a.id asc ,a.month desc

SELECT Id, Month, Salary
FROM (SELECT Id, Month, SUM(Salary) OVER (PARTITION BY Id ORDER BY Month ROWS 2 PRECEDING) AS Salary, rank() OVER (PARTITION BY Id ORDER BY Month DESC) AS r
      FROM Employee) t
WHERE r > 1
ORDER BY Id, Month DESC;

mysql,力扣,会员要没了,记下题目_第10张图片


select sum(Frequency) from Numbers order by Number

with 
a as (select *, row_number() over(partition by Company order by Salary, Id) as rn from Employee),
b as (select Company,max(rn) rn from a group by Company)

select a.Id,a.Company,a.Salary 
from a left join b
on a.Company = b.Company
where a.rn in (b.rn/2,b.rn/2+1,(b.rn+1)/2) 
order by company,Salary

mysql,力扣,会员要没了,记下题目_第11张图片
mysql,力扣,会员要没了,记下题目_第12张图片

select t1.d1 as install_dt, count(t1.player_id) as installs, round(count(a2.event_date)/count(t1.player_id), 2) as Day1_retention  
from
(
    select a1.player_id, min(a1.event_date) as d1
    from Activity as a1
    group by a1.player_id
) as t1
left join 
Activity a2
on t1.player_id = a2.player_id and datediff(a2.event_date, t1.d1) = 1
group by t1.d1;

mysql,力扣,会员要没了,记下题目_第13张图片
mysql,力扣,会员要没了,记下题目_第14张图片

# Write your MySQL query statement below
select tm.team_id,tm.team_name ,ifnull(sum(las.summ),0) num_points 
from 
(select tmp.host_team t ,sum(tmp.h) summ
from 
(
    select Matches.host_team ,Matches.guest_team,
    case
    when host_goals>guest_goals  then 3
    when host_goals<guest_goals then 0
    else 1 end h 
   
    from Matches )tmp
    group by tmp.host_team
    union all
    select tmp.guest_team t ,sum(tmp.g) summ
    from 
    (
        select Matches.host_team ,Matches.guest_team,    
        case
        when host_goals<guest_goals  then 3
        when host_goals>guest_goals then 0
        else 1 end g
        from Matches )tmp
        group by tmp.guest_team ) las right join teams tm on las.t=tm.team_id
group by tm.team_id
order by num_points desc ,tm.team_id asc

mysql,力扣,会员要没了,记下题目_第15张图片
mysql,力扣,会员要没了,记下题目_第16张图片

with sc as (
    select avg(s.amount) money, DATE_FORMAT(s.pay_date,'%Y-%m')monthx from salary s group by DATE_FORMAT(s.pay_date,'%Y-%m'))
select DATE_FORMAT(s.pay_date,'%Y-%m') pay_month,e.department_id,
case
when round(avg(s.amount),2) >round((select money from sc where monthx=pay_month),2) then 'higher'
when round(avg(s.amount),2) <round((select money from sc where monthx=pay_month),2) then 'lower'
else 'same' end comparison
from 
 employee e join salary s on s.employee_id = e.employee_id
 group by e.department_id,DATE_FORMAT(s.pay_date,'%Y-%m')
order by pay_month desc

你可能感兴趣的:(mysql)