Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi!
I use this Query to fetch location:

SQL
SELECT top 6 L_ID, L_Name, 1 as OBCol FROM Locations where L_Name like 'ban%' UNION  SELECT Distinct
top 4 L_ID, L_Name, 2 as OBCol FROM Locations where L_Name like '%ban%'  order by OBCol


And my result shown as:

C#
ID                 Name             OBCol
3	           Bandarban	          1
118	           Bangladesh	          1
3	            Bandarban	          2
18       	    Gaibandha	          2
102	            Albania	              2
118	            Bangladesh	          2



here i don't want to show duplicate value like Bandarban or Bandarban.how can i do it plz help me

What I have tried:

As i know that Union remove duplicay and i already use union but it can't do that.if I remove group by then its working but i need to use group by.
Posted
Updated 3-May-16 23:17pm
Comments
CHill60 4-May-16 5:07am    
You haven't got a group by in your query!
Share some of your data from Locations and explain what you want your query to show - that query you have so far looks far too complicated - i.e. what are the rules for using ban% and %ban%
Member 9361273 4-May-16 5:20am    
ok here i want to fetch those data which have 'ban' pattern and first i want to show those words which start with 'ban' pattern and then want to show those word which have 'pattern' in middle or last
CHill60 4-May-16 6:05am    
Then I think the solution I've posted will work for you - particularly the 2nd option I gave.

1 solution

Probably the simplest way to sort your problem as currently stated is to change the where clause on the 2nd query in the UNION e.g.
SQL
SELECT top 6 L_ID, L_Name, 1 as OBCol 
FROM Locations where L_Name like 'ban%'
UNION  
SELECT top 4 L_ID, L_Name, 2 as OBCol 
FROM Locations where L_Name like '%ban%'  and L_Name NOT LIKE 'ban%'
order by OBCol

Alternatively you could drop the UNION all together and work out the value OBCol using CASE
SQL
SELECT L_ID, L_Name, 
CASE WHEN L_Name like 'ban%' THEN 1 ELSE 2 END as OBCol 
FROM Locations where L_Name like '%ban%'
order by OBCol
 
Share this answer
 
Comments
Karthik_Mahalingam 4-May-16 5:27am    
5

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