简体   繁体   English

在SQL Server 2008中排序

[英]Order by in SQL Server 2008

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 | ID | Image Type |Created Dt | 图像类型|创建的Dt | Image Position | 图片位置|
'--------------------------------------------------------- '------------------------------------------------- --------
1* | 1 * | Original Image 原始图片 | | 2013-11-20 17:27:06.380 2013-11-20 17:27:06.380 | | 1 | 1 |
2* | 2 * | Original Image 原始图片 | | 2013-11-20 17:27:07.380 2013-11-20 17:27:07.380 | | 2 | 2 |
3* | 3 * | *Blank_Image *| *空白图片* | 2013-11-20 17:27:08.380 2013-11-20 17:27:08.380 | | 0 | 0 |
4* | 4 * | Original Image 原始图片 | | 2013-11-20 17:27:09.380 2013-11-20 17:27:09.380 | | 3 | 3 |
'--------------------------------------------------------- '------------------------------------------------- --------

Now if I use 现在,如果我使用

Order by [Created dt], [Image Position] Then I am getting proper 按[Created dt],[Image Position]排序,然后我就可以正常工作了

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 4的图像位置更改为图像位置1,将图像ID 1更改为图像位置3

'--------------------------------------------------------- '------------------------------------------------- --------
Id | ID | Image Type |Created Dt | 图像类型|创建的Dt | Image Position | 图片位置|
'--------------------------------------------------------- '------------------------------------------------- --------
1* | 1 * | Original Image 原始图片 | | 2013-11-20 17:27:06.380 2013-11-20 17:27:06.380 | | 3 | 3 |
2* | 2 * | Original Image 原始图片 | | 2013-11-20 17:27:07.380 2013-11-20 17:27:07.380 | | 2 | 2 |
3* | 3 * | *Blank_Image *| *空白图片* | 2013-11-20 17:27:08.380 2013-11-20 17:27:08.380 | | 0 | 0 |
4* | 4 * | Original Image 原始图片 | | 2013-11-20 17:27:09.380 2013-11-20 17:27:09.380 | | 1 | 1 |
'--------------------------------------------------------- '------------------------------------------------- --------

Now my expected output should be 现在我的预期输出应该是

'--------------------------------------------------------- '------------------------------------------------- --------
Id | ID | Image Type |Created Dt | 图像类型|创建的Dt | Image Position | 图片位置|
'--------------------------------------------------------- '------------------------------------------------- --------
4* | 4 * | Original Image 原始图片 | | 2013-11-20 17:27:09.380 2013-11-20 17:27:09.380 | | 1 | 1 |
2* | 2 * | Original Image 原始图片 | | 2013-11-20 17:27:07.380 2013-11-20 17:27:07.380 | | 2 | 2 |
3* | 3 * | *Blank_Image *| *空白图片* | 2013-11-20 17:27:08.380 2013-11-20 17:27:08.380 | | 0 | 0 |
1* | 1 * | Original Image 原始图片 | | 2013-11-20 17:27:06.380 2013-11-20 17:27:06.380 | | 3 | 3 |
'--------------------------------------------------------- '------------------------------------------------- --------

But here in this case, if I am using 但是在这种情况下,如果我正在使用

Order by [Created dt], [Image Position] it is giving me 按[Created dt],[Image Position]排序,它给了我

'--------------------------------------------------------- '------------------------------------------------- --------
Id | ID | Image Type |Created Dt | 图像类型|创建的Dt | Image Position | 图片位置|
'--------------------------------------------------------- '------------------------------------------------- --------
1* | 1 * | Original Image 原始图片 | | 2013-11-20 17:27:06.380 2013-11-20 17:27:06.380 | | 3 | 3 |
2* | 2 * | Original Image 原始图片 | | 2013-11-20 17:27:07.380 2013-11-20 17:27:07.380 | | 2 | 2 |
3* | 3 * | *Blank_Image *| *空白图片* | 2013-11-20 17:27:08.380 2013-11-20 17:27:08.380 | | 0 | 0 |
4* | 4 * | Original Image 原始图片 | | 2013-11-20 17:27:09.380 2013-11-20 17:27:09.380 | | 1 | 1 |
'--------------------------------------------------------- '------------------------------------------------- --------

I am not able to explain this more than this.. If someone can understand this, please help me out. 我无法对此进行更多解释。.如果有人可以理解,请帮助我。

Thanks in advance. 提前致谢。

Since you are sorting on CreatedDate first and then ImagePosition - this is the expected behavior. 由于您首先在CreatedDate上排序,然后在ImagePosition上排序-这是预期的行为。 The second sorting field (ImagePosition) will only be used as a tiebreaker where multiple rows for the same CreatedDate are found. 第二个排序字段(ImagePosition)将仅用作决胜局,在该处发现同一CreatedDate的多个行。 In your case, all CreatedDate's are different, and so the ImagePosition sorting is never used (or needed). 在您的情况下,所有CreatedDate都不同,因此从不使用(或不需要)ImagePosition排序。

Try changing your sort order to get what you want: 尝试更改排序顺序以获取所需内容:

Order by [Image Position], [Created dt]

如果我理解正确,则需要:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM