如何分别计算多个分区中的数据?

时间:2017-06-16 08:19:10

标签: sql hive

例如,有一个表tablea,包含3个分区:day1,day2,day3。

是否可以计算:

select count(distinct uid) as c1 from tablea where day = day1;
select count(distinct uid) as c2 from tablea where day = day2;
select count(distinct uid) as c3 from tablea where day = day3;

在一个没有'union'的HQL中,然后返回c1+c2+c3

select .. from tablea where day between .. and ..不正确。

解决

这可以通过以下方式完成:

select sum(uid) 
from (
    select 
        count(distinct uid) as uid
    from tablea 
    where day between 'day1' and 'day3'
    group by day) a;

1 个答案:

答案 0 :(得分:0)

如果您想要总计SELECT SUM(CASE WHEN day = day1 THEN 1 ELSE 0 END)+ SUM(CASE WHEN day = day2 THEN 1 ELSE 0 END)+ SUM(CASE WHEN day = day3 THEN 1 ELSE 0 END) FROM tablea 。然后我认为你可以这样做:

SELECT 
    SUM(CASE WHEN day <= day1 AND day>=day3 THEN 1 ELSE 0 END)
FROM
    tablea

<强>更新

我认为你也可以这样做:

#import <XCTest/XCTest.h>
#import <KIF/KIF.h>

@interface AutomatedUITestsSampleUITests : KIFTestCase

@end

@implementation AutomatedUITestsSampleUITests

- (void)setUp {
    [super setUp];

    // Put setup code here. This method is called before the invocation of each test method in the class.

    // In UI tests it is usually best to stop immediately when a failure occurs.
    self.continueAfterFailure = NO;
    // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
    [[[XCUIApplication alloc] init] launch];

    // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
}

- (void)tearDown {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
    [super tearDown];
}

- (void)testExample {

    [tester waitForViewWithAccessibilityLabel:@"LOGIN - Button"];

    [tester tapViewWithAccessibilityLabel:@"LOGIN - Button" traits:UIAccessibilityTraitButton];
}