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

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

全局:

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

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

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

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;


. Χ
. 1point3acres

/*


. 1point3acres
+------------+------------+------------+------------+
. Χ


| ops_id | first_name | last_name  | tl_id |


. 1point3acres. Waral dи,
+------------+------------+------------+------------+ ..



|          1 | Tanisha    | Glyndwr      |          2 |
..
. Χ
.google  и
|          2 | Jess       | Suresh       |       NULL |



|          3 | Tracy      | Skuld        |          3 |



|          4 | Alvin      | Uzziah      |          1 |



|          5 | Felix      | Wu           |          2 |



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



5 rows in set (0.00 sec)



*/





SELECT * FROM team_lead LIMIT 5;



/*



+------------+------------+-----------+. 1point3acres.com



| tl_id | first_name | last_name |
. ----


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


..
|          1 | James      | Stjepan   |
.1point3acres


|          2 | Amy        | Lindita     |.google  и



|          3 | Lamar      | Muhsin |
. From 1point 3acres bbs
. ----

|          4 | Anita      | Dareios     |-baidu 1point3acres



|          5 | Jackie     | Black      |

.

+------------+------------+-----------+.--



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


*/

上一篇:SAS adv 1月31日国内在家考 957过
下一篇:一个关于DS platform的想法
推荐
jeremyhu87 2021-2-2 23:57:03 | 只看该作者
全局:
Below is using SQL Server:

  1. SELECT tmp1.tl.id
  2. FROM (
  3.         SELECT ROW_NUMBER() OVER (. 1point 3acres
  4.                         ORDER BY num_operators ASC
  5.                         ) AS row_id
  6.                 ,tmp.tl.id
  7.         FROM (
  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
  13.                 ) AS tmp. check 1point3acres for more.
  14.         ) AS tmp1
  15. WHERE tmp1.row_id <= 3
复制代码
.google  и


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.
回复

使用道具 举报

全局:
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-baidu 1point3acres
  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
回复

使用道具 举报

推荐
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,-baidu 1point3acres
  6.         tl.first_name,
  7.         tl.last_name. From 1point 3acres bbs
  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 (

谢谢回复
回复

使用道具 举报

🔗
 楼主| 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:. ----
[mw_shl_code=sql,true]SEL ...
. From 1point 3acres bbs
谢谢回复。
回复

使用道具 举报

🔗
 楼主| Sunny12 2021-2-5 03:06:55 | 只看该作者
全局:
jeremyhu87 发表于 2021-2-2 23:57. .и
Below is using SQL Server:. From 1point 3acres bbs

[mw_shl_code=sql,true]SELECT tmp1.tl.id

非常感谢
回复

使用道具 举报

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

本版积分规则

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