Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-04-01 : 15:55:12
|
| Hi,I have a #temp table and I would like to eliminate the top 1% of data from the table based on a column.If the column I need to validate against is called LevelDiff, what filter should I use toe delete these records?for example if I have the temp table populated as below:--Create #Temp tableSELECT InquiryDate, Client, InquiryID, DealerCount, ResponseCount, ExecutedDealer, ExecutedLevel = CPLevel, CoverDealer, CoverLevel, LevelDiff = abs(CPLevel - CoverLevel), TradeYear, TradeMonthName, TradeMonth, DealerTypeDate, QuarterByYearinto #TempFROM #ResponseWHERE CoverLevel is not NULLORDER BY InquiryDate, InquiryID--Before taking the average exclude the top 1% of the trades with --the widest spread between COVER and DONE --LevelDiff = abs(CPLevel - CoverLevel)--select LevelDiff from #Temp select * from #Tempwhere LevelDiff= 1/100*(LevelDiff)Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-01 : 15:59:57
|
| use SELECT TOP 99 PERCENT columns...FROM TableORDER BY yourcol DESC------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-04-01 : 16:28:01
|
| I forgot to add :If I am selecting from the #Temp table that has the column in question, does it make sense to use the query you provided and insert into another temp table say #TempExclude before I calculate the average?SELECT TOP 99 PERCENT columns...FROM #TempINTO#TempExcludeORDER BY yourcol DESC--calculates the averageselect TradeMonthName, TradeYear, ExecuteToCoverDiff = avg(LevelDiff), ResponseCount = avg(convert(float, ResponseCount)), TradeMonth, QuarterByYearINTO #OldDealerfrom #TempWHERE DealerTypeDate = 'OLD'group by TradeYear, QuarterByYear, TradeMonthName, TradeMonth order by TradeMonth, TradeYearThanks |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-04-01 : 16:42:32
|
| I tried doing the following:SELECT TOP 99 PERCENT InquiryDate, Client, InquiryID, DealerCount, ResponseCount, ExecutedDealer, ExecutedLevel, CoverDealer, CoverLevel, LevelDiff TradeYear, TradeMonthName, TradeMonth, DealerTypeDate, QuarterByYearFROM #TempORDER BY LevelDiff DESCI get the below results which appears same as select * from #Temp.The query you suggested should exclude top 1% but the rowcount appears different.select * form #Temp gives 12207 rows while the query you suggested gives 12085 rowsShould I not be seeing the top 1% values for column Level Diff ?Thanks |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-04-01 : 16:59:47
|
| I tried the below which definitely excludes the TOP 1% but I see column name LevelDiff as TradeYear column name.The values are perfect though. I am missing the TradeYear column values .Not sure why. Any idea?SELECT InquiryDate, Client, InquiryID, DealerCount, ResponseCount, ExecutedDealer, ExecutedLevel, CoverDealer, CoverLevel, LevelDiff TradeYear, TradeMonthName, TradeMonth, DealerTypeDate, QuarterByYearFROM #TempWHERE LevelDiff NOT IN(SELECT TOP 1 PERCENT t.LevelDiff FROM #Temp tORDER BY LevelDiff DESC)ORDER BY LevelDiff DESCThanks |
 |
|
|
sqlnovice123
Constraint Violating Yak Guru
262 Posts |
Posted - 2010-04-01 : 17:12:51
|
| Never mind, i was missing a comma. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-02 : 02:05:50
|
| so were you able to sort it out?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|