【发布时间】:2023-01-16 04:49:35
【问题描述】:
我正在尝试创建一个程序,用户在文本框中输入一个月并按下按钮,向用户显示假期数据库中所有城市的每晚平均价格数据。 例如: 阿姆斯特丹:134.44 并且当开始日期与输入的月份相同并且结束日期不计算用户输入的月份中的第几天时,反之亦然
这是我的程序:
ALTER PROCEDURE sp_AdminAvgPriceMonth
-- Add the parameters for the stored procedure here
@month int
AS
BEGIN
-- Insert statements for procedure here
select case
when month(StartDate)= @month and month(EndDate)=@month
then avg(datediff(day, StartDate, EndDate)*price)
when month(StartDate)=@month and month(EndDate)<>@month
then avg(datediff(day, StartDate, EOMONTH(StartDate))*price)
--month(StartDate)<>@month and month(EndDate)=@month
else avg(datediff(day, DATEADD(month, DATEDIFF(month, 0, StartDate), 0), EndDate)*price)
end as avrgPrice
from VacationsTable VT inner join FlatsTable FT on VT.FlatId=FT.FlatId
group by City
【问题讨论】:
标签: sql-server stored-procedures