假设我有一个数据集:
ID NEXT ORDER
1456 1679 1
1679 1679 2
1578 1679 3
9492 1679 4
此数据集中唯一要更改的列是NEXT。 ID始终是唯一的,NEXT始终与单个ID相关联,并且将在每一行中重复。 ORDER是一个顺序的int。
我需要一个orderby或其他将执行以下排序的SQL语句:
NEXT ID始终位于订购的底部。
在NEXT的ORDER之后具有顺序更大ORDER的ID始终位于顶部
然后顺序从第二行开始顺序开始,ORDER是第一行的顺序之后的下一行
示例:
对于上表,订购将是
1578
9492
1456
1679
下一个已更改的另一个表:
ID NEXT ORDER
1456 1578 1
1679 1578 2
1578 1578 3
9492 1578 4
将是:
9492
1456
1679
1578
答案 0 :(得分:2)
+(void)showPositiveMessage :(NSString*)message{
[ViewController showAlertWithBackgroundColor:[UIColor greenColor] textColor:[UIColor whiteColor] message:message];}
+(void)showNegativeMessage :(NSString*)message{
[ViewController showAlertWithBackgroundColor:[UIColor redColor] textColor:[UIColor whiteColor] message:message];}
+(void)showAlertWithBackgroundColor:(UIColor*)backgroundColor textColor:(UIColor*)textColor message:(NSString*)String{
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
UILabel* label = [[UILabel alloc] initWithFrame:CGRectZero];
label.textAlignment = NSTextAlignmentCenter;
label.text = String;
label.font = [UIFont fontWithName:@"HelveticaNeue" size:FONTSIZE];
label.adjustsFontSizeToFitWidth = true;
[label sizeToFit];
label.numberOfLines = 4;
label.layer.shadowColor = [UIColor grayColor].CGColor;
label.layer.shadowOffset = CGSizeMake(4, 3);
label.layer.shadowOpacity = 0.3;
label.frame = CGRectMake(320, 64, appDelegate.window.frame.size.width, 44);
label.alpha = 1;
label.backgroundColor = backgroundColor;
label.textColor = textColor;
[appDelegate.window addSubview:label];
CGRect basketTopFrame = label.frame;
basketTopFrame.origin.x = 0;
[UIView animateWithDuration:2.0 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0.1 options:UIViewAnimationOptionCurveEaseOut animations: ^(void){
label.frame = basketTopFrame;
} completion:^(BOOL finished){
[label removeFromSuperview];
}
];}
SQLFiddle
(小提琴使用Postgresql,因为Sql Server目前似乎已被打破)。
答案 1 :(得分:1)
类似于Joel的回答,仅使用窗口函数而不是INNER JOIN或CROSS APPLY:
SELECT
*
FROM
#temp t
ORDER BY
CASE
WHEN t.[Order] > MAX(CASE WHEN ID = [NEXT] THEN [ORDER] END) OVER () THEN -999999999 + t.[Order]
ELSE t.[Order]
END
这是一个使用CROSS APPLY
的版本SELECT
t.*
FROM
#temp t
CROSS APPLY (SELECT [Order] FROM #temp WHERE ID = NEXT) n
ORDER BY
CASE
WHEN t.[Order] > n.[Order] THEN -99999999 + t.[Order]
ELSE t.[Order]
END
答案 2 :(得分:0)
这应该有效:
示例数据:
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
DROP TABLE #temp;
CREATE TABLE #temp(ID INT
, [NEXT] INT
, [ORDER] INT);
INSERT INTO #temp
VALUES
(1456
, 1679
, 1),
(1679
, 1679
, 2),
(1578
, 1679
, 3),
(9492
, 1679
, 4);
QUERY:
DECLARE @rowcount INT;
SELECT @rowcount = COUNT(1)
FROM #temp
WHERE [ORDER] >
(SELECT [ORDER]
FROM #temp
WHERE ID = [NEXT]);
SELECT ID
FROM
(SELECT *
, rn = ROW_NUMBER() OVER(ORDER BY [ORDER])
FROM #temp
WHERE [ORDER] >
(SELECT [ORDER]
FROM #temp
WHERE ID = [NEXT])
UNION
SELECT *
, rn = @rowcount + ROW_NUMBER() OVER(ORDER BY [ORDER])
FROM #temp
WHERE [ORDER] <=
(SELECT [ORDER]
FROM #temp
WHERE ID = [NEXT])) AS A
ORDER BY rn;
结果: