Click here to Skip to main content
15,886,056 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I am fetching top 10 records from particular table. In my application if user want some specific record to be fetched so I want output that specific record also but only 10 record should be fetched including that specific.

Eg. suppose I want 2 records which are not in top 10 but that 2 records also come including top 8 so total will be 10.
Posted
Comments
Jibesh 20-Dec-12 6:59am    
is the 2 specific records comes through the sql query result? check your query condition you may have written to include the specific record too. you may use 'NOT IN' or 'NOT EXISTS' clause in your query to discard the specific record inclusion in the normal query.

Try this idea, Hope it helps you.
First load the data into DataTable_1. (Top 10)
Then User details into DataTable_2. (User Required Data)
Now merge the DataTable_1 and DataTable_2 using union.
Then Select Top 10.
Make sure that DataTable_2 records are loaded first.
It is simple.
 
Share this answer
 
Comments
Avinash_Pathak 20-Dec-12 23:29pm    
your idea is working but if i dont want to use union because its performance effective...is that any alternative??
I am not sure why would you want to fetch records that were not requested by user, but here is one approach to your problem
Lets consider a sample table with a list of random names
SQL
DECLARE @Sample TABLE
(
	ID INT IDENTITY(1,1) PRIMARY KEY,
	SampleText VARCHAR(50)
)

INSERT INTO @Sample
SELECT 'abc' UNION ALL
SELECT 'abc1' UNION ALL
SELECT 'abc2' UNION ALL
SELECT 'abc3' UNION ALL
SELECT 'abc4' UNION ALL
SELECT 'xyz' UNION ALL
SELECT 'xyz1' UNION ALL
SELECT 'xyz2' UNION ALL
SELECT 'xyz3' UNION ALL
SELECT 'efg' UNION ALL
SELECT 'pqr' UNION ALL
SELECT 'mno' UNION ALL
SELECT 'efg' UNION ALL
SELECT 'sfr' UNION ALL
SELECT 'qwd' UNION ALL
SELECT 'gte' UNION ALL
SELECT 'sdf' UNION ALL
SELECT 'gtg' UNION ALL
SELECT 'sde' 


Lets assume user requested for records that start with abc. First thing to do is to find the number of records that match this criteria.
SQL
DECLARE @Count INT
SELECT @Count = COUNT(*) FROM @Sample WHERE SampleText LIKE 'abc%'

Next get all the records that match the criteria and if the number of records matching the criteria is less than 10 then fetch other records (which don't match the user's criteria) so total records retrieved is 10.
SQL
SELECT TOP 10 * FROM @Sample WHERE SampleText LIKE 'abc%'
UNION ALL
SELECT TOP (10 - CASE WHEN @Count > 10 THEN 10 ELSE @Count END) *  FROM @Sample WHERE ID NOT IN (SELECT ID FROM @Sample WHERE SampleText LIKE 'abc%')
 
Share this answer
 
Comments
Avinash_Pathak 20-Dec-12 23:30pm    
this is correct but if i dont want to use union...because it is performance effective...is that ant alternative...??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900