SQL 时间对比

本文介绍了一种用于比较房产交易中完税时间、产权证时间和交易时间的方法。通过复杂的SQL语句,确定这些时间是否在两年内,以符合特定条件。

描述:
数据库表中包含完税时间、产权证时间、交易时间等三个时间,首先将完税时间与产权证时间进行对比,选出较早的一个时间,之后将改时间与交易时间进行对比,看是否在2年之内。

数据表截图:
在这里插入图片描述

主要思路:
分别取完税时间、产权证时间与交易时间的年份做差,并比较两个差值的大小,取较大的差值,并将较大的差值与±2进行比较:
(1) 差值=2时:判断月时间差值:
① 月时间差=0时:判断日时间差:
a.日时间差 >0时:显示‘否’;
b.日时间差 <=0时:显示‘是’;
② 月时间差<0时:显示‘是’;
③ 月时间差>0时:显示‘否’;
(2) 差值=-2时:判断月时间差值:
① 月时间差=0时:判断日时间差:
a.日时间差 <0时:显示‘否’;
b.日时间差 >=0时:显示‘是’;
② 月时间差 <0时:显示‘是’;
③ 月时间差 >0时:显示‘否’;
(3) 差值在-2~2之间时:显示‘是’;
(4) 差值>2 或 <-2时:显示‘否’;


SELECT [Id]
      ,[TitleCertificateNum]
      ,[TaxPaymentProveDateTime]
	   ,(select CONVERT(varchar(100),TaxPaymentProveDateTime,23)) as 完税证明时间
      ,[TitleCertificateDateTime]
	  ,(select CONVERT(varchar(100),TitleCertificateDateTime,23))as 产权证时间
      ,[TransactionDateTime]
	  ,(select CONVERT(varchar(100),TransactionDateTime,23))as 交易日期
	  ,(select CONVERT(varchar(100),(select (case
					when [TaxPaymentProveDateTime]<[TitleCertificateDateTime]
					then [TaxPaymentProveDateTime]
					else [TitleCertificateDateTime]
					end
				  )
		 ),23))as 证明时间短
	   ,(select
			(
				case 
				--年时间差为2时
				when 
					(
						(select 
								(
									case
									when ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime)))) > ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
									then ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime))))
									else ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
									end
								)
						 )
					) = 2
					then 
						( 
							case 
							--月时间差为0时
							when 
								(
									(select 
										(
											case
											when ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime)))) > ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
											then ( Max(convert(int,DateName(MONTH,TransactionDateTime)))- Max(convert(int,DateName(MONTH,TaxPaymentProveDateTime))))
											else ( Max(convert(int,DateName(MONTH,TransactionDateTime)))- Max(convert(int,DateName(MONTH,[TitleCertificateDateTime]))))
											end
										)
									)
								) = 0
							then	
								(
									case 
									--日时间差大于0时
									when 
										(
											(select 
												(
													case
													when ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime)))) > ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
													then ( Max(convert(int,DateName(DAY,TransactionDateTime)))- Max(convert(int,DateName(DAY,TaxPaymentProveDateTime))))
													else ( Max(convert(int,DateName(DAY,TransactionDateTime)))- Max(convert(int,DateName(DAY,[TitleCertificateDateTime]))))
													end
												)
											)
										) > 0
									then	'否'
									else	'是'
									end
								)
							--月时间差小于0时
							when 
								(
									(select 
										(
											case
											when ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime)))) > ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
											then ( Max(convert(int,DateName(MONTH,TransactionDateTime)))- Max(convert(int,DateName(MONTH,TaxPaymentProveDateTime))))
											else ( Max(convert(int,DateName(MONTH,TransactionDateTime)))- Max(convert(int,DateName(MONTH,[TitleCertificateDateTime]))))
											end
										)
									)
								) < 0
							then	'是'
							else	'否'
							end
						)
					--年时间差为-2时
					when 
					(
						(select 
								(
									case
									when ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime)))) > ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
									then ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime))))
									else ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
									end
								)
						 )
					) = -2
					then 
						( 
							case 
							--月时间差为0时
							when 
								(
									(select 
										(
											case
											when ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime)))) > ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
											then ( Max(convert(int,DateName(MONTH,TransactionDateTime)))- Max(convert(int,DateName(MONTH,TaxPaymentProveDateTime))))
											else ( Max(convert(int,DateName(MONTH,TransactionDateTime)))- Max(convert(int,DateName(MONTH,[TitleCertificateDateTime]))))
											end
										)
									)
								) = 0
							then	
								(
									case 
									--日时间差不为0时
									when 
										(
											(select 
												(
													case
													when ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime)))) > ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
													then ( Max(convert(int,DateName(DAY,TransactionDateTime)))- Max(convert(int,DateName(DAY,TaxPaymentProveDateTime))))
													else ( Max(convert(int,DateName(DAY,TransactionDateTime)))- Max(convert(int,DateName(DAY,[TitleCertificateDateTime]))))
													end
												)
											)
										) < 0
									then	'否'
									else	'是'
									end
								)
							--月时间差小于0时
							when 
								(
									(select 
										(
											case
											when ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime)))) > ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
											then ( Max(convert(int,DateName(MONTH,TransactionDateTime)))- Max(convert(int,DateName(MONTH,TaxPaymentProveDateTime))))
											else ( Max(convert(int,DateName(MONTH,TransactionDateTime)))- Max(convert(int,DateName(MONTH,[TitleCertificateDateTime]))))
											end
										)
									)
								) < 0
							then	'是'
							else	'否'
							end
						)
					--年时间差在-2~2之间时
					when 
					(
						(select 
								(
									case
									when ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime)))) > ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
									then ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,TaxPaymentProveDateTime))))
									else ( Max(convert(int,DateName(YEAR,TransactionDateTime)))- Max(convert(int,DateName(YEAR,[TitleCertificateDateTime]))))
									end
								)
						 )
					) between -2 and 2
					--then '是'
					--年时间差为-2时
					
					then '是'
					else '否'
					end
			)
		) as 是否2年内
  FROM [TimeComparison]
  group by Id,[TitleCertificateNum],[TaxPaymentProveDateTime],[TitleCertificateDateTime],[TransactionDateTime]

