July 2, 2013 at 3:52 am
I have a customer table
select * from customer
and i need some specific recid member details so i can write a query like this
select * from customer where C_recid in (50200,50194,50430,50191,1)
but the Output of this query ascending order by C_recid
but i wanna output based on (50200,50194,50430,50191,1) this kind only
i got output in union all for individual row
is any other method is possible to get output like this??
Thanks in advance
July 2, 2013 at 4:07 am
Just add an ORDER BY clause:
select *
from customer
where C_recid in (50200,50194,50430,50191,1)
order by C_recid
July 2, 2013 at 4:12 am
I was tried that but the Output will be comes based on ascending order based but i wanna the output come arranged on in() class based.....
July 2, 2013 at 4:25 am
I'm sorry, I didn't understand you wanted the order of the resultset be the same as the sequence order entered in the IN clause.
That requirement is not a standrd function and is harder to implement. If the IN() clause is always the same you could build a CASE to determine the ORDER BY sequence:
select *
from customer
where C_recid in (50200,50194,50430,50191,1)
order by
CASE C_recid
WHEN 50200 THEN 1
WHEN 50194 THEN 2
WHEN 50430 THEN 3
WHEN 50191 THEN 4
WHEN 1 THEN 5
ELSE 6
END
Above CASE statement will change the value of "C_redid" to another value and determine the ORDER BY sequence on this new value. The value of "C_redid" is only changed to determine the ORDER BY and is not visible in the SELECT.
July 2, 2013 at 5:11 am
that C_recId is probably a varchar?
adding a format to the order by might help instead:
select *
from customer
where C_recid in (50200,50194,50430,50191,1)
order by RIGHT('00000' + C_recid ,5) DESC
Lowell
July 2, 2013 at 5:16 am
How is this list (50200,50194,50430,50191,1) generated? Could you leverage the method to create a two-column table instead?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
July 2, 2013 at 5:39 am
techmarimuthu (7/2/2013)but i wanna extra add distinct for C_recid
Do you mean there are more records in the resultset with the same value for "C_redid"? And you want just 5 unique records in your resultset that displays each value in the IN() clause?
Can you post some DDL (table definition) statements and sample data (INSERT statements).
To get an unique resultset you can add a GROUP BY or use SELECT DISTINCT to the query. But it depends on the other columns in your SELECT statement, if this will give you the desired result. We need to know how to handle the data in the other columns.
Like if the data in your tables is like this sample below...
[font="Courier New"]C_redid column_value
50194 'basic'
50194 'advanced'
50194 'expert'[/font]
...and you want only one row in your resultset: wich value of "column_value" you want in your resultset?
July 2, 2013 at 6:01 am
techmarimuthu (7/2/2013)
@ChrisM@Work : this are (50200,50194,50430,50191,1) Record Id
Yes I can see that! How does the list come into existence? Is it typed in by you or generated by a front-end?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
July 3, 2013 at 4:14 am
Code To Simulate OP's Table:
CREATE TABLE customer (C_recid INT);
INSERT INTO customer VALUES (50200),(50194),(50430),(50191),(1);
Here is my solution:
DECLARE @Lookup TABLE (C_recid INT, OrderSeq SMALLINT);
INSERT INTO @Lookup VALUES (50200, 1), (50194, 2), (50430, 3), (50191, 4), (1, 5);
SELECT
c.*
FROM
customer c
INNER JOIN
@Lookup l
ON c.C_recid = l.C_recid
ORDER BY
l.OrderSeq;
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply