Sql查询将它们组合成1 ..需要帮助

时间:2014-12-13 23:14:33

标签: sql select join subquery correlated-subquery

Select TourDateRange.Detail,                
            (select top 1 TourID from TourDates where TourDateRangeID=TourDateRange.ID)as TourID,
            (select top 1 TourDateRangeID from TourDates where TourDateRangeID=TourDateRange.ID)as TourDateRangeID,
            (select top 1 StartDate from TourDates where TourDateRangeID=TourDateRange.ID)as StartDate,
            (select top 1 StartDate from TourDates where TourDateRangeID=TourDateRange.ID order by ID desc)as EndDate,
            (select top 1 AdultPrice from TourDates where TourDateRangeID=TourDateRange.ID)as AdultPrice,
            (select top 1 ChildPrice from TourDates where TourDateRangeID=TourDateRange.ID)as ChildPrice,
            (select top 1 ID from TourDates where TourDateRangeID=TourDateRange.ID)as TourDatesID
        From TourDateRange
        Where TourDateRange.TourID = @sTourID
        Order By TourDateRange.ID Desc

        Select '' as Detail,@sTourID as TourID,'' as TourDateRangeID,StartDate,EndDate,AdultPrice,ChildPrice,ID as TourDatesID From TourDates where TourID = @sTourID AND TourDateRangeID is Null

帮我解决这个问题我想加入一个单一的查询...意味着要把1个数据表放在后面

1 个答案:

答案 0 :(得分:0)

据我所知,您的查询很清楚,您需要参加结果导览,其中包括" TourDateRange"加上那些没有的。

我希望这会给你所需的结果:

Select 
    ISNULL(TourDateRange.Detail, '') AS Detail,                
    TourDates.TourID,
    ISNULL(TourDates.TourDateRangeID, '') AS TourDateRangeID,
    MIN(TourDates.StartDate) AS MinStartDate,
    MAX(TourDates.EndDate) AS MaxEndDate,
    TourDates.AdultPrice,
    TourDates.ChildPrice,
    MAX(TourDates.ID) as MaxTourDatesID
From 
    TourDates 
    LEFT OUTER JOIN TourDateRange
        ON TourDates.TourDateRangeID = TourDateRange.ID
Where 
    TourDates.TourID = @sTourID
GROUP BY
    ISNULL(TourDateRange.Detail, '') ,                
    TourDates.TourID,
    ISNULL(TourDates.TourDateRangeID, '') ,
    TourDates.AdultPrice,
    TourDates.ChildPrice,
Order by
    TourDateRangeID