查询结果:
在这里插入图片描述

SQL 中进行时间对比有多种方法,以下是一些常见的场景及对应的实现方式: ### 比较两个时间字段的大小 在查询中,可以直接使用比较运算符(如 `>`、`<`、`>=`、`<=`、`=`)来对比两个时间字段。假设存在一个 `orders` 表,包含 `order_time` 和 `delivery_time` 两个时间字段,现在要找出订单时间早于配送时间的记录: ```sql SELECT * FROM orders WHERE order_time < delivery_time; ``` ### 与特定时间对比 可以将时间字段与一个特定的时间点进行对比。例如,在 `orders` 表中,找出 2024 年 1 月 1 日之后下单的订单: ```sql SELECT * FROM orders WHERE order_time > '2024-01-01 00:00:00'; ``` ### 使用 DATEDIFF 函数计算时间差 `DATEDIFF` 函数可用于计算两个时间之间的差值。例如,计算 `orders` 表中每个订单的下单时间和配送时间之间相差的天数: ```sql SELECT order_id, DATEDIFF(DAY, order_time, delivery_time) AS days_difference FROM orders; ``` ### 使用 TIMESTAMPDIFF 函数(以 MySQL 为例) `TIMESTAMPDIFF` 函数可以更灵活地计算时间差,能指定不同的时间单位。例如,计算 `orders` 表中每个订单的下单时间和配送时间之间相差的小时数: ```sql SELECT order_id, TIMESTAMPDIFF(HOUR, order_time, delivery_time) AS hours_difference FROM orders; ``` ### 得出 SQL 语句执行时间 若要得出 SQL 语句的执行时间,可在 `select` 语句前加 `declare @d datetime; set @d = getdate();`,并在 `select` 语句后加 `select [语句执行花费时间(毫秒)] = datediff(ms, @d, getdate())` [^1]。示例如下: ```sql declare @d datetime; set @d = getdate(); SELECT * FROM orders WHERE order_time > '2024-01-01 00:00:00'; select [语句执行花费时间(毫秒)] = datediff(ms, @d, getdate()); ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值