我使用 HP Vertica 编写查询。我想选择一些看起来像Excel的数据,当你使用VLOOKUP
函数并启用范围标志时,Excel应该这样做[VLOOKUP(A1; B1:C4; 2; 1 )]。
我举一个简单的例子来更好地理解。我有一张显示历史仓库运动的表格。
stock_history
-------------
|product|location|time_stamp |
|-------|--------|------------|
| A | Loc A | 2015-01-13 |
| A | Loc B | 2015-03-13 |
现在我希望在每个月份看到A的位置(让我们说每个月只允许一次移动以使其更容易)
看起来应该是这样的
|product|location|month |
|-------|--------|----- ---|
| A | Loc A | 2015-01 |
| A | Loc A | 2015-02 |
| A | Loc B | 2015-03 |
我已经生成了一个显示所有月份的表格:
all_months
----------
|month |
|---------|
| 2015-01 |
| 2015-02 |
| 2015-03 |
这是我试过的一个声明
select his.product
, his.location
, mon.month
from stock_history as his
left outer join all_months as mon
on mon.month = to_char( time_stamp, 'YYYY-MM' )
|product |location|month |
|--------|--------|----- ---|
| A | Loc A | 2015-01 |
| (null) | (null) | 2015-02 |
| A | Loc B | 2015-03 |
我如何管理它以获得产品A也在二月份线,因为它仍然在2月的位置A?
感谢您阅读我的问题。我期待着得到你的答案;)
此致 菲利克斯
答案 0 :(得分:0)
您可以使用cross join
生成所有月份/产品组合。然后使用相关子查询从最近或当前月份获取位置:
select mon.month, p.product,
(select sh.location
from stock_history sh
where mon.month <= to_char(sh.time_stamp, 'YYYY-MM' ) and p.product = sh.product
order by mon.month desc
limit 1
) as location
from (select distinct product p from stock_history) p cross join
all_months mon;
答案 1 :(得分:0)
你走了! 我还添加了添加月份的示例。使用递归功能。 我用oracle测试过,也应该和vertica一起工作。
-(void)validateLoginWithUsername:(NSString *)usernam withPassword:(NSString *)pasword
{
NSString *urlString = [NSString stringWithFormat:@baseURL@"/1/login?username=%@&password=%@",usernam,pasword];
NSURL *url = [NSURL URLWithString:urlString];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = @{ @AppId : @id1,
@APIKey : @key1 };
self.session = [NSURLSession sharedSession];
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURLSessionDataTask * dataTask = [self.session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200)
{
NSDictionary *dataToBeTransferedBack = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
ATTDataLibrary *instance = [ATTDataLibrary getInstanceOfDataLibrary];
[instance manupulateUserData:dataToBeTransferedBack];
[singletonInstance getTheSubjectsData];
}
else
{
dispatch_sync(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Invalid Login" message:@"Please Check the username and password entered" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
});
}