问题
如何通过SQL查询获得每个酒店的总费用和每8天转移一次?
详情
8天= 7晚
意思是8天=每个酒店住宿7天
因为最后一天他乘坐的航班没有留在酒店。
结果我需要得到它
我使用以下查询:
var myApp = angular.module("myApp", []);
// Main controller
myApp.controller("appCtrl", ["$scope", "$http", "$q", function ($scope, $http, $q) {
// Get stations data
$http.get("https://rata.digitraffic.fi/api/v1/metadata/stations.json").then(function (response) {
// Load the stations data
$scope.stations = response.data;
// We need to map the following requests into an array of promises so we can resolve them concurrently.
var stationPromises = $scope.stations.map(function (station) {
// Use the "angular way" to define query parameters.
return $http.get("https://rata.digitraffic.fi/api/v1/live-trains", {
params: { station: station.stationShortCode }
}).then(function (response) {
// Assign this set of trains to the station so we don't overwrite other staions trains...
station.trains = response.data;
// Another set of promises to get all of the departure information for each train.
var trainPromises = station.trains.map(function (train) {
// Lets use the "angular way" to define parameters again.
return $http.get("https://rata.digitraffic.fi/api/v1/compositions/" + train.trainNumber, {
params: { departure_date: train.departureDate }
}).then(function (response) {
// Assign this set of compositions to the train so we don't overwrite other compositions for other trains...
train.compositions = response.data;
});
});
return $q.all(trainPromises);
});
});
// Resolve all of the promises.
return $q.all(stationPromises);
}).catch(function (err) {
// Something bad happended...
console.error(err);
});
}]);
我在这个小提琴中找到的样本数据
http://dbfiddle.uk/?rdbms=sqlserver_2016&fiddle=0096a903948a93c1269e931328648be2
更新原始帖子
在下面查询答案1 不能正确计算转移价值 它给我1300转移成本 但是正确的是700
的sampleData
;with cte_HotelPrice
as
(
select
T6.HotelPrice,
T4.HotelID,
T5.HotelName,
T3.DetailsDurationID from package T
inner join StartPackage T1 on T.PackageId=T1.PackageId
inner join packageduration T2 on T.PackageId=T2.PackageId
inner join (SELECT *, RN = ROW_NUMBER() OVER (PARTITION BY PackageDurationsId ORDER BY Days)
FROM DurationDetails) T3 on T2.PackageDurationsId=T3.PackageDurationsID
inner join DayDetails T4 on T3.DetailsDurationID=T4.DetailsDurationID
left join Hotel T5 on T4.HotelID=T5.HotelID
cross apply (select HotelPrice from HotelPrice where HotelID=T4.HotelID and FromDate<=DATEADD(day, T3.RN - 1, T1.StartDate) and ToDate>=DATEADD(day, T3.RN - 1, T1.StartDate)) T6
)
,TransferPrice as
(
select
ttd.Price,
dds.DetailsDurationID
from package p
inner join StartPackage s on p.PackageId=s.PackageId
inner join packageduration pd on p.PackageId=pd.PackageId
inner join (SELECT *, RN = ROW_NUMBER() OVER (PARTITION BY PackageDurationsId ORDER BY Days)
FROM DurationDetails) dd on pd.PackageDurationsId=dd.PackageDurationsID
inner join DayDetails dds on dd.DetailsDurationID=dds.DetailsDurationID
left join TransferType tt on dds.TransferTypeID=tt.TransferID
cross apply (select Price from TransferPeriod where TransferTypeID=dds.TransferTypeID and FromDate<=DATEADD(day, dd.RN - 1, s.StartDate) and Todate>=DATEADD(day, dd.RN - 1, s.StartDate)) ttd
)
select
S4.HotelID,S4.HotelName, S.PackageName, S1.StartDate, S1.EndDate,
sum(S4.HotelPrice) AS cost,
sum(S5.Price) as transfercost
from package S
inner join StartPackage S1 on S.PackageId=S1.PackageId
inner join packageduration S2 on S.PackageId=S2.PackageId
inner join DurationDetails S3 on S2.PackageDurationsId=S3.PackageDurationsID
left join cte_HotelPrice S4 on S3.DetailsDurationID=S4.DetailsDurationID
left join TransferPrice S5 on S3.DetailsDurationID=S5.DetailsDurationID
GROUP BY S4.HotelID, S4.HotelName,S.PackageName, S1.StartDate, S1.EndDate
答案 0 :(得分:0)
这将输出所需的结果,但不确定这是否已优化或符合您的要求。我刚编辑了你的选择陈述,没有费心去检查ctes。
SELECT S4.HotelID ,
S4.HotelName ,
S.PackageName ,
S1.StartDate ,
S1.EndDate ,
SUM(S4.HotelPrice) AS cost ,
TransferCost
FROM package S
INNER JOIN StartPackage S1 ON S.PackageId = S1.PackageId
INNER JOIN packageduration S2 ON S.PackageId = S2.PackageId
INNER JOIN DurationDetails S3 ON S2.PackageDurationsId = S3.PackageDurationsID
LEFT JOIN cte_HotelPrice S4 ON S3.DetailsDurationID = S4.DetailsDurationID
OUTER APPLY ( SELECT SUM(Price) AS TransferCost ,
HotelId
FROM TransferPrice
WHERE S4.HotelID = HotelID
GROUP BY HotelID
) tp
WHERE S4.HotelId IS NOT NULL
GROUP BY S4.HotelID ,
S4.HotelName ,
S.PackageName ,
S1.StartDate ,
S1.EndDate ,
TransferCost;
编辑 - 对于您的新样本数据,您的CTE - TransferPrice返回4条记录,总计为1000条。有没有理由说它不应该是1000?我不熟悉业务需求。
Price DetailsDurationID
200 DD01
200 DD01
300 DD08
300 DD08
更新 - 请参阅新查询;还将HotelID添加到您的CTE,现在我们有您喜欢的转移成本。由于转账是在第8天向Hotel1收费,因此费用列已经更改,这是我想要实现的目标。