Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,




I written the query as below by using that am getting the required results but the Sno column
values are not continuous eg:sno is getting results 1 to 50 for first query and then again it's getting the results 1 to 100 using second query but now i need sno as 1 to 150 results with continuous number
SQL
SELECT ROW_NUMBER() OVER (ORDER BY id desc ) Sno, Id,Title,  from Job as c WITH (NOLOCK)
where  (CountryID=108 and CompanyName='dell')
group by Id, Title, 
union all
SELECT ROW_NUMBER() OVER (ORDER BY id desc ) Sno, Id,Title, from Job as c WITH (NOLOCK)
where  (CountryID!=108 and CompanyName='dell')
group by Id, Title



Please help me how can i get the continuous number
Posted
Updated 13-Jul-20 3:54am
v2

Have a look at sample query:
SQL
SELECT ROW_NUMBER() OVER (ORDER BY ...) AS Sno
FROM (
    SELECT
    ...
    UNION ALL
    SELECT 
    ...
) AS T
ORDER BY Sno



Note: in your example you're trying to get data exactly in this way:
SQL
WHERE (CountryID=108 OR C***ryID!=108) AND CompanyName='dell'


From logical point of view, it's poor idea. Better use query like:
SQL
SELECT ROW_NUMBER() OVER (ORDER BY id desc ) Sno, Id,Title
FROM Job as c WITH (NOLOCK)
WHERE CompanyName='dell'


[EDIT]
In case you want to add custom order, use query like this:
SQL
SELECT ROW_NUMBER() OVER (ORDER BY id desc ) Sno, Id,Title
FROM Job as c WITH (NOLOCK)
WHERE CompanyName='dell'
ORDER BY CASE WHEN CoutryID=108 THEN 0 ELSE CountryID END

[/EDIT]
 
Share this answer
 
v2
Comments
Member 10226004 15-Jul-14 8:31am    
i want to display top results companyname=dell and countryid=108 and then after remaining results should display so now please guide me how can i approach
Maciej Los 15-Jul-14 8:35am    
See updated answer ([EDIT] part).
i should use row_number partition in first select statement not second select statement .
Then how can i retrieve output
 
Share this answer
 
Comments
CHill60 13-Jul-20 10:34am    
If you have a question then use the red "Ask a Question" link at the top of this page. Be sure to read the posting guidelines there.

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