我有一个包含以下格式数据的表格:
Mand_ID Type Sub_Type D_Freq Limit_Type mon_freq Val Units
======= ======= ======== ====== =========== ========= ==== =======
IAC901 TRACKING_ERROR EX_ANTE NA LESS_THAN MONTHLY 200 BASIS_POINTS
IAC901 TRACKING_ERROR EX_ANTE NA GREATER_THAN MONTHLY 150 BASIS_POINTS
IAC901 TRACKING_ERROR EX_ANTE NA LESS_THAN MONTHLY 450 BASIS_POINTS
IAC901 TRACKING_ERROR EX_ANTE NA GREATER_THAN_EQ MONTHLY 0 BASIS_POINTS
现在我想选择max(Val)和min(Val)为一的行。 即我想从上表中一次选择以下两行
Mand_ID Type Sub_Type D_Freq Limit_Type mon_freq Val Units
======= ======= ======== ====== =========== ========= === ======
IAC901 TRACKING_ERROR EX_ANTE NA LESS_THAN MONTHLY 450 BASIS_POINTS
IAC901 TRACKING_ERROR EX_ANTE NA GREATER_THAN_EQ MONTHLY 0 BASIS_POINTS
注意:表中Mand_ID
有许多不同的值;我希望每个单独的Mand_ID
具有最大和最小值的行。
答案 0 :(得分:1)
select *
from theTable t,
(select max(val) as max_val, min(val) as min_val from theTable) as max_min
where t.val=max_min.max_val or t.val=max_min.min_val
答案 1 :(得分:-1)
假设该表是从主包加载的文本文件:
TestText is:
Mand_ID Type Sub_Type D_Freq Limit_Type mon_freq Val Units
======= ======= ======== ====== =========== ========= ==== =======
IAC901 TRACKING_ERROR EX_ANTE NA LESS_THAN MONTHLY 200 BASIS_POINTS
IAC901 TRACKING_ERROR EX_ANTE NA GREATER_THAN MONTHLY 150 BASIS_POINTS
IAC901 TRACKING_ERROR EX_ANTE NA LESS_THAN MONTHLY 450 BASIS_POINTS
IAC901 TRACKING_ERROR EX_ANTE NA GREATER_THAN_EQ MONTHLY 0 BASIS_POINTS
...比
// Do any additional setup after loading the view, typically from a nib.
NSString* path = [[NSBundle mainBundle] pathForResource:@"TestText"
ofType:@"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error: nil];
NSArray *array = [content componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSString *numberString;
int MinIndex=0,MaxIndex=0,MinVal=0,MaxVal = 0;
BOOL FirstTrackingError=YES;
for (int i=0;i<[array count];++i)
{
if ([[array objectAtIndex:i] rangeOfString:@"TRACKING_ERROR"].location != NSNotFound)
{
NSRange range = [[array objectAtIndex:i] rangeOfString:@"MONTHLY"];
NSString *substring = [[array objectAtIndex:i] substringFromIndex:NSMaxRange(range)];
NSScanner *scanner = [NSScanner scannerWithString: substring];
NSCharacterSet *numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
// Throw away characters before the first number.
[scanner scanUpToCharactersFromSet: numbers intoString:NULL];
// Collect numbers.
[scanner scanCharactersFromSet:numbers intoString:&numberString];
if(FirstTrackingError == YES)
{
MinIndex=MaxIndex=i;
MinVal=MaxVal=[numberString intValue];
FirstTrackingError=NO;
}
else
{
if ([numberString intValue] > MaxVal)
{
MaxVal=[numberString intValue];
MaxIndex=i;
}
else if ([numberString intValue] < MinVal)
{
MinVal=[numberString intValue];
MinIndex=i;
}
}
}
}
NSLog(@"%@",[array objectAtIndex:MinIndex]);
NSLog(@"%@",[array objectAtIndex:MaxIndex]);
NSString *result=[array objectAtIndex:MaxIndex];
result=[result stringByAppendingFormat:@"\n%@",[array objectAtIndex:MinIndex]];
NSLog(@"Result : \r\n%@",result);
The result is:
IAC901 TRACKING_ERROR EX_ANTE NA LESS_THAN MONTHLY 450 BASIS_POINTS
IAC901 TRACKING_ERROR EX_ANTE NA GREATER_THAN_EQ MONTHLY 0 BASIS_POINTS