如何连接"类似" SQL中的行?

时间:2015-07-14 07:41:23

标签: sql oracle

现在我有以下SQL:

//
//  ViewController.m
//  DynamicNewSections
//
//  Created by J Pulikkottil on 14/07/15.
//  Copyright (c) 2015 J Pulikkottil. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property(nonatomic) NSMutableArray *arrayData;
@property (strong, nonatomic) IBOutlet UITableView *tableViewTest;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.arrayData = [NSMutableArray array];


//section1
[self addSectionArray];
}
- (IBAction)addSection:(UIButton *)sender {

[self addSectionArray];
[self.tableViewTest reloadData];
}

- (void) addSectionArray{

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Height",@"label", @"", @"value", nil];
NSArray *arraysection = [NSArray arrayWithObjects:dict, nil];

[self.arrayData addObject:arraysection];

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{


UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableViewTest.frame.size.width, 45)];
view.backgroundColor = [UIColor yellowColor];
return view;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

// Return the number of sections.
return [self.arrayData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

// Return the number of rows in the section.
return [[self.arrayData objectAtIndex:section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellDetails" forIndexPath:indexPath];

NSArray *arraysection = [self.arrayData objectAtIndex:indexPath.section];
NSDictionary *dict = [arraysection objectAtIndex:indexPath.row];

cell.textLabel.text = [dict valueForKey:@"label"];
cell.detailTextLabel.text = [dict valueForKey:@"value"];
return cell;
}


@end

查询结果为:

SELECT * FROM (    
    SELECT Employee, Template, TRUNC(A.FROM) DATE_FROM, SUM(B.AMOUNTTOTAL) AMNT_TOTAL, SUM(B.AMOUNT) AMNT
    FROM Table1 A, Table2 B, Table3 C
    WHERE ...
    GROUP BY Employee, Template, TRUNC(A.FROM)
    )
    PIVOT
    (
    SUM (AMNT_TOTAL)
      FOR DATE_FROM IN ('01-MAY-2015' date1, '02-MAY-2015' date2, '03-MAY-2015' date3)
    ) 

如何将数据转换为以下形式:

+------------+------------+--------+---------+---------+---------+
|  Employee  |  Template  |  AMNT  |  DATE1  |  DATE2  |  DATE3  |
+------------+------------+--------+---------+---------+---------+
| Kate       | TemplA     |    2   |         |    12   |         |
| Kate       | TemplA     |    4   |         |         |         |
| Kate       | TemplA     |    7   |    16   |         |    14   |
| John       | TemplB     |    5   |    18   |    25   |         |
+------------+------------+--------+---------+---------+---------+

我知道我可以使用连接运算符:添加外部查询:

+------------+------------+---------+---------+---------+
|  Employee  |  Template  |  DATE1  |  DATE2  |  DATE3  |
+------------+------------+---------+---------+---------+
| Kate       | TemplA     |   16|7  |   12|2  |   14|7  |
| John       | TemplB     |   18|5  |   25|5  |         |
+------------+------------+---------+---------+---------+

但如何分组?

1 个答案:

答案 0 :(得分:0)

您可以使用max始终获取非空值:

SELECT 
  Employee, 
  Template, 
  max (DATE1||'|'||AMT) DATE1,
  ... 
  FROM (old_query)
  GROUP BY Employee, Template