I have the following code and I would like to remove the top 5% of the records for the Triage_To_Dr_Sees_Adjust
numeric variable. Is this possible(I know using Select TOP(95) Percent
would give me the top 95% but I want reverse of that, I would like the bottom 95%)?. Thank you!
UPDATE: I included code from an answer below, now my issue is that this code produces no records.
use EMTCQIData
DECLARE @StartDate Date
DECLARE @EndDate Date
Set @StartDate = '05/01/2017'
Set @EndDate = '12/31/2018'
SELECT sub.*
FROM (
Select *,
Format([CHECKIN_DATE_TIME],'dddd') AS Checkin_Day,
CONVERT(VARCHAR(10),[CHECKIN_DATE_TIME],111) as Checkin_date,
CONVERT(VARCHAR(20),[CHECKIN_DATE_TIME],108) as Checkin_Time,
Case when TRIAGE_TO_DR_SEES < 0 then 0 else TRIAGE_TO_DR_SEES end as
TRIAGE_TO_DR_SEES_ADJUST
FROM ED_TAT_MASTER
WHERE
(CHECKIN_DATE_TIME > @StartDate and CHECKIN_DATE_TIME < @EndDate)
AND (TRIAGE_DATE_TIME not like '')
AND (TRIAGE_TO_DR_SEES not like '%no%')
AND (TRACK_GROUP='ED Tracking Group')
AND (FORMAT([CHECKIN_DATE_TIME], 'dddd') = 'Sunday')
AND CHECKIN_DATE_TIME not like '%2017-07-02%'
AND CHECKIN_DATE_TIME not like '%2017-12-24%'
AND CHECKIN_DATE_TIME not like '%2018-06-10%'
) sub
WHERE sub.TRIAGE_TO_DR_SEES not in (select top(5) Percent sub.TRIAGE_TO_DR_SEES
FROM ED_TAT_MASTER
ORDER BY CHECKIN_DATE_TIME)
TOP (5) PERCENT
and switch the direction of yourORDER BY
? The "last" 5 percent of records in one direction is the first in the other.IN
could be expensive (you'll need to do 2 scans of the table), and this seems harder to read.SELECT * FROM (SELECT TOP (95) PERCENT * FROM [Table] ORDER BY ID DESC) ORDER BY ID ASC;
I don't mean to be rude, but I'm not sure what part of that is difficult to understand there.WITH
. That was just me generating sample data. I'll update the query a bit for you.