在SQL Server中,我试图计算特定客户的租车下降和租车取货之间的天数,汽车制造商和日期

时间:2019-11-30 17:46:56

标签: sql sql-server

These are the data tables我尝试了几种不同的select语句,以计算出汽车被挑选到应该放下之间的天数。它应该返回值4,但是,我不断得到1

select  count  (Date_Out) as TotalDays 
from Customer, rentals
where Customer.CID = Rentals.CID
and CName = 'Black'
and Make = 'GM'
and Date_out = '11/01/2009'

当我尝试对Date_out和Date_returned求和时,它给出了一个错误,即您无法对小日期的数据类型求和,我不确定我缺少什么,但是任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

在Sql Server中,可以使用DATEDIFF计算DATE / TIME / DATETIME值之间的间隔。

SELECT DATEDIFF(day, r.Date_Out, r.Date_Returned) as TotalDays 
FROM Customer c
JOIN Rentals r
ON r.CID = c.CID
AND c.CName = 'Black'
AND r.Make = 'GM'
AND CAST(r.Date_out AS DATE) = CAST('2019-11-01' AS DATE)