-1

餐桌时间:

ID  Time in    Time out  Vehicle Type
1   10:30      11:20     bike
2   10:40      12:30     car

表收费:

Id  Vehicle Type  Rate
1   bike          50
2   Car           100

帮助我编写一个查询,通过应用与车辆类型相关的费率,在两个单独的列中返回总持续时间和成本。

4

2 回答 2

0

给你一个SQL FIDDLE 示例

你也可以用同样的方式提问。我认为这可以帮助您收取更多费用,因为多出 1 分钟将被视为多出一小时.....


select A.id, 
       Time_in, 
       Time_out,
       A.Vehicle_type,
       CEILING(DATEDIFF(Minute, Time_in, Time_out)/60.0) as Park_Duration_Hours,
       B.Rate * CEILING(DATEDIFF(Minute, Time_in, Time_out)/60.0) as TotalCharge
from TableTime A
left join TableCharges B
on A.Vehicle_Type = B.Vehicle_Type

于 2013-03-28T07:48:20.583 回答
0

对于持续时间,您可以先转换为[Time in]然后减去[Time out]DateTime

DATEDIFF(hour, cast('2013-01-01 ' + t1.[Time in] as datetime), cast('2013-01-01 ' + t1.[Time out] as datetime) )

最终查询是:

SELECT 
  t1.id, t1.[Time in], t1.[Time out], t1.[Vehicle Type]
, cast('2013-01-01 ' + t1.[Time IN]  as  datetime) as tin
, cast('2013-01-01 ' + t1.[Time out]  as  datetime) as tout
, DATEDIFF(hour,  cast('2013-01-01 ' + t1.[Time in] as datetime), cast('2013-01-01 ' + t1.[Time out] as datetime) ) 
as Duration
, (DATEDIFF(hour,  cast('2013-01-01 ' + t1.[Time in] as datetime), cast('2013-01-01 ' + t1.[Time out] as datetime) ) ) * 
( select rate from t2 where t2.[Vehicle Type] = t1.[Vehicle Type] )
as Charge 

FROM t1

这是 SQL 小提琴

于 2013-03-28T07:35:16.613 回答