May 8, 2010 at 1:40 am
Hello,
I have a Data Table 'InvoiceDetails' which contains Invoice and its Details.
Below is my Query,
SELECT *
FROM InvoiceDetails
WHERE InvoiceNo IN ('1248','1063','963','1524')
Can anyone pls tell me how to the data in the Below order
---------------------------------------------------------
InvoiceNo | InvoiceValue | StockCode |
---------------------------------------------------------
1248 | 2000 | AAAAA |
---------------------------------------------------------
1063 | 5000 | BBBBB |
--------------------------------------------------------
963 | 1000 | CCCCC |
--------------------------------------------------------
1524 | 8000 | DDDDD |
---------------------------------------------------------
i.e., the result should be in the same order as mentioned inside " IN Clause"
Regards and Thanks,
Kavya
May 8, 2010 at 4:55 am
You would nee to provide an ORDER BY clause based on a column that will hold any order criteria.
Since SQL Server does not perform a row-by-row processing, it will select all matching rows and, without a given ORDER BY clause, return the result more or less random (it might be orderd by the clustered index, but there's no guarantee either...)
May 8, 2010 at 8:00 pm
lmu92 (5/8/2010)
You would nee to provide an ORDER BY clause based on a column that will hold any order criteria.Since SQL Server does not perform a row-by-row processing, it will select all matching rows and, without a given ORDER BY clause, return the result more or less random (it might be orderd by the clustered index, but there's no guarantee either...)
I'm thinking he wants it in the same order list in the IN.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 8, 2010 at 8:02 pm
c_kavya_shri (5/8/2010)
Hello,I have a Data Table 'InvoiceDetails' which contains Invoice and its Details.
Below is my Query,
SELECT *
FROM InvoiceDetails
WHERE InvoiceNo IN ('1248','1063','963','1524')
Can anyone pls tell me how to the data in the Below order
---------------------------------------------------------
InvoiceNo | InvoiceValue | StockCode |
---------------------------------------------------------
1248 | 2000 | AAAAA |
---------------------------------------------------------
1063 | 5000 | BBBBB |
--------------------------------------------------------
963 | 1000 | CCCCC |
--------------------------------------------------------
1524 | 8000 | DDDDD |
---------------------------------------------------------
i.e., the result should be in the same order as mentioned inside " IN Clause"
Regards and Thanks,
Kavya
This is important to figuring out how to do what you ask. Where does the data from the IN clause come from? Is it a passed in parameter, hard coded as you have it, embedded SQL in a GUI, or something else? Don't be bashful with the details here... there are answers for each method but I have to be sure as to where the data in the IN clause is coming from?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 9, 2010 at 8:46 am
If it is hard coded, I'm thinking as below.
SELECT *
FROM InvoiceDetails
WHERE InvoiceNo IN ('1248','1063','963','1524')
ORDER BY CASE InvoiceNo
WHEN '1248' THEN 1
WHEN '1063' THEN 2
WHEN '963' THEN 3
WHEN '1524' THEN 4
END
Is there any other way if it is hard coded ?
Thanks & Regards,
MC
May 9, 2010 at 9:20 am
only4mithunc (5/9/2010)
If it is hard coded, I'm thinking as below.
SELECT *
FROM InvoiceDetails
WHERE InvoiceNo IN ('1248','1063','963','1524')
ORDER BY CASE InvoiceNo
WHEN '1248' THEN 1
WHEN '1063' THEN 2
WHEN '963' THEN 3
WHEN '1524' THEN 4
END
Is there any other way if it is hard coded ?
That's the right line of thinking but I try to avoid hardcoding because it makes a mess in the future when things change. We still need to know what the source of these invoice numbers is so we can come up with a good and proper non-hardcoded solution.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 9, 2010 at 9:28 am
I agree with you Jeff.
Thanks & Regards,
MC
May 9, 2010 at 1:41 pm
Heh... don't ya just love it when the OP doesn't even show up for 24 hours?
--Jeff Moden
Change is inevitable... Change for the better is not.
May 9, 2010 at 9:26 pm
Hello ,
The data inside the " IN Clause" is hard coded and I want the output in the same order as it is inside the "IN Clause". Using "Order By" will not solve my problem.
Thanks for the Quick Response
Regards,
Kavya
May 9, 2010 at 9:57 pm
Hello Mithun,
Thanks for the code. It solved my Problem.
Regards,
Kavya
May 9, 2010 at 10:15 pm
c_kavya_shri (5/9/2010)
Hello ,The data inside the " IN Clause" is hard coded and I want the output in the same order as it is inside the "IN Clause". Using "Order By" will not solve my problem.
Thanks for the Quick Response
Regards,
Kavya
SELECT *
FROM InvoiceDetails
WHERE InvoiceNo IN ('1248','1063','963','1524')
ORDER BY CASE InvoiceNo
WHEN '1248' THEN 1
WHEN '1063' THEN 2
WHEN '963' THEN 3
WHEN '1524' THEN 4
END
c_kavya_shri (5/9/2010)
Hello Mithun,Thanks for the code. It solved my Problem.
Regards,
Kavya
BWAA-HAAA... I'm thinking that ORDER BY DID solve your problem.
But, unless it's a one-off script, you or someone in the future still have a problem whether you care to acknowledge it or not... hardcoding.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 10, 2010 at 6:06 am
Hi Jeff,
I would like to know how you will approach this if the values are not hard coded, say it is from some table or it is a string with more than 100 values.
Thanks & Regards,
MC
May 10, 2010 at 6:49 am
only4mithunc (5/10/2010)
Hi Jeff,I would like to know how you will approach this if the values are not hard coded, say it is from some table or it is a string with more than 100 values.
Sure... That's why I was asking what the "source" of the criteria is.
If it's meant to be relatively "static", then a table would do the trick nicely because if you need to change the criteria, you don't have to find and change the code... just update the table for the invoice number and position that it should appear in for the ORDER BY. That also works especially well if the criteria is used in other code.
However, its difficult for me to believe that invoice numbers would be "static", not just in this case, but in any case. Although the problem could certainly be done using dynamic SQL, you have to be careful about SQL Injection with anything that may be public facing and, sometimes, with things that face internal people depending on who has access. With that in mind, I'd have a CSV splitter built into a CTE or an Inline Table Valued Function to parse the parameters and join to those parsed parameters. Of course, the splitter would also return a number representing the position that each invoice number should appear in for the ORDER BY.
Some people will pass the parameters as XML and parse that. That's fine, I suppose... I like to be kinder to the pipe, though.
If it's a true one-off piece of code, then hardcoding would probably be fine but I've also been through supposed one-off situations like this where they recur more often than not. It may be worth building a small stored proc for such a thing.
For more information on how you can pass 1, 2, or even 3 dimensional parameters in simple delimited formats, please see the following article...
http://www.sqlservercentral.com/articles/T-SQL/63003/
... and a good splitter iTVF will make life even easier.
--Jeff Moden
Change is inevitable... Change for the better is not.
May 10, 2010 at 7:08 am
Thanks a lot for the reply Jeff.
Thanks & Regards,
MC
May 10, 2010 at 9:04 pm
You bet, MC... thanks for the feedback.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy