Bigquery unnest 最小行数

Bigquery unnest minimum number of rows

我正在尝试编写一个带有限制的非嵌套查询。当我使用 unnest 时,它会为我提供其他列的重复条目,这是预期的,但是有没有一种方法可以指定我希望特定列至少有 10 个唯一记录?

这是我的示例数据和查询

food.id                     STRING  NULLABLE    
food.basket.id              STRING  NULLABLE
food.foodType               STRING  NULLABLE
food.price                  INTEGER NULLABLE    
food.printed                BOOLEAN NULLABLE    
food.variations             RECORD  REPEATED    
food.variations.id          INTEGER REPEATED    
food.variations.amount      INTEGER NULLABLE

示例数据

id     basket.id.    foodType.    price.    printed.    variations.id      variations.amount
1.      abbcd.         JUNK.       100.       TRUE.        1234.            10
                                                           2345.            20
2.      cdefg.         PIZZA.      200.       TRUE.        1234.            10
                                                           2345.            20
                                                           5678.            20
                                                           7816.            40
3.      pqrst.         JUNK.       200.       FALSE.       1234.            10
                                                           2345.            20
                                                           5678.            20
                                                           7816.            40
4.      uiwka.         TOAST.      500.       FALSE.       1234.            10
                                                           9898.            20
                                                           5678.            20
                                                           7816.            40

这是我的示例查询:

select basket.id as bId, foodType, price, printed, v.id as vId, v.amount as vAmount from my_table_name left join UNNEST (variations) as v limit 10;

这是结果:

id       bId.        foodType.    price.    printed.       vId.           vAmount
1.      abbcd.         JUNK.       100.       TRUE.        1234.            10
2.      abbcd.         JUNK.       100.       TRUE.        2345.            20
3.      cdefg.         PIZZA.      200.       TRUE.        1234.            10
4.      cdefg.         PIZZA.      200.       TRUE.        2345.            20
5.      cdefg.         PIZZA.      200.       TRUE.        5678.            20
6.      cdefg.         PIZZA.      200.       TRUE.        7816.            40
7.      pqrst.         JUNK.       200.       FALSE.       1234.            10
8.      pqrst.         JUNK.       200.       FALSE.       2345.            20
9.      pqrst.         JUNK.       200.       FALSE.       5678.            20
10.     pqrst.         JUNK.       200.       FALSE.       7816.            40

所以现在我的带有 bId uiwka 的行丢失了。 所以我正在尝试编写一个查询,该查询将限制我返回的唯一 bId 的数量。

您只需将 LIMIT 向上移动一点 - 如下例

#standardSQL
SELECT 
  basket.id AS bId, 
  foodType, price, 
  printed, 
  v.id AS vId, 
  v.amount AS vAmount 
FROM (SELECT * FROM `project.dataset.table` LIMIT 10)
LEFT JOIN UNNEST (variations) AS v