0

I have an issue with my query.

I need to sort my records based on the Created date and Image position columns.

Where as I have the table as below

'---------------------------------------------------------
Id | Image Type |Created Dt | Image Position |
'---------------------------------------------------------
1*|Original Image |2013-11-20 17:27:06.380 | 1 |
2*|Original Image |2013-11-20 17:27:07.380 | 2 |
3*|*Blank_Image *|2013-11-20 17:27:08.380 | 0 |
4*|Original Image |2013-11-20 17:27:09.380 | 3 |
'---------------------------------------------------------

Now if I use

Order by [Created dt], [Image Position] Then I am getting proper

Now I want to change the image position of imge id 4 to image position 1 and image id 1 to image position 3

'---------------------------------------------------------
Id | Image Type |Created Dt | Image Position |
'---------------------------------------------------------
1*|Original Image |2013-11-20 17:27:06.380 | 3 |
2*|Original Image |2013-11-20 17:27:07.380 | 2 |
3*|*Blank_Image *|2013-11-20 17:27:08.380 | 0 |
4*|Original Image |2013-11-20 17:27:09.380 | 1 |
'---------------------------------------------------------

Now my expected output should be

'---------------------------------------------------------
Id | Image Type |Created Dt | Image Position |
'---------------------------------------------------------
4*|Original Image |2013-11-20 17:27:09.380 | 1 |
2*|Original Image |2013-11-20 17:27:07.380 | 2 |
3*|*Blank_Image *|2013-11-20 17:27:08.380 | 0 |
1*|Original Image |2013-11-20 17:27:06.380 | 3 |
'---------------------------------------------------------

But here in this case, if I am using

Order by [Created dt], [Image Position] it is giving me

'---------------------------------------------------------
Id | Image Type |Created Dt | Image Position |
'---------------------------------------------------------
1*|Original Image |2013-11-20 17:27:06.380 | 3 |
2*|Original Image |2013-11-20 17:27:07.380 | 2 |
3*|*Blank_Image *|2013-11-20 17:27:08.380 | 0 |
4*|Original Image |2013-11-20 17:27:09.380 | 1 |
'---------------------------------------------------------

I am not able to explain this more than this.. If someone can understand this, please help me out.

Thanks in advance.

3
  • What is it you actually want to do with this. You desired data seems not be sorted by date nor by image position? Commented Nov 20, 2013 at 15:22
  • I want to the all original images to be sorted based on image position but at the same time, I want all Blank images should be sorted on created dt. Will this be possible? Sorry this is little complicated. Commented Nov 20, 2013 at 15:34
  • I don't think what you want can be done by simply ordering by date and position. Commented Nov 20, 2013 at 16:02

2 Answers 2

3

Since you are sorting on CreatedDate first and then ImagePosition - this is the expected behavior. The second sorting field (ImagePosition) will only be used as a tiebreaker where multiple rows for the same CreatedDate are found. In your case, all CreatedDate's are different, and so the ImagePosition sorting is never used (or needed).

Try changing your sort order to get what you want:

Order by [Image Position], [Created dt]
Sign up to request clarification or add additional context in comments.

2 Comments

I want all blank pages without any sorting and remaining original pages with sorting. If a blank page have position as 3 even after sorting the position should be 3.
To clarify, the reason the query worked for you initially is not because of the ImagePosition (since that was being ignored), but rather because the CreatedDt's happen to occur in the order you anticipated. The only criteria being used here is CreatedDt - if you want to add other criteria, you can do so - but that won't leave any of your rows unsorted, nor can you ever do this using an ORDER BY statement. You can simulate what you want (unsorted rows among sorted rows) if you have some hard logic to put in place, but it will look fairly messy by the time you are done.
0

If I understand correctly you need:

order by [Created dt] DESC, [Image Position]

3 Comments

No, that's not the problem at all - it would also still give an undesired result.
Yes you're right I didn't undesrstand it correctly. I just saw that the row that has "17:27:07.380" is above "17:27:07.380". My answer is incorrect
I want all blank pages without any sorting and remaining original pages with sorting. If a blank page have position as 3 even after sorting the position should be 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.