SQL 力扣 第3天 字符串处理函数/正则

题目一:1667. 修复表中的名字

题目描述:
力扣链接:https://leetcode.cn/problems/fix-names-in-a-table/?envType=study-plan&id=sql-beginner&plan=sql&plan_progress=4sjbxye

在这里插入图片描述

表: Users

±---------------±--------+
| Column Name | Type |
±---------------±--------+
| user_id | int |
| name | varchar |
±---------------±--------+
user_id 是该表的主键。
该表包含用户的 ID 和名字。名字仅由小写和大写字符组成。

编写一个 SQL 查询来修复名字,使得只有第一个字符是大写的,其余都是小写的。

返回按 user_id 排序的结果表。

查询结果格式示例如下。

示例 1:

输入:
Users table:
±--------±------+
| user_id | name |
±--------±------+
| 1 | aLice |
| 2 | bOB |
±--------±------+
输出:
±--------±------+
| user_id | name |
±--------±------+
| 1 | Alice |
| 2 | Bob |
±--------±------+

MS SQL Server代码:

select user_id,(upper(substring([name],1,1))+lower(substring([name],2,len([name])-1)))  [name] 
from Users order by user_id

提交结果:

在这里插入图片描述

题目二:1484. 按日期分组销售产品

力扣链接:https://leetcode.cn/problems/group-sold-products-by-the-date/?envType=study-plan&id=sql-beginner&plan=sql&plan_progress=4sjbxye
题目描述:
在这里插入图片描述

±------------±--------+
| 列名 | 类型 |
±------------±--------+
| sell_date | date |
| product | varchar |
±------------±--------+
此表没有主键,它可能包含重复项。
此表的每一行都包含产品名称和在市场上销售的日期。

编写一个 SQL 查询来查找每个日期、销售的不同产品的数量及其名称。
每个日期的销售产品名称应按词典序排列。
返回按 sell_date 排序的结果表。
查询结果格式如下例所示。

示例 1:

输入:
Activities 表:
±-----------±------------+
| sell_date | product |
±-----------±------------+
| 2020-05-30 | Headphone |
| 2020-06-01 | Pencil |
| 2020-06-02 | Mask |
| 2020-05-30 | Basketball |
| 2020-06-01 | Bible |
| 2020-06-02 | Mask |
| 2020-05-30 | T-Shirt |
±-----------±------------+
输出:
±-----------±---------±-----------------------------+
| sell_date | num_sold | products |
±-----------±---------±-----------------------------+
| 2020-05-30 | 3 | Basketball,Headphone,T-shirt |
| 2020-06-01 | 2 | Bible,Pencil |
| 2020-06-02 | 1 | Mask |
±-----------±---------±-----------------------------+
解释:
对于2020-05-30,出售的物品是 (Headphone, Basketball, T-shirt),按词典序排列,并用逗号 ‘,’ 分隔。
对于2020-06-01,出售的物品是 (Pencil, Bible),按词典序排列,并用逗号分隔。
对于2020-06-02,出售的物品是 (Mask),只需返回该物品名。

MS SQL Server代码:

select sell_date,count(1) num_sold
,stuff((select ',' + product from (select distinct sell_date,product from Activities) b where b.sell_date = a.sell_date for xml path('')),1,1,'') products 
from (select distinct sell_date,product from Activities) a group by sell_date

提交结果:

在这里插入图片描述

题目三:1527. 患某种疾病的患者

力扣链接:https://leetcode.cn/problems/patients-with-a-condition/?envType=study-plan&id=sql-beginner&plan=sql&plan_progress=4sjbxye
题目描述:

在这里插入图片描述

患者信息表: Patients

±-------------±--------+
| Column Name | Type |
±-------------±--------+
| patient_id | int |
| patient_name | varchar |
| conditions | varchar |
±-------------±--------+
patient_id (患者 ID)是该表的主键。
‘conditions’ (疾病)包含 0 个或以上的疾病代码,以空格分隔。
这个表包含医院中患者的信息。

写一条 SQL 语句,查询患有 I 类糖尿病的患者 ID (patient_id)、患者姓名(patient_name)以及其患有的所有疾病代码(conditions)。I 类糖尿病的代码总是包含前缀 DIAB1 。

按 任意顺序 返回结果表。

查询结果格式如下示例所示。

示例 1:

输入:
Patients表:
±-----------±-------------±-------------+
| patient_id | patient_name | conditions |
±-----------±-------------±-------------+
| 1 | Daniel | YFEV COUGH |
| 2 | Alice | |
| 3 | Bob | DIAB100 MYOP |
| 4 | George | ACNE DIAB100 |
| 5 | Alain | DIAB201 |
±-----------±-------------±-------------+
输出:
±-----------±-------------±-------------+
| patient_id | patient_name | conditions |
±-----------±-------------±-------------+
| 3 | Bob | DIAB100 MYOP |
| 4 | George | ACNE DIAB100 |
±-----------±-------------±-------------+
解释:Bob 和 George 都患有代码以 DIAB1 开头的疾病。

MS SQL Server代码:

select patient_id,patient_name,conditions from Patients 
where isnull(conditions,'') like '% DIAB1%' or isnull(conditions,'') like 'DIAB1%'

提交结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值