简体   繁体   中英

SQL Server : how to select just the top row of each group in a query that already has a join?

I am trying to select just the top row for each group in a SQL query:

SELECT DISTINCT 
    CASE 
       WHEN S.ResEndDate = @StartDate 
          THEN 'ASAP' 
          ELSE '' 
    END AS 'ASAP', 
    R.RENTALNUMBER, 
    R.Area, 
    R.PadNum, 
    R.Size, 
    R.Pet, 
    R.Cost
FROM 
    RMS_Rentals R
INNER JOIN 
    RMS_Reservations S ON R.RentalNumber = S.RentalID
WHERE 
    Size = @RentalType  
    AND (RentalNumber NOT IN ((SELECT RentalID 
                               FROM RMS_Reservations  
                               WHERE NOT ((ResStartDate > @EndDate OR  ResEndDate <= @StartDate)))))  
GROUP BY 
    RENTALNUMBER, AREA, PadNum, Size, Pet, Cost, ResStartDate, ResEndDate
ORDER BY 
    RENTALNUMBER, 1 DESC

The query is used to check and see if a specific rental is available to rent given a specific type, reservation start date, and end date. It also has to show ASAP if the given start date is the same as the end date of a different reservation.

The way the query is now, it shows two rows for all the rentals that are ASAP (one where it is not ASAP and one where it is) and only one row for the rentals that are not ASAP.

I would like it to just show 1 row for each rental and whether or not that rental is ASAP.

I am using SQL Server 2008R2

Sample Result Set:

ASAP    RENTALNUMBER    Area      PadNum       Size   Pet

ASAP    CB0001          Cabin Row   1         Cabin    No 
        CB0001          Cabin Row   1         Cabin    No 
ASAP    CB0003          Cabin Row   3         Cabin    No 
        CB0003          Cabin Row   3         Cabin    No 
ASAP    CB0005          Cabin Row   5         Cabin    No 
        CB0005          Cabin Row   5         Cabin    No 
        CB0006          Cabin Row   6         Cabin    No 
ASAP    CB0007          Cabin Row   7         Cabin    No 
        CB0007          Cabin Row   7         Cabin    No 
        CB0008          Cabin Row   8         Cabin    No 
ASAP    CB0009          Cabin Row   9         Cabin    No 
        CB0009          Cabin Row   9         Cabin    No 
        CB0010          Cabin Row   10        Cabin    No 
ASAP    CB0012          Cabin Row   12        Cabin    No 
        CB0012          Cabin Row   12        Cabin    No 
        CB0052          Melody Lane 52        Cabin    NO 
ASAP    CB0053          Melody Lane 53        Cabin    No 
        CB0053          Melody Lane 53        Cabin    No 
ASAP    CB0054          Melody Lane 54        Cabin    No 
        CB0054          Melody Lane 54        Cabin    No 
        CB0055          Melody Lane 55        Cabin    No 
        RV0052          Shady Lane  NULL      Cabin    No 

What I want it to return is just one row for each unique RentalNumber showing whether or not that Rental is a ASAP:

ASAP    RENTALNUMBER    Area      PadNum       Size   Pet

ASAP    CB0001          Cabin Row   1         Cabin    No 
ASAP    CB0003          Cabin Row   3         Cabin    No 
ASAP    CB0005          Cabin Row   5         Cabin    No 
        CB0006          Cabin Row   6         Cabin    No 
ASAP    CB0007          Cabin Row   7         Cabin    No 
        CB0008          Cabin Row   8         Cabin    No 
ASAP    CB0009          Cabin Row   9         Cabin    No 
        CB0010          Cabin Row   10        Cabin    No 
ASAP    CB0012          Cabin Row   12        Cabin    No 
        CB0052          Melody Lane 52        Cabin    NO 
ASAP    CB0053          Melody Lane 53        Cabin    No 
ASAP    CB0054          Melody Lane 54        Cabin    No 
        CB0055          Melody Lane 55        Cabin    No 
        RV0052          Shady Lane  NULL      Cabin    No 

Given your sample data, you can do:

SELECT MAX(ASAP) as ASAP, RENTALNUMBER, Area, PadNum, Size, Pet
FROM t
GROUP BY RENTALNUMBER, Area, PadNum, Size, Pet;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM