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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Top Query

Author  Topic 

visa123
Yak Posting Veteran

54 Posts

Posted - 2011-07-20 : 04:17:00
Hi

In the below query i have to retreive '212012,212713,212749' empid's from table1 first. and then i can retreive anyother empid from table1

i have created the below query but its not retrieving. can anyone help for this?


SELECT TOP 4 * FROM
(



SELECT * FROM table1


UNION

SELECT * FROM table1 WHERE empid IN (SELECT SaparaterID from dbo.fn_SplitCommaSeparatedValues('212012,212713,212749'))



) e




Visa.G

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2011-07-20 : 05:40:38
You must have something to denote order. Tables doesn't have a physical order to rely on, only logical order.
SELECT TOP(4)	*
FROM (
SELECT 1 AS theOrder,
*
FROM table1

UNION ALL

SELECT 2 AS theOrder,
t1.*
FROM table1 AS t1
INNER JOIN dbo.fn_SplitCommaSeparatedValues('212012,212713,212749') AS t2 ON t2.SaparaterID = t1.empid
) AS e
ORDER BY theOrder



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

visa123
Yak Posting Veteran

54 Posts

Posted - 2011-07-20 : 05:48:40


Yes i got it.I did one mistake in this Query


SELECT TOP 4 * FROM
(



SELECT 1 as Sortorder ,* FROM table1


UNION

SELECT 2 as Sortorder,* FROM table1 WHERE empid IN (SELECT SaparaterID from dbo.fn_SplitCommaSeparatedValues('212012,212713,212749'))



) e order by SortOrder



Visa.G
Go to Top of Page

visa123
Yak Posting Veteran

54 Posts

Posted - 2011-07-20 : 05:51:20
Thanks SwePeso.

Visa.G
Go to Top of Page
   

- Advertisement -