新列的多个BigQuery子选择

时间:2016-06-16 21:27:24

标签: google-bigquery

我有一张表与一对多商业关系的表格'由于多个行业代码与给定邮政编码的业务相匹配,因此将其命名为邮政编码。一个单独的表格按邮政编码包含住户。要将邮政编码作为行和给定业务作为列的家庭进行求和,同时对多行中匹配相同邮政编码的家庭进行重复数据删除(以避免过多计算家庭),我查询

SELECT ZIPCode, SUM(SumHouseholds1) AS Company1  
FROM (  
    SELECT ZIPCode, SUM(Households) OVER (PARTITION BY ZIPCode, DBAName) AS SumHouseholds1  
    FROM Business  
    JOIN Location  
    ON Location.ZIPCode = Business.ZIPCode  
    WHERE DBAName='Company1'  
GROUP BY DBAName, ZIPCode, Households)  
GROUP BY ZIPCode  

输出如下:

ZIPCode Company1
    10001 17007
    10003 54084

当我尝试将其他列(Company2,Company3等)添加到原始SELECT语句时:

SELECT ZIPCode, SUM(SumHouseholds1) AS Company1  
FROM (  
    SELECT ZIPCode, SUM(Households) OVER (PARTITION BY ZIPCode, DBAName) AS SumHouseholds1  
    FROM Business  
    JOIN Location  
    ON Location.ZIPCode = Business.ZIPCode  
    WHERE DBAName='Company1'  
GROUP BY DBAName, ZIPCode, Households),  
SUM(SumHouseholds2) AS Company2  
FROM (  
    SELECT ZIPCode, SUM(Households) OVER (PARTITION BY ZIPCode, DBAName) AS SumHouseholds2  
    FROM Business  
    JOIN Location  
    ON Location.ZIPCode = Business.ZIPCode  
    WHERE DBAName='Company2'  
GROUP BY DBAName, ZIPCode, Households)
GROUP BY ZIPCode 

我遇到了一个遭遇" " FROM" " FROM""错误。

1 个答案:

答案 0 :(得分:1)

好的,假设您的初始代码真的适合您 - 下面将解决第二个查询的问题

public class MultipleVideoPlayActivity extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

    private static final String TAG = "MediaPlayer";
    private static final int[] SURFACE_RES_IDS = { R.id.video_1_surfaceview, R.id.video_2_surfaceview };

    private MediaPlayer[] mMediaPlayers = new MediaPlayer[SURFACE_RES_IDS.length];
    private SurfaceView[] mSurfaceViews = new SurfaceView[SURFACE_RES_IDS.length];
    private SurfaceHolder[] mSurfaceHolders = new SurfaceHolder[SURFACE_RES_IDS.length];
    private boolean[] mSizeKnown = new boolean[SURFACE_RES_IDS.length];
    private boolean[] mVideoReady = new boolean[SURFACE_RES_IDS.length];
    int i = 0; // index of video playout

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
         setContentView(R.layout.multi_videos_layout);

        // create surface holders
        for (int i = 0; i < mSurfaceViews.length; i++) {
            mSurfaceViews[i] = (SurfaceView) findViewById(SURFACE_RES_IDS[i]);
            mSurfaceHolders[i] = mSurfaceViews[i].getHolder();
            mSurfaceHolders[i].addCallback(this);
            mSurfaceHolders[i].setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
    }

但是,即使现在它有效(我希望,因为我根本没有测试过它)它太沉重而且难以管理 下面解决了这个问题(仍然没有经过测试但应该起作用,至少应该给你一个想法)

SELECT 
  c1.ZIPCode AS ZIPCode, 
  c1.Company1 AS Company1, 
  c2.Company2 AS Company2, 
  c3.Company3 AS Company3
