嘗試使用BigQuery的標準SQL
#standardSQL
WITH segments AS (
SELECT ProductID, MAX(ProdSegID) AS ProdSegIDCalc
FROM yourTable
GROUP BY ProductID
)
SELECT t.ProductID, t.ProdSegID, s.ProdSegIDCalc
FROM yourTable AS t
JOIN segments AS s
ON t.ProductID = s.ProductID
ORDER BY ProductID
您可以測試/使用其虛擬數據從你的問題發揮:
#standardSQL
WITH yourTable AS (
SELECT 1 AS ProductID, 1 AS ProdSegID UNION ALL
SELECT 1, NULL UNION ALL
SELECT 1, NULL UNION ALL
SELECT 2, NULL UNION ALL
SELECT 2, 5 UNION ALL
SELECT 2, 5 UNION ALL
SELECT 3, NULL UNION ALL
SELECT 3, NULL UNION ALL
SELECT 3, 18
),
segments AS (
SELECT ProductID, MAX(ProdSegID) AS ProdSegIDCalc
FROM yourTable
GROUP BY ProductID
)
SELECT t.ProductID, t.ProdSegID, s.ProdSegIDCalc
FROM yourTable AS t
JOIN segments AS s
ON t.ProductID = s.ProductID
ORDER BY ProductID
注:個人我 - 我會去使用Analytic Functions
,正如Gordon的答案中所建議的那樣。但想給你更多的選擇 - 取決於你的實際使用情況,它可能或多或少有用
謝謝大家的幫助。戈登的解決方案爲我工作得很好。 – Albertok
親切的問候 Albrecht – Albertok