SQL Server select top 100 by group?

The following code gets me the top 100 tours and reservations

How can I make that into one select so that I get 200 records... 100 or each "group"

SELECT   TOP 100 *
FROM     ewAppointments
WHERE    AppointmentTitle LIKE  'Reservation%'
ORDER BY AppointmentID DESC

SELECT   TOP 100 *
FROM     ewAppointments
WHERE    AppointmentTitle LIKE  'Tour%'
ORDER BY AppointmentID DESC;

Open in new window

Larry Bristersr. DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
For just two groups, easiest way is probably to just UNION ALL the two results:

SELECT *
FROM (
    SELECT   TOP 100 *
    FROM     ewAppointments
    WHERE    AppointmentTitle LIKE  'Reservation%'
    ORDER BY AppointmentID DESC
) AS result1
UNION ALL
SELECT *
FROM (
    SELECT   TOP 100 *
    FROM     ewAppointments
    WHERE    AppointmentTitle LIKE  'Tour%'
    ORDER BY AppointmentID DESC
) AS result2
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Larry Bristersr. DeveloperAuthor Commented:
Hey Scott,
 To get what I wanted the way I wanted to see it I modified yours to what I have below.

Do you see any fundamental issues with this?

SELECT   source.*
FROM     (   SELECT   TOP 100 a.*
             FROM     ewAppointments a WITH (NOLOCK)
			 JOIN SalesSites s WITH (NOLOCK) ON a.SalesSiteID = s.SalesSiteID
             WHERE    AppointmentTitle LIKE 'Reservation%'
                      AND AppointmentDate BETWEEN '2018-01-01' AND '2018-01-31'
					  AND s.BUsinessID = 104302
             ORDER BY AppointmentID DESC
             UNION ALL
             SELECT   TOP 100 a.*
             FROM     ewAppointments a WITH (NOLOCK)
			 JOIN SalesSites s WITH (NOLOCK) ON a.SalesSiteID = s.SalesSiteID
             WHERE    AppointmentTitle LIKE 'Tour%'
                      AND AppointmentDate BETWEEN '2018-01-01' AND '2018-01-31'
					  AND s.BUsinessID = 104302
             ORDER BY AppointmentID DESC ) source
ORDER BY source.AppointmentDate DESC;

Open in new window

0
Scott PletcherSenior DBACommented:
Nah, that should work fine.
0
Larry Bristersr. DeveloperAuthor Commented:
Thanks
0
Scott PletcherSenior DBACommented:
As a side note, I strongly oppose using BETWEEN for dates or times, so I would prefer this instead:

AND AppointmentDate >= '20180101' AND AppointmentDate < '20180201'

If a time-of-day is somehow added to that column later, even if it goes to datetime2 type, the >= with < method continues to work.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.