Click here to Skip to main content
15,886,714 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a table test with a column usrName. table contains 10 different UserName.
now i want to retrieve all UserName but i want first 5 userName on top which will be chosen by me and rest after then

What I have tried:

i want first 5 userName on top which will be chosen by me
Posted
Updated 24-Jul-16 23:23pm
Comments
[no name] 25-Jul-16 3:34am    
You can add an additional field e.g. Position to set the dislplay position and sort your query according to it.
Rajiv.net40 25-Jul-16 4:36am    
can you give some example
[no name] 25-Jul-16 5:24am    
I added solution #3. I hope it helps you.
OriginalGriff 25-Jul-16 3:36am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So what exactly do you mean by "chosen by me" and "the rest"? Perhaps if you share some sample data, and explain how you "chose" which should be in what order it might help?
Use the "Improve question" widget to edit your question and provide better information.
koolprasad2003 25-Jul-16 5:27am    
What you mean by 'chosen by me' ? if you want first 5, just use TOP clause for it
Select Top(5) userid from table1

You can add an aditional column which holds the information TopUser or not.
SQL
CREATE TABLE test
(
   UserName char(30),
   TopMemberPos integer default 1
)

We define by ourself, that '0' means TopMember.

Test data:
SQL
INSERT INTO test (UserName, TopMemberPos) Values
('U1', '1'),
('U2', '0'),
('U3', '1'),
('U4', '0'),
('U5', '1'),
('U6', '0'),
('U7', '1'),
('U8', '0'),
('U9', '0');

Test query:
SQL
SELECT UserName from test order by TopMemberPos, UserName;

Testresult:
UserName
--------
U2                            
U4                            
U6                            
U8                            
U9                            
U1                            
U3                            
U5                            
U7                            

I hope it helps.


[Edit] More intuitive logic/Name/conventions
SQL
CREATE TABLE test
(
   UserName char(30),
   TopMember integer default 0
)

We define by ourself, that '1' means TopMember.

Test data:
SQL
INSERT INTO test (UserName, TopMember) Values
('U1', '0'),
('U2', '1'),
('U3', '0'),
('U4', '1'),
('U5', '0'),
('U6', '1'),
('U7', '0'),
('U8', '1'),
('U9', '1');

Test query:
SQL
SELECT UserName from test order by TopMember DESC, UserName;

Testresult:Same as above
 
Share this answer
 
v2
Comments
Rajiv.net40 25-Jul-16 5:30am    
thank u very much......this is right answar
[no name] 25-Jul-16 5:31am    
You are welcome
Rajiv.net40 25-Jul-16 6:50am    
sir there is problem with this. it shows false on top but i need true on top. how to do ?
[no name] 25-Jul-16 6:55am    
I guessed that a question like this will arrive :)

How you mean that it Shows false on top? Do you convert the integer into boolean? If yes you can use ! Operator...

Or you really define '0' as not TopMember and '1' as TopMember, but in this case you have to adapt then also the "order by" in the query.

Rajiv.net40 25-Jul-16 7:02am    
sir how i use ! Operator give me a example
In various ways this can be done.
1. You can add another column to the table to set the sort order and for rest all you can set a large no. as sort order, let's say 99 for your case.
2. Using CASE WHEN in ORDER BY-
SQL
SELECT UuserName,....
FROM YourTable
ORDER BY CASE WHEN UserName IN('user9','user3'..) THEN 1 ELSE 99 END


There are many other approaches to do this too.

Hope, it helps :)
 
Share this answer
 
try using union[^]

SQL
select usrName from mytable where usrName in ( 'user1','user2','user3','user4','user5')
union
select usrName from mytable where usrName not in ( 'user1','user2','user3','user4','user5')
 
Share this answer
 
Comments
Rajiv.net40 25-Jul-16 4:51am    
if i have 1000 user then ?
Karthik_Mahalingam 25-Jul-16 4:54am    
what is your expected outcome ?
based on that i will reframe the query.
Rajiv.net40 25-Jul-16 4:59am    
i need set for n user
Karthik_Mahalingam 25-Jul-16 5:07am    
then you will have to pass the users and comma separated string and split it in sql and use it

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