如何始终选择某个值1st sql

时间:2017-01-23 10:33:09

标签: sql sql-server

说我有一个带值

的TABLE

1,2,2,3,4和ALL

在SAME专栏

然后我

SELECT DISTINCT

value

FROM table

我如何始终选择' All' 1号?

或至少命令它'所有'是第1名。

注意:该列是VARCHAR(x)

修改

SELECT DISTINCT
Strategy
FROM @Strategy
ORDER BY case when Strategy = 'All' then 0 else 1 end, 
Strategy DESC

这是我目前的查询

4 个答案:

答案 0 :(得分:2)

您为每个问题选择了正确的关键字(DISTINCT)和正确的表达式(ORDER BY CASE WHEN ... END)。诀窍是逐个解决问题,而不是一次。

SELECT *
FROM 
(
    SELECT DISTINCT T1.*
    FROM (VALUES ('1'), ('2'), ('2'), ('3'), ('4'), ('ALL')) AS T1(Strategy)
) AS T2
ORDER BY CASE WHEN Strategy = 'ALL' THEN 0 ELSE 1 END, Strategy;

答案 1 :(得分:1)

在这种情况下,只需使用:

void

答案 2 :(得分:0)

您可以使用NSURLSession(而不是NSMutableURLRequest *request =[[NSMutableURLRequest alloc] initWithURL:requestURL]; NSLog(@"fetchAPromotionWithType = %@", requestURL); [request setHTTPMethod:@"GET"]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; int responseStatusCode = (int)[httpResponse statusCode]; if ( responseStatusCode == OK_RESPONSE_CODE || responseStatusCode == CREATED_RESPONSE_CODE || responseStatusCode == ACCEPTED_RESPONSE_CODE || responseStatusCode == PARTIAL_RESPONSE_CODE) { if (data) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; NSLog(@"Data->%@",dict); if (dict != nil && [dict isKindOfClass:[NSDictionary class]]) { NSDictionary *dataDict = [dict objectForKey:@"data"]; if (dataDict != nil && [dataDict isKindOfClass:[NSDictionary class]]) { //Get your success Data } } } } else { if (responseStatusCode == 0) { NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"No Connection! Please try again.", @"message", nil]; //Get Your Fail Data } else if (data) { // NSArray *tempArray = @[@"fan", @"vungle"]; //[JLSharedData sharedManager].savedWaterfallInterstitialArray = nil; //testing NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; //Get Your Fail Data } } }); }]; [task resume]; }]; )与@jarlh建议的上述评论中的GROUP BY条款(稍微调整一下)。

DISTINCT

或者继续使用ORDER BY,您可以将SELECT Strategy FROM @Strategy GROUP BY Strategy ORDER BY CASE WHEN Strategy = 'All' THEN 0 ELSE Strategy END 语句移动到DISTINCT子句,以创建要在CASE子句中使用的新列:

SELECT

或者,如果后续项目的顺序无关紧要,请按照@ChrisChurch的建议保持简单。

答案 3 :(得分:0)

请试一试。

Declare @table table (id varchar(max))

Insert into @table values ('1'),('2'),('2'),('All'),('3'),('4')

select * from @table

--select * from @table order by id 

select * from @table order by case when id = 'All' then 0 else id end 

另一个转折,假设您有2个varchar条目仍然是这个逻辑工作

Declare @table table (id varchar(max))

Insert into @table values ('1'),('2'),('2'),('All'),('3'),('4'),('Select')

select * from @table

select * from @table order by id 

select * from @table order by case when id in ( 'All' , 'Select') then 0 else id end