12
返回列表 发新帖
楼主: 少女喵
跳转到指定楼层
上一主题 下一主题
收起左侧

Wayfair OA 两道SQL

🔗
ageha8878 2019-11-6 23:43:45 | 只看该作者
全局:
rongy2018 发表于 2019-4-21 05:37. check 1point3acres for more.
赶汽车的那道题, 不知道这么写对不对?
with best_catch as (
        select p.id as passenger_id,  ...

我觉得这是最优解
回复

使用道具 举报

🔗
kittycerry 2020-1-29 00:24:33 | 只看该作者
全局:
duck的题后边没有截图看不到了 :(
回复

使用道具 举报

🔗
kittycerry 2020-1-29 11:51:54 | 只看该作者
全局:
鸭子那道题截图好像题目不完整,看不到需要积分部分,是我漏了什么吗?
回复

使用道具 举报

🔗
cuteflydragon 2020-11-19 02:57:44 | 只看该作者
全局:
bus:
SELECT id, ISNULL(Passengers_on_board,0) as Passengers_on_board FROM buses
LEFT JOIN
        (SELECT Bus_id, COUNT(*) Passengers_on_board
        FROM
                (SELECT Passenger_id, Bus_id
                FROM
                        (SELECT *, RANK()OVER(PARTITION BY Passenger_id ORDER BY Bus_time) Rk_num.1point3acres
                        FROM. 1point 3 acres
                                (SELECT P.id as Passenger_id, P.origin, P.destination, P.time as P_time, B.time as Bus_time, B.id as Bus_id
                                FROM passengers P JOIN buses B. From 1point 3acres bbs
                                ON P.origin=B.origin AND P.destination=B.destination AND B.time>=P.time) a
                        )b
                WHERE Rk_num=1) c
        GROUP BY Bus_id) d
ON buses.id = d.Bus_id
回复

使用道具 举报

🔗
cuteflydragon 2020-11-20 01:51:42 | 只看该作者
全局:
vivianinus 发表于 2019-4-15 07:04
公车:
WITH on_board AS (
    SELECT p.id  AS p_id, MIN(CAST(b.time AS TIME)) AS b_time

Happy Duck:-baidu 1point3acres

SELECT D.pond_id, SUM(CASE WHEN ((temp_preference='+' AND temperature>temp_limit) OR. 1point3acres
                                                                                                  (temp_preference='-' AND temperature<temp_limit) ) THEN 1 ELSE 0
                                                                                                                                                                 END) as Happy_ducks. From 1point 3acres bbs
        FROM ducks D JOIN species S
        ON D.specied_id=S.id
        JOIN ponds P
        ON P.id=D.pond_id
GROUP BY pond_id ..
ORDER BY pond_id
回复

使用道具 举报

🔗
lily58london 2022-3-12 06:45:09 | 只看该作者
全局:
you are welcome
drop table buses, passengers;
create table buses (bus_id int primary key, origin varchar not null, destination varchar not null, time varchar not null, unique(origin, destination, time));                               
INSERT INTO buses (bus_id, origin, destination, time) VALUES
(10, 'W', 'B', '10:55'),
(20, 'B', 'P', '06:20'),
(21, 'B', 'P', '14:00'),
(22, 'B', 'P', '21:40'),
(30, 'P', 'M', '13:30');
. ----
create table passengers (pass_id int primary key, origin varchar not null, destination varchar not null, time varchar not null);                               
INSERT INTO passengers (pass_id, origin, destination, time) VALUES. .и
(1, 'P', 'M', '13:30'),
(2, 'P', 'M', '13:31'),
(10, 'W', 'P', '10:00'),
(11, 'W', 'B', '22:31'),
(40, 'B', 'P', '06:15'),
(41, 'B', 'P', '06:50'),
(42, 'B', 'P', '07:12'),
(43, 'B', 'P', '12:03'),
(44, 'B', 'P', '20:00');.1point3acres
select b.id, count(p.id) from buses b join passengers p on p.origin = b.origin and p.destination = b.destination and p.time <= b.time group by b.id order by b.id

-- 对每一个passager来说,最小的bus arrival time 就是他要乘的
with tmp as (
select p.pass_id, min(b.time) as boarding_time
from buses b join passengers p on p.origin = b.origin and p.destination = b.destination and p.time <= b.time
group by p.pass_id
order by p.pass_id)

select b.bus_id as id, count(pass_id)
from buses b
left join tmp
on b.time = tmp.boarding_time
group by id
order by id
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表