2

我需要帮助编写一个存储过程,它需要一个表值参数@Locations,其类型定义如下:

CREATE TYPE [dbo].[tvpLocation] AS TABLE(
    [CountryId] [int] NULL,
    [ResortName] [nvarchar](100) NULL,
    [Ordinal] [int] NOT NULL,
    PRIMARY KEY CLUSTERED 
(
    [Ordinal] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)

@Locations 将包含至少 1 行。每行将有一个非空的 CountryId,并且可能有一个非空的 ResortName。每行都有一个唯一的序数,第一个是 0。@Locations 中 CountryId 和 ResortName 的组合将是唯一的。

sproc 需要搜索以下表结构。

替代文字

可以通过右键单击图像并查看图像来更好地查看图像,或者根据您的浏览器进行类似操作。

现在这就是我卡住的地方,sproc 应该能够找到 Tours 在哪里:

  • Tour 的第一个 TourHotel(Ordinal 0)与第一行 @Locations(Ordinal 0)具有相同的 CountryId(和 ResortName,如果指定)。
  • 此外,如果@Locations 有 > 1 行,则 Tour 必须有额外的 TourHotels,所有这些都必须在这些剩余的 @Locations 行的剩余 CountryIds(和 ResortNames,如果指定)中。

编辑这是我最终使用的代码,基于 Anthony Faull 的建议。非常感谢安东尼:

select distinct T.Id
from tblTour T
join tblTourHotel TH on TH.TourId = T.Id
join tblHotel H ON H.Id = TH.HotelId
JOIN @Locations L ON
    (
        (
            L.Ordinal = 0
            AND TH.Ordinal = 0
        )
        OR
        (
            L.Ordinal > 0
            AND TH.Ordinal > 0
        )
    )
    AND L.CountryId = H.CountryId
    AND
    (
        L.ResortName = H.ResortName
        OR L.ResortName IS NULL
    )
cross apply( select COUNT(TH2.Id) AS [Count] FROM tblTourHotel TH2 where TH2.TourId = TH.TourId ) TourHotelCount
where
TourHotelCount.[Count] = @LocationCount
group by T.Id, T.TourRef, T.Description, T.DepartureDate, T.NumNights, T.DepartureAirportId, T.DestinationAirportId, T.AirlineId, T.FEPrice
having COUNT(distinct TH.Id) = @LocationCount
4

2 回答 2

1
WITH Matches AS
(
    SELECT TH.TourId, TH.Ordinal
    FROM dbo.tblTourHotel TH
    WHERE EXISTS
    (
        SELECT 1
        FROM dbo.tblHotel H
        JOIN @Locations L ON
            (
                (
                    L.Ordinal = 0
                    AND TH.Ordinal = 0
                )
                OR
                (
                    L.Ordinal > 0
                    AND TH.Ordinal > 0
                )
            )
            AND L.CountryId = H.CountryId
            AND
            (
                L.ResortName = H.ResortName
                OR L.ResortName IS NULL
            )
        WHERE H.Id = TH.HotelId
    )
)
SELECT M.TourId
FROM Matches M
WHERE
(
    NOT EXISTS
    (
        SELECT 1
        FROM @Locations L
        WHERE L.Ordinal > 0
    )
    OR NOT EXISTS
    (
        SELECT 1
        FROM dbo.tblTourHotel TH
        WHERE TH.Ordinal > 0
        AND NOT EXISTS
        (
            SELECT 1
            FROM Matches M
            WHERE M.Ordinal = TH.Ordinal
            AND M.TourId = TH.TourId
        )
    )
)
WHERE M.Ordinal = 0
于 2010-12-24T20:35:08.370 回答
0
SELECT TH.TourId
FROM dbo.tblTourHotel TH
JOIN dbo.tblHotel H on H.Id = TH.HotelId
LEFT JOIN @Locations L
    ON SIGN(L.Ordinal) = SIGN(TH.Ordinal) 
    AND L.CountryId = H.CountryId 
    AND (L.ResortName = H.ResortName OR L.ResortName IS NULL)
GROUP BY TH.TourId
HAVING COUNT(H.CountryId) = COUNT(L.CountryId)
于 2010-12-22T14:46:27.687 回答