Sql 如何在不将新值写入表的情况下向现有列添加新值?

Sql 如何在不将新值写入表的情况下向现有列添加新值?,sql,hive,hiveql,Sql,Hive,Hiveql,我有下表1: | yyyy_mm_dd | id | feature | status | |------------|----|-----------------|---------------| | 2019-05-13 | 2 | pricing | implemented | | 2019-05-13 | 2 | pricing | first_contact | | 2019-05-13 | 5 | reviews

我有下表1:

| yyyy_mm_dd | id | feature         | status        |
|------------|----|-----------------|---------------|
| 2019-05-13 | 2  | pricing         | implemented   |
| 2019-05-13 | 2  | pricing         | first_contact |
| 2019-05-13 | 5  | reviews         | implemented   |
| 2019-05-13 | 5  | pricing         | implemented   |
| 2019-05-13 | 6  | reviews         | first_contact |
| 2019-05-13 | 6  | reviews         | implemented   |
| 2019-05-13 | 6  | promotions_geo  | first_contact |
| 2019-05-13 | 6  | prop_management | first_contact |
有两种状态,已实施和第一个联系人。我想介绍第三个,这将是无接触。这将是ID总数减去处于已实施状态和第一个联系人状态的ID之和

我可以从二级表中获得ID总数,如下所示:

select
    count(id)
from
    table2
因此,我尝试将上述各项合并,以便得到ID的总计数,然后减去:

select
    yyyy_mm_dd,
    feature,
    count(s.id) as implemented_and_first_contact_total,
    null as total_ids
from
    table1 s
where
    s.yyyy_mm_dd = '2020-05-06'
group by
    1,2,4
union all
select
    null as yyyy_mm_dd,
    null as feature,
    null as implemented_and_first_contact_total,
    count(id) as total_ids
from
    table2
现在我不确定如何从total_ID中减去implemented_和first_contact_total,以获得no_contact的值,并将其作为status列中的值。也许在这里使用工会是不正确的

编辑:输出。假设总共有300个ID。输出如下所示:

| yyyy_mm_dd | feature         | status        | id_count |
|------------|-----------------|---------------|----------|
| 2019-05-13 | pricing         | implemented   | 2        |
| 2019-05-13 | pricing         | first_contact | 1        |
| 2019-05-13 | pricing         | no_contact    | 297      |
| 2019-05-13 | reviews         | implemented   | 2        |
| 2019-05-13 | reviews         | first_contact | 1        |
| 2019-05-13 | reviews         | no_contact    | 297      |
| 2019-05-13 | promotions_geo  | first_contact | 1        |
| 2019-05-13 | promotions_geo  | no_contact    | 299      |
| 2019-05-13 | prop_management | first_contact | 1        |
| 2019-05-13 | prop_management | no_contact    | 299      |
这是你想要的吗

select yyyy_mm_dd,
       (count(distinct id) -
        count(distinct case when status in ('implemented', 'first_contact') then id end)
       ) as no_contact
from t
group by yyyy_mm_dd
这是你想要的吗

select yyyy_mm_dd,
       (count(distinct id) -
        count(distinct case when status in ('implemented', 'first_contact') then id end)
       ) as no_contact
from t
group by yyyy_mm_dd

更新:从SELECT中删除了不相关的子查询,并添加了交叉联接

试试这个:

select yyyy_mm_dd, feature, status,
count(id) as id_count
from table1 
group by yyyy_mm_dd, feature, status
union all
select yyyy_mm_dd, feature, 'no_contact' as status, 
(cnt - count(id)) as id_count
from table1 cross join (select count(id) as cnt from table2) 
group by yyyy_mm_dd, feature, cnt;

更新:从SELECT中删除了不相关的子查询,并添加了交叉联接

试试这个:

select yyyy_mm_dd, feature, status,
count(id) as id_count
from table1 
group by yyyy_mm_dd, feature, status
union all
select yyyy_mm_dd, feature, 'no_contact' as status, 
(cnt - count(id)) as id_count
from table1 cross join (select count(id) as cnt from table2) 
group by yyyy_mm_dd, feature, cnt;


编辑您的问题并显示您想要的结果。我修改了示例数据并添加了示例输出。我希望现在更清楚了,你怎么把表1和表2连接起来?表2中有哪些列?@django unchecked表2中包含元信息。两个表都可以通过ID列连接。编辑您的问题并显示所需的结果。我修改了示例数据并添加了示例输出。我希望现在更清楚了,你怎么把表1和表2连接起来?表2中有哪些列?@django unchecked表2中包含元信息。这两个表都可以通过ID列连接。我希望“无联系人”状态是“状态”列中的值,而不是新列。@stackq。然后将其插入到表中或使用union all查询。我希望“无联系人”状态是“状态”列中的值,而不是新列。@stackq。然后将其插入到表中或使用union all查询。实际上,如果您看到group by子句和id_count,则它们是两个不同的结果集。它对你有用吗?我是说在你的解决方案中,表1被使用了两次。是的,我需要使用它,因为我需要从第二个表中减去id总数。如果答案对您有效,请接受。表1仅包含总ID的子集,因此计数将不正确。这就是为什么我们需要从表2中计算ID。检查计算。它是按照你在问题中提出的那样做的。从表2中选择countid-countid作为id\u count这部分从表2中获取count不是真的,如果你看到group by子句和id\u count,它们是两个不同的结果集。它对你有用吗?我是说在你的解决方案中,表1被使用了两次。是的,我需要使用它,因为我需要从第二个表中减去id总数。如果答案对您有效,请接受。表1仅包含总ID的子集,因此计数将不正确。这就是为什么我们需要从表2中计算ID。检查计算。它正在执行您在问题中提出的操作。从表2中选择countid-countid作为id\u计数这部分从表2中获取计数