查看: 2686| 回复: 9
跳转到指定楼层
上一主题 下一主题
收起左侧

[数学应数] SQL 请帮忙看下

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
Write a query to select the top 3 team leads with the least amount of operators.. 1point 3acres

SELECT * FROM operator LIMIT 5 RIGHT JOIN team_lead LIMIT 5 ON operator LIMIT 5. tl_id= team_lead LIMIT 5. tl_id.
. ----
COUNT(*) OVER (PARTITION BY tl_id ).

ORDER BY DESC
请大家帮忙看看错哪了
----------------------------
SELECT * FROM operator LIMIT 5;. ----
.google  и




/*. .и



+------------+------------+------------+------------+



| ops_id | first_name | last_name  | tl_id |



+------------+------------+------------+------------+



|          1 | Tanisha    | Glyndwr      |          2 |. ----



|          2 | Jess       | Suresh       |       NULL |



|          3 | Tracy      | Skuld        |          3 |. 1point3acres



|          4 | Alvin      | Uzziah      |          1 |



|          5 | Felix      | Wu           |          2 |



+------------+------------+------------+------------+

. 1point 3 acres

5 rows in set (0.00 sec)


.1point3acres
*/


-baidu 1point3acres


SELECT * FROM team_lead LIMIT 5;



/*



+------------+------------+-----------+



| tl_id | first_name | last_name |

. check 1point3acres for more.

+------------+------------+-----------+. 1point 3 acres

. check 1point3acres for more.
.1point3acres
|          1 | James      | Stjepan   |
.
..

|          2 | Amy        | Lindita     |



|          3 | Lamar      | Muhsin |



|          4 | Anita      | Dareios     |


. 1point 3acres
|          5 | Jackie     | Black      |

. 1point3acres

+------------+------------+-----------+



5 rows in set (0.00 sec)
. Waral dи,
. From 1point 3acres bbs

*/

上一篇:SAS adv 1月31日国内在家考 957过
下一篇:一个关于DS platform的想法
🔗
CH3COOH 2021-2-2 10:49:45 | 只看该作者
全局:
Not tested.
  1. select tl_id, first_name, last_name
  2. from ( ..
  3.     select
  4.         count(ops_id) as ops_count,
  5.         tl_id,
  6.         tl.first_name,
  7.         tl.last_name
  8.     from team_lead as tl
  9.     left join operator as ops using(tl_id)
  10.     group by tl_id
  11.     order by ops_count desc. ----
  12.     limit 3
  13. ) as tbl
复制代码
回复

使用道具 举报

🔗
 楼主| Sunny12 2021-2-2 15:04:20 | 只看该作者
全局:
CH3COOH 发表于 2021-2-2 10:49
Not tested.
[mw_shl_code=sql,true]select tl_id, first_name, last_name
from (

谢谢回复
回复

使用道具 举报

全局:
Not sure which SQL language you are using, script below with basic logic:
  1. SELECT *
  2. FROM (
  3.         SELECT tl_id, ROW_NUMBER() OVER(PARTITION BY tl_id ORDER BY count_op) as rank_op
  4.         FROM (
  5.                 SELECT t.tl_id, COUNT(1) as count_op
  6.                 FROM operator o
  7.                 RIGHT JOIN team_lead t
  8.                         ON o.tl_id = t.tl_id.--
  9.                 GROUP BY t.tl_id
  10.                 ) a
  11.         ) b
  12. WHERE rank_op < 4;
复制代码

You could pick one of rank function (RANK, DENSE_RANK, ROW_NUMBER) based on request.
Hope it helps. .и
回复

使用道具 举报

🔗
jeremyhu87 2021-2-2 23:57:03 | 只看该作者
全局:
Below is using SQL Server:

  1. SELECT tmp1.tl.id
  2. FROM (
  3.         SELECT ROW_NUMBER() OVER (. From 1point 3acres bbs
  4.                         ORDER BY num_operators ASC
  5.                         ) AS row_id
  6.                 ,tmp.tl.id. check 1point3acres for more.
  7.         FROM (. check 1point3acres for more.
  8.                 SELECT tl.tl_id
  9.                         ,count(p.operator_id) AS num_operators
  10.                 FROM team_lead tl. Χ
  11.                 LEFT JOIN operators p ON tl.tl_id = p.tl_id
  12.                 GROUP BY tl.tl_id.google  и
  13.                 ) AS tmp
  14.         ) AS tmp1
  15. WHERE tmp1.row_id <= 3
复制代码

.

Two more things you might need to consider:.
1) How to break ties. For example, if there are more than 3 team leads who do not have operators, how to find the top 3?
2) Do not partition by tl_id inside ROW_NUMBER function, or else you will find all row number ids are 1.

Hope this helps.
回复

使用道具 举报

🔗
 楼主| Sunny12 2021-2-5 03:06:46 | 只看该作者
全局:
去加州买保时捷 发表于 2021-2-2 22:39
Not sure which SQL language you are using, script below with basic logic:-baidu 1point3acres
[mw_shl_code=sql,true]SEL ...

谢谢回复。
回复

使用道具 举报

🔗
 楼主| Sunny12 2021-2-5 03:06:55 | 只看该作者
全局:
jeremyhu87 发表于 2021-2-2 23:57
Below is using SQL Server:
-baidu 1point3acres
[mw_shl_code=sql,true]SELECT tmp1.tl.id

非常感谢
回复

使用道具 举报

🔗
momof2girls 2021-5-5 12:53:38 | 只看该作者
全局:
inner join , group by, order by desc limit
回复

使用道具 举报

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

本版积分规则

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