大查询-联接两个表

时间:2020-10-28 00:15:02

标签: sql google-cloud-platform google-bigquery legacy-sql

我们有两个如下表:

表A

 Name | Question  | Answer
 -----+-----------+-------
 Bob  | Interest  | art_and_theatre      
 Sue  | Interest  | finances_and_investments
 Sue  | Interest  | art_and_theatre
 Joe  | Interest  | cooking_and_nutrition
 Joe  | Interest  | nutrition_and_drinks
 Joe  | Interest  | eco_life
 Joe  | Interest  | beauty
 Bob  | Interest  | nutrition_and_drinks

表B(静态)

           Interest                         |   Segment
--------------------------------------------+------------------
art_and_theatre                             |   S1
cooking_and_nutrition, nutrition_and_drinks |   S2 
finances_and_investments                    |   S3
finances_and_investments                    |   S4
technology                                  |   S5
telecommunications                          |   S6
art_and_theatre                             |   S7
art_and_theatre                             |   S8
eco_life, cooking_and_nutrition, beauty     |   S9

期望的表

 Name | Question  | Answer
 -----+-----------+-------
 Bob  | Interest  | art_and_theatre      
 Sue  | Interest  | finances_and_investments
 Sue  | Interest  | art_and_theatre
 Joe  | Interest  | cooking_and_nutrition
 Joe  | Interest  | nutrition_and_drinks 
 Bob  | Interest  | nutrition_and_drinks
          (+)
 Bob  | Segment   | S1
 Bob  | Segment   | S7
 Bob  | Segment   | S8
 Sue  | Segment   | S3
 Sue  | Segment   | S4
 Sue  | Segment   | S1
 Sue  | Segment   | S7
 Sue  | Segment   | S8
 Joe  | Segment   | S2
 Joe  | Segment   | S9

如您所见,一个用户可以有多个兴趣,并且多个兴趣可以属于一个细分。大查询中可以进行这种JOIN吗?

注意:“兴趣”列将包含一个或多个值。仅当所有值都匹配时,才需要连接细分。

3 个答案:

答案 0 :(得分:2)

以下是用于BigQuery标准SQL

#standardSQL
select name, question, answer from `project.dataset.tableA`
union all
select distinct name, 'segment' as question, segment as answer
from (
  select answer, segment 
  from `project.dataset.tableB`, 
  unnest(split(interest, ', ')) answer
)
join `project.dataset.tableA`
using(answer)
-- order by question, name, answer    

如果要应用于您的问题的样本数据-输出为

enter image description here

答案 1 :(得分:0)

嗯。 。 。我在想union alljoin

select a.name, a.question, a.answer
from a
union all
select a.name, 'Segment', b.segment
from a join
     b
     on a.answer = b.interest;

答案 2 :(得分:0)

是可以的,您应该可以使用以下SQL来实现

            app.hnow = this.Height;// size of form displayed now
            app.wnow = this.Width;
            app.module = 2; //indicates look up mode              
            lookprmtby lookby = new lookprmtby(this); instantiate Form2
            app.hprev = lookby.ClientSize.Height; // gets original size of form being called
            app.wprev = lookby.ClientSize.Width;
            lookby.ClientSize = new Size(app.wnow, app.hnow);// set new size before show  
            lookby.WindowState = FormWindowState.Normal;
            lookby.StartPosition = FormStartPosition.Manual;
            // set lookby's location to same location as current form - i.e. left = 0 top = 174
            lookby.Location = this.Location;// this does not work / lookby.location is 0,0 after this statement
            lookby.Left = this.Location.X; // left and top do not work either-they evaluate to 0
            lookby.Top = this.Location.Y;
            lookby.Show();`
相关问题