Hive计算另一个表中匹配行的数量

时间:2017-07-04 01:51:30

标签: hive presto

表1:

| ID | Timestamp
| 1  | 1970
| 2  | 1971

表2:

| Timestamp |
| 1970      |
| 1970      |
| 1970      |
| 1971      |
| 1971      |

如何执行以我得到的方式连接表的查询:

| ID | Timestamp | Count
| 1  | 1970      | 3
| 2  | 1971      | 2

基本上是join on timestamp,但只是为了得到一个计数?

1 个答案:

答案 0 :(得分:1)

{
        xtype: 'fieldset',
        title: 'Add to Descriptors',
        items: [
            {
                xtype: 'checkboxgroup',
                items: [
                    {boxLabel: '1.1', name: ''},
                    {boxLabel: '1.2', name: ''},
                    {boxLabel: '1.3', name: ''},
                    {boxLabel: '1.4', name: ''},
                    {boxLabel: '1.5', name: ''},
                    {boxLabel: '1.6', name: ''}
                ]
            },
            {
                xtype: 'checkboxgroup',
                items: [
                    {boxLabel: '2.1', name: ''},
                    {boxLabel: '2.2', name: ''},
                    {boxLabel: '2.3', name: ''},
                    {boxLabel: '2.4', name: ''},
                    {boxLabel: '2.5', name: ''},
                    {boxLabel: '2.6', name: ''}
                ]
            }
        ]
}
select  t1.ID
       ,t1.`Timestamp`
       ,coalesce (t2.`Count`,0) as `Count`

from    Table1 t1

        left join  (select      `Timestamp`
                                ,count(*)    as `Count`
                    from        Table2
                    group by    `Timestamp`
                    ) t2

        on          t2.`Timestamp` = t1.`Timestamp`