使用表值参数进行查询

发布于 2024-10-08 17:30:51 字数 1728 浏览 6 评论 0原文

我需要编写存储过程的帮助,它需要一个表值参数@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 组合将是唯一的。

存储过程需要根据以下表结构进行搜索。

alt text

右键单击​​图像并查看图像或类似操作(具体取决于您的浏览器)可以更好地查看图像。

现在这就是我陷入困境的地方,存储过程应该能够在其中找到 Tours:

  • The Tour 的第 1 个 TourHotel(序号 0) 具有相同的 CountryId(和 ResortName 如果指定)的第一行 @Locations(序数 0)。
  • 并且如果 @Locations 有 > 1 行, 旅游必须有额外的 TourHotels,所有这些都必须位于 这些剩余 @Locations 行的剩余 CountryId(以及 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

I need help please with writing a sproc, it takes a table-valued parameter @Locations, whose Type is defined as follows:

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 will contain at least 1 row. Each row WILL have a non-null CountryId, and MAY have a non-null ResortName.
Each row will have a unique Ordinal, the first being 0. The combinations of CountryId and ResortName in @Locations will be unique.

The sproc needs to search against the following table structure.

alt text

The image can be seen better by right-clicking it and View Image, or similar depending on your browser.

Now this is where I'm stuck, the sproc should be able to find Tours where:

  • The Tour's 1st TourHotel (Ordinal 0)
    has the same CountryId (and ResortName
    if specified) of the 1st row of
    @Locations (Ordinal 0).
  • And also if @Locations has > 1 row,
    the Tour must have additional
    TourHotels, ALL of which must be in the
    remaining CountryIds (and ResortNames if specified) of these remaining @Locations rows.

Edit This is the code I finally used, based on Anthony Faull's suggestion. Thank you so much Anthony:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

始于初秋 2024-10-15 17:30:51
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
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
转身泪倾城 2024-10-15 17:30:51
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)
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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文