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
 Is this thing is possible with table

Author  Topic 

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2012-12-06 : 23:55:22
hello all,

I had a table of 10 record for my users table.Now My problem is i have record in 8th row that record i need to show as first record
like user table

1 Patient Information Patient Information 1 H
2 Patient Demographics Patient Demographics 1 P
3 Languages Languages 1 P
4 Race Race 1 P
5 Ethnicity Ethnicity 1 P
6 City City 1 P
7 State State 1 P
8 Zip Codes Zip Codes 1 P
9 Billing Encounter Billing Encounter 1 H

in that one 8th record i need to move as first record

i wrote query like this

select * from users where userId = 8

union

select * from users where userid != 8..

It shows exact output but how need to 8th record first place permanently

P.V.P.MOhan

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2012-12-07 : 00:09:36
You need to
select * from users ORDER BY <whatever the order is>
with no ORDER BY clause there is no guaranteed order.
You might need to introduce a sequence column so you can order your data as required.
Go to Top of Page

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2012-12-07 : 01:01:18
can u please explain me in query i can't able to find it out

P.V.P.MOhan
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2012-12-07 : 01:59:33
[code]
DECLARE @table TABLE(id int, name varchar(100))
INSERT INTO @table
SELECT 1, 'Patient Information' union all
SELECT 2, 'Patient Demographics' union all
SELECT 3, 'Languages' union all
SELECT 4, 'Race' union all
SELECT 5, 'Ethnicity' union all
SELECT 6, 'City' union all
SELECT 7, 'State' union all
SELECT 8, 'Zip Codes' union all
SELECT 9, 'Billing Encounter'

SELECT *
FROM @table
ORDER BY CASE when id = 8 THEN 1 ELSE 2 END
[/code]

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-12-07 : 08:14:02
quote:
Originally posted by mohan123

hello all,

I had a table of 10 record for my users table.Now My problem is i have record in 8th row that record i need to show as first record
like user table

1 Patient Information Patient Information 1 H
2 Patient Demographics Patient Demographics 1 P
3 Languages Languages 1 P
4 Race Race 1 P
5 Ethnicity Ethnicity 1 P
6 City City 1 P
7 State State 1 P
8 Zip Codes Zip Codes 1 P
9 Billing Encounter Billing Encounter 1 H

in that one 8th record i need to move as first record

i wrote query like this

select * from users where userId = 8

union

select * from users where userid != 8..

It shows exact output but how need to 8th record first place permanently

P.V.P.MOhan


whats the need for permanently doing this change in table? why cant you retrieve the records based you your required order on the fly by means of ORDER BY

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -