如何执行LIKE函数引用另一列?

时间:2019-05-08 14:15:36

标签: google-bigquery

所以我的问题是,我正在使用的三个表Table A / Table B / Table CTable A & Table B内都有数字机制。因此,假设Table A是支出,Table B是收入。 Table C包含视频游戏的名称以及其使用的辅助名称。我想做的是能够同时显示视频游戏名称和辅助视频游戏名称。

我试图做类似的事情,

a.Name LIKE c.Name_Secondary

但这仅显示a.Name个结果。这些表如下所示:

Table A
Name           |    Revenue
TW WH2         |    30.00
Battlefield    |    20.00
TW WH2         |    15.00
Battlefield    |     5.00
T2 DLC 18674   |     5.00
B DLC 57948    |    10.00
T2 DLC 18674   |     5.00
B DLC 57948    |     5.00

Table B
Name           |    Spend
TW WH2         |    60.00
Battlefield    |    40.00
TW WH2         |    40.00
Battlefield    |    20.00
T2 DLC 18674   |     9.00
B DLC 57948    |    15.00
T2 DLC 18674   |    10.00
B DLC 57948    |     9.00    

Table C        
Name           |    Name_Secondary
TW WH2         |    T2 DLC 18674
Battlefield    |    B DLC 57948

我当前的查询如下:

SELECT DISTINCT
  a.Name,
  SUM(b.Spend),
  SUM(a.Revenue)
FROM
  Example.Table_A a

INNER JOIN
  (
    SELECT
      Name,
      SUM(Spend) AS Spend
    FROM
      Example.Table_B
    GROUP BY 1) b
ON
 a.Name = b.Name

INNER JOIN
  Example.Table_C c
ON
 a.Name = b.Name

GROUP BY
  1

我尝试将Name_Secondary添加到第一个SELECT中,

SELECT DISTINCT
  a.Name,
  c.Name_Secondary
  SUM(b.Spend),
  SUM(a.Revenue)
FROM
  Example.Table_A a

但是我得到的结果仅来自a.Name,我得到的结果如下:

Name         |      Name_Secondary    |    Spend     |    Revenue
TW WH2       |      T2 DLC 18674      |   100.00     |     45.00
Battlefield  |      B DLC 57948       |    60.00     |     25.00

我还尝试将其添加到WHERE函数中,例如:

WHERE
 a.Name LIKE c.Name_Secondary
GROUP BY
  1

但是在没有插入Name_Secondary列的情况下显示了与上述相同的结果。

我希望显示的结果是:

Name         |     Spend     |   Revenue
TW WH2       |     118.00    |    55.00
Battlefield  |      84.00    |    40.00

这有可能吗?还是会变成这样:

WHERE
 (a.Name LIKE '%TW%'
OR a.Name LIKE '%Batt%'
OR a.Name LIKE '%T2_DLC_18674%'
OR a.Name LIKE '%B_DLC_57948%')
GROUP BY
  1

任何帮助都是很好的,如果需要进一步说明,我当然可以在“编辑”中添加更多信息。

-Maykid

EDIT1

我还要澄清一下,TableC只是具有Names且仅包含Name_Secondary的查找表。因此,对于TableATableC,它们将具有数百个名称,而TableC仅具有几个名称(20-30)。例如:

Table A 
Name
TH WH2
Battlefield
TH WH
Satisfactory
Starcraft II

Table C
Name              |    Name_Secondary
TW WH2            |    T2 DLC 18674
Battlefield       |    B DLC 57948

为此您可以看到,Table C仅在列出的5个Names中只有2个,因为只有2个具有Name_Secondary。很抱歉给您带来的困惑,还没弄清楚!

1 个答案:

答案 0 :(得分:2)

以下是用于BigQuery标准SQL

#standardSQL
WITH flattened_tableC AS (
  SELECT Name, Name AS join_value FROM `project.dataset.tableC` c UNION ALL
  SELECT Name, Name_Secondary AS join_value FROM `project.dataset.tableC` c 
), revenues AS (
  SELECT c.Name, SUM(Revenue) AS Revenue
  FROM flattened_tableC c
  LEFT JOIN `project.dataset.tableA` a ON c.join_value = a.Name
  GROUP BY 1 
), spending AS (
  SELECT c.Name, SUM(Spend) AS Spend
  FROM flattened_tableC c
  LEFT JOIN `project.dataset.tableB` b ON c.join_value = b.Name
  GROUP BY 1 
)
SELECT Name, Spend, Revenue 
FROM spending 
FULL OUTER JOIN revenues 
USING (Name)  

如果要应用于问题的样本数据-上面的查询会产生下面的结果

Row Name        Spend   Revenue  
1   TW WH2      119.0   55.0     
2   Battlefield 84.0    40.0