SQL练习一

文章提供了两个SQL查询示例,分别计算2023年3月的投保人数(考虑用户可能购买多个保单)和投保的总金额。查询基于userinsure和insuresum表,通过日期条件筛选3月份的数据,使用了DATE_FORMAT,PERIOD_DIFF,YEAR,MONTH函数来处理日期。
摘要由CSDN通过智能技术生成

SQL练习一

练习一:

需求:

  1. 2023年3月投保的人数(客户可能买多个保单)
  2. 2023年3月客户投保的总金额

userinsure 表

CREATE TABLE userinsure(
  `userid` int NOT NULL COMMENT '用户id',
  `username` varchar(30) NOT NULL COMMENT '用户名',
  `insureid` int NOT NULL COMMENT '保单id',
  `date` datetime NOT NULL COMMENT '创建时间'
);
INSERT INTO `insuresum` (`insureid`, `insuremoney`) VALUES (1001110003, 500000);
INSERT INTO `insuresum` (`insureid`, `insuremoney`) VALUES (1001110004, 400000);
INSERT INTO `insuresum` (`insureid`, `insuremoney`) VALUES (2001110001, 100000);
INSERT INTO `insuresum` (`insureid`, `insuremoney`) VALUES (2011110001, 10000);
INSERT INTO `insuresum` (`insureid`, `insuremoney`) VALUES (2011110002, 30000);
INSERT INTO `insuresum` (`insureid`, `insuremoney`) VALUES (2011110003, 100000);
INSERT INTO `insuresum` (`insureid`, `insuremoney`) VALUES (2011120008, 50000);
INSERT INTO `insuresum` (`insureid`, `insuremoney`) VALUES (2011120009, 99999);

insuresum 表

CREATE TABLE insuresum(
  `insureid` int NOT NULL COMMENT '保单id',
  `insuremoney` double NOT NULL COMMENT '投保金额'
);

INSERT INTO `userinsure` (`userid`, `username`, `insureid`, `date`) VALUES (1, '哇哈哈', 2011110001, '2023-03-04 17:05:12');
INSERT INTO `userinsure` (`userid`, `username`, `insureid`, `date`) VALUES (2, '喜羊羊', 2011110002, '2023-03-04 17:11:21');
INSERT INTO `userinsure` (`userid`, `username`, `insureid`, `date`) VALUES (3, '艾达丝', 2011110003, '2023-03-05 17:12:45');
INSERT INTO `userinsure` (`userid`, `username`, `insureid`, `date`) VALUES (4, '爱丽丝', 2011120009, '2023-04-02 17:13:37');
INSERT INTO `userinsure` (`userid`, `username`, `insureid`, `date`) VALUES (5, '手打', 2011120008, '2023-04-01 17:14:03');
INSERT INTO `userinsure` (`userid`, `username`, `insureid`, `date`) VALUES (6, '喜羊羊', 2001110001, '2023-04-02 17:14:44');
INSERT INTO `userinsure` (`userid`, `username`, `insureid`, `date`) VALUES (7, '哇哈哈', 1001110003, '2023-03-13 17:15:17');
INSERT INTO `userinsure` (`userid`, `username`, `insureid`, `date`) VALUES (8, '哇哈哈', 1001110004, '2023-04-14 17:31:17');
3月份投保的人数(一个客户可能在3月份 参加多个保险)
查询出3月人数,去重
SELECT
	COUNT(
	DISTINCT ( username )) 
FROM
	userinsure 
WHERE
	PERIOD_DIFF(
		date_format( now( ), '%Y%m' ),
	date_format( DATE, '%Y%m' )) = 1;
2023年3月客户投保的总金额
查询出2023年3月的金额 求和,
2023年3月这个日期条件有两种实现方式:
	1.现在时间(现在时间 23年4月)的上一个月
	2.指定具体的年月
SELECT
	SUM(isum.insuremoney) '3月份总金额(元)'
FROM
	userinsure ui
INNER JOIN 
	insuresum isum on ui.insureid = isum.insureid
WHERE
	PERIOD_DIFF(
		date_format( now( ), '%Y%m' ),
	date_format( DATE, '%Y%m' )) = 1;
	-------------------------------------
SELECT
	SUM(isum.insuremoney) '3月份总金额(元)'
FROM
	userinsure ui
INNER JOIN 
	insuresum isum on ui.insureid = isum.insureid
WHERE
	YEAR(DATE)='2023' AND MONTH(DATE)='3';
	---------------------------------------
SELECT
	SUM(isum.insuremoney) '3月份总金额(元)'
FROM
	userinsure ui
INNER JOIN 
	insuresum isum on ui.insureid = isum.insureid
WHERE
	DATE_FORMAT(date,'%Y-%m') = '2023-03';

题主学习记录,大家多多包涵。
在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kboy01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值