分解表中两列的所有可能组合

问题描述 投票:0回答:1

我有一张桌子:

ID      POC     SID      VALUE
===============================
50      poc1     1         2

51      poc1     1         3
51      poc1     2         1
51      poc2    (null)   (null)

52      poc1    (null)   (null)
52      poc2    (null)   (null) 

53      poc4     1         1
53      poc5     2         2

我希望结果表如下所示,其中每个

(POC, SID)
都有
ID
的所有可能组合:

ID      POC     SID      VALUE
===============================
50      poc1    1          2

51      poc1    1          3
51      poc2    1        (null)
51      poc1    2          1
51      poc2    2        (null)

52      poc1    (null)   (null)
52      poc2    (null)   (null)

53      poc4    1          1
53      poc5    1        (null)  
53      poc4    2        (null)  
53      poc5    2          2  

要得到上述结果的 SQL 查询是什么?

sql google-bigquery
1个回答
0
投票

您还需要另外两张表,一张包含所有

ID
值,一张包含所有
POC

交叉连接它们,然后左连接这个主表。

SELECT
  i.ID,
  p.POC,
  t.SID,
  t.Value
FROM AllId i
CROSS JOIN AllPoc p
LEFT JOIN YourTable t
  ON t.ID = i.ID
 AND t.POC = p.POC;
© www.soinside.com 2019 - 2024. All rights reserved.