FROM (
  SELECT ZIPCode, SUM(SumHouseholds1) AS Company1  
  FROM (  
      SELECT ZIPCode, SUM(Households) OVER (PARTITION BY ZIPCode, DBAName) AS SumHouseholds1  
      FROM Business  
      JOIN Location  
      ON Location.ZIPCode = Business.ZIPCode  
      WHERE DBAName='Company1'  
  GROUP BY DBAName, ZIPCode, Households)
) AS c1
JOIN (
  SELECT ZIPCode, SUM(SumHouseholds2) AS Company2  
  FROM (  
      SELECT ZIPCode, SUM(Households) OVER (PARTITION BY ZIPCode, DBAName) AS SumHouseholds2  
      FROM Business  
      JOIN Location  
      ON Location.ZIPCode = Business.ZIPCode  
      WHERE DBAName='Company2'  
  GROUP BY DBAName, ZIPCode, Households)
) AS c2
ON c1.ZIPCode = c2.ZIPCode
JOIN (
  SELECT ZIPCode, SUM(SumHouseholds3) AS Company3  
  FROM (  
      SELECT ZIPCode, SUM(Households) OVER (PARTITION BY ZIPCode, DBAName) AS SumHouseholds3  
      FROM Business  
      JOIN Location  
      ON Location.ZIPCode = Business.ZIPCode  
      WHERE DBAName='Company3'  
  GROUP BY DBAName, ZIPCode, Households)
) AS c3
ON c1.ZIPCode = c3.ZIPCode
  

2016年7月12日更新基于评论中的更多信息

SELECT
  ZIPCode,
  SUM(CASE WHEN DBAName='Company1' THEN Company ELSE 0 END) AS Company1,
  SUM(CASE WHEN DBAName='Company2' THEN Company ELSE 0 END) AS Company2,
  SUM(CASE WHEN DBAName='Company3' THEN Company ELSE 0 END) AS Company3
FROM (
  SELECT ZIPCode, DBAName, SUM(SumHouseholds1) AS Company
  FROM (  
      SELECT ZIPCode, SUM(Households) OVER (PARTITION BY ZIPCode, DBAName) AS SumHouseholds  
      FROM Business  
      JOIN Location  
      ON Location.ZIPCode = Business.ZIPCode  
  GROUP BY DBAName, ZIPCode, Households)
)
GROUP BY ZIPCode

输出

SELECT
 ZIPCode,
 SUM(CASE WHEN DBAName='Company1' THEN Company ELSE 0 END) AS Company1,
 SUM(CASE WHEN DBAName='Company2' THEN Company ELSE 0 END) AS Company2,
 SUM(CASE WHEN DBAName='Company3' THEN Company ELSE 0 END) AS Company3
FROM (
 SELECT ZIPCode, DBAName, SUM(SumHouseholds) AS Company
 FROM (  
    SELECT ZIPCode, DBAName, SUM(Households) OVER (PARTITION BY ZIPCode, DBAName) AS SumHouseholds  
    FROM Business  
    JOIN Location  
    ON Location.ZIPCode = Business.Market  
    GROUP BY DBAName, ZIPCode, Households
 )
 GROUP BY DBAName, ZIPCode
)
GROUP BY ZIPCode
  

进一步的想法

以上&#34;修复&#34;仍然完全依赖于你的逻辑是正确的假设。

我确实填充它不是:

我认为以下调整可以纠正:

首先 - 家庭分组看起来非常可疑,在审核了你的笔记之后,我认为你需要在下面

ZIPCode Company1    Company2    Company3     
10001    5           5           5   
10016    8           8           8   
12345   17          17          17   
16420   10           0           0   

反过来 - 可以进一步简化为

SELECT
  ZIPCode,
  SUM(CASE WHEN DBAName='Company1' THEN Company ELSE 0 END) AS Company1,
  SUM(CASE WHEN DBAName='Company2' THEN Company ELSE 0 END) AS Company2,
  SUM(CASE WHEN DBAName='Company3' THEN Company ELSE 0 END) AS Company3
FROM (  
  SELECT ZIPCode, DBAName, SUM(Households) AS Company  
  FROM (
    SELECT Market, DBAName 
    FROM AS Business 
    GROUP BY Market, DBAName
  ) AS Business
  JOIN Location  
  ON Location.ZIPCode = Business.Market  
  GROUP BY DBAName, ZIPCode
)
GROUP BY ZIPCode
  

不知怎的,我觉得 - 最后一个问题是你在找什么!

但是我仍然不知道你真实的一些细节 - 最可能更复杂 - 用例,所以在这种情况下你的原始逻辑可能是正确的