SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


ORDER BY Should be same as my input in IN()


ORDER BY Should be same as my input in IN()

Author
Message
lokesha.b
lokesha.b
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 32
DECLARE @T TABLE(ID INT,NAME VARCHAR(10))

INSERT INTO @T VALUES(1,'PPP')
INSERT INTO @T VALUES(2,'AAA')
INSERT INTO @T VALUES(2,'ZZZ')
INSERT INTO @T VALUES(3,'XYZ')
INSERT INTO @T VALUES(4,'PQR')
INSERT INTO @T VALUES(5,'ZAB')

SELECT * FROM @T WHERE NAME IN('XYZ','AAA','PQR')

ID NAME
----------
2 AAA
3 XYZ
4 PQR

--But I need the result as below (i.e, the order should be same as how I had input the value in the IN() )

ID NAME
-----------
3 XYZ
2 AAA
4 PQR

-- CAN YOU PLEASE GIVE ME A SINGLE QUERY WITHOUT DOING ANY FUNCTIONS OR TEMP TABLES
nigelrivett
nigelrivett
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1056 Visits: 211
Maybe dynamic sql for the order by

A cte with union all for each value might do it.


Cursors never.
DTS - only when needed and never to control.

nigelrivett
nigelrivett
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1056 Visits: 211
;with cte as
(
select val = 'XYZ', seq = 1
union all
select val = 'AAA', seq = 1
union all
select val = 'PQR', seq = 1
)
SELECT t.*
FROM @T t
join cte
on t.Name = cte.val
order by cte.seq

If you put the string into a variable you could use the cte to parse it into a table.


Cursors never.
DTS - only when needed and never to control.

Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12074 Visits: 5478
You cannot force the order as per list in IN.
You should consider re-factoring your query.
One of the ways:


SELECT t.*
FROM @T t
JOIN (VALUES (1,'XYZ'),(2,'AAA'),(3,'PQR')) lst(rn,name)
ON lst.name= t.name
ORDER by lst.rn



_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
GSquared
GSquared
SSC Guru
SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)SSC Guru (56K reputation)

Group: General Forum Members
Points: 56273 Visits: 9730
Is the IN list an input parameter of some sort, or is it hard-coded into the query? You weren't clear on that, but it's the deciding factor on how you do something like this.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
carlosaamaral
carlosaamaral
SSC Journeyman
SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)SSC Journeyman (83 reputation)

Group: General Forum Members
Points: 83 Visits: 1049
Logic ???? w00t

DECLARE @T TABLE(ID INT,NAME VARCHAR(10))

INSERT INTO @T VALUES(1,'PPP')
INSERT INTO @T VALUES(2,'AAA')
INSERT INTO @T VALUES(2,'ZZZ')
INSERT INTO @T VALUES(3,'XYZ')
INSERT INTO @T VALUES(4,'PQR')
INSERT INTO @T VALUES(5,'ZAB')

DECLARE @T_INPUT TABLE(ID int IDENTITY(1,1) , NAME VARCHAR(10))

INSERT INTO @T_INPUT VALUES('XYZ')
INSERT INTO @T_INPUT VALUES('AAA')
INSERT INTO @T_INPUT VALUES('PQR')


/* SELECT * FROM @T WHERE NAME IN('XYZ','AAA','PQR') */


SELECT AA.*
FROM @T AA,
@T_INPUT BB
WHERE AA.NAME=BB.NAME
ORDER BY BB.ID




Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12074 Visits: 5478



....
SELECT AA.*
FROM @T AA,
@T_INPUT BB
WHERE AA.NAME=BB.NAME
ORDER BY BB.ID







Here I will play J.CELKO: Mate, you should really learn how to use ANSI standard JOINS...

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Phil Parkin
Phil Parkin
SSC Guru
SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)SSC Guru (50K reputation)

Group: General Forum Members
Points: 50289 Visits: 21151
Eugene Elutin (12/11/2012)


Here I will play J.CELKO: ......


Steady on, don't overdo it Hehe


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
Eugene Elutin
Eugene Elutin
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12074 Visits: 5478
Phil Parkin (12/11/2012)
Eugene Elutin (12/11/2012)


Here I will play J.CELKO: ......


Steady on, don't overdo it Hehe


I hope I didn't, as I have not complained about lack of PK and didn't state ISO numbers and didn't use "punch card" preamble... Hehe

_____________________________________________
"The only true wisdom is in knowing you know nothing"
"O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search