【问题标题】:Merge date columns in different tables合并不同表中的日期列
【发布时间】:2018-08-15 17:06:17
【问题描述】:

我正在尝试合并月份列并连接来自两个不同表的数据。我已经尝试过 union all 和 full join,但结果并没有达到我的预期。我想要 1 个月的专栏和其他 4 列(它们是否有空值无关紧要)非常感谢!

这是我想要的结果:

month    | new_placements | new_mrr | exits | lost_mrr
20190101 | null           | null    | 8     | 19900
20181101 | 144            | 148000  | null  | null

从这两个表中:

table1

month1   | new_placements | new_mrr 
20181101 | 144            | 148000

table2

month    | exits          | lost_mrr
20190101 | 8              | 19900

【问题讨论】:

    标签: mysql google-bigquery bigquery-standard-sql


    【解决方案1】:

    规范一点都不清楚。

    以下查询将满足规范的一种可能解释:

    SELECT n.month1                AS month_column
         , n.new_placements        AS new_placements
         , n.new_mrr               AS new_mrr
         , ''                      AS exits
         , ''                      AS lost_mrr
      FROM table1 n
     UNION ALL
    SELECT o.month                 AS month_column
         , ''                      AS new_placements
         , ''                      AS new_mrr
         , o.exits                 AS exits
         , o.lost_mrr              AS lost_mrr
      FROM table2 o
    

    --

    编辑

    使用问题中显示的数据(左对齐),值似乎是字符串而不是数值。数值将是正确的。理想情况下,我们应该知道列的数据类型,最好是两个表的实际定义。我们可以创建和填充上面的查询不会引发错误的示例表。)

    使用UNION ALL 集合运算符,每个集合必须具有相同数量的列,并且每个列位置需要具有相同(或兼容)的数据类型。

    SELECT n.month1                AS month_column
         , n.new_placements        AS new_placements
         , n.new_mrr               AS new_mrr
         , NULL                    AS exits
         , NULL                    AS lost_mrr
      FROM table1 n
     UNION ALL
    SELECT o.month                 AS month_column
         , NULL                    AS new_placements
         , NULL                    AS new_mrr
         , o.exits                 AS exits
         , o.lost_mrr              AS lost_mrr
      FROM table2 o
    

    【讨论】:

    • 抱歉给您带来了困惑。两个表中实际上都没有空值。为了更清楚,我编辑了帖子。我是一个新的 sql 学习者,非常感谢您的帮助。我会试试你的解决方案。
    • 当我尝试运行它时,UNION ALL 中的第 2 列有不兼容的类型:INT64,STRING at [34:1]
    • 这就是为什么您需要描述实际结构的原因。你为什么在工会中使用“空字符串”。让空值成为空值。此外,您只需要并集第一部分的别名。您还需要最大化外部选择中的列以展平结构。
    • @TGray:如果我们想折叠行,是的,我们可以将 UNION ALL 查询包装在括号中,并将其作为内联视图引用。外部查询可以使用 GROUP BY 子句并在 SELECT 列表中使用聚合函数。这一切都是真的。但是我们是否“需要”这样做,这取决于实际的规范。
    • @TGray:在 UNION ALL 查询中,列的名称由第一个查询确定。所以,是的,第二个查询中的列别名被忽略了。但是,当仅在 UNION ALL 中运行其中一个查询时,这些可以用作文档,并有助于调试。在第二个查询中包含列别名并不是问题。
    【解决方案2】:
    SELECT month_column
         , max(new_placements) as new_placements
         , max(new_mrr) as new_mrr
         , max(exits) as exits
         , max(lost_mrr) as lost_mrr
      from (SELECT n.month1                AS month_column
                 , n.new_placements        AS new_placements
                 , n.new_mrr               AS new_mrr
                 , null                    AS exits
                 , null                    AS lost_mrr
              FROM table1 n
            UNION ALL
            SELECT o.month 
                 , null 
                 , null
                 , o.exits
                 , o.lost_mrr
              FROM table2 o) as a
    GROUP BY month_column;
    

    【讨论】:

      猜你喜欢
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      • 2013-06-28
      • 1970-01-01
      • 2022-08-12
      • 1970-01-01
      相关资源
      最近更新 更多