在 bigquery 中交叉应用等效的 MSSQL 运算符

use*_*267 3 sql-server google-bigquery

Sql 服务器语法或 bigquery 语法中是否存在与交叉应用等效的内容。

我需要在没有交叉应用的情况下编写此查询:

   select *
    from t1 
    cross apply (select top 1 col1,col2,col3
    from t2
    where col1 = t1.col1
    and (col2 = '0' or col2 = t1.col2)
and (col3 = '0' or (col3 = left(t1.col3)  and t1.col4 = 'fr'))
    order by col2 desc, col3 desc)
Run Code Online (Sandbox Code Playgroud)

名为“Delivery”的 t1 包含交货信息:

Delivery_ID,Delivery_ShipmentDate,Country_Code,address_ZipCode and the providerservice_id
Run Code Online (Sandbox Code Playgroud)

名为“ProviderService”的 t2 包含有关提供者服务的信息:

Providerservice_id,DCountry_ID , DZone_Code  as well as a numeric ProviderService_DeliveryTime.
Run Code Online (Sandbox Code Playgroud)

因此,每个 Delivery_ID 都有一个 ProviderService_ID 并且对于同一个 Providerservice,我们可以根据此表中的其他列(DCountry_ID 、 DZone_Code)有几个不同的 ProviderService_DeliveryTime

所以最终的查询将是:

    Select t1.Delivery_ShipmentDate,t2.ProviderService_DeliveryTime
    from Delivery t1
    cross apply (select top 1 ProviderService_DeliveryTime,DCountry_ID , DZone_Code 
from ProviderService 
    where ProviderService_id = t1.ProviderService_id
And (DCountry_ID = '0' or DCountry_ID = t1.Country_Code)
And (DZone_Code = '0' or (DZone_Code = left(t1.address_ZipCode,2) and t1.Country_Code='FR'))
        order by t2.DCountry_ID desc, t2.DZone_Code desc)sq
Run Code Online (Sandbox Code Playgroud)

事实上,我需要首先在 sql server 语法中编写相同的查询,然后在 Bigquery 中编写它,因为 BigQuery 无法识别“交叉应用”运算符。

我尝试使用 row_number 窗口函数来完成此操作,但它不起作用:

    with cte as (Select t1.Delivery_ShipmentDate,t2.ProviderService_DeliveryTime,row_number() over (partition by t2.providerservice_id order by t2.DCountry_ID desc, t2.DZone_Code desc) as num
    from Delivery t1
    inner join providerservice t2 on t1.providerservice_id = t2.providerservice_id
And (DCountry_ID = '0' or DCountry_ID = t1.Country_Code)
And (DZone_Code = '0' or (DZone_Code = left(t1.address_ZipCode,2) and t1.Country_Code='FR')))


select * from cte where num = 1
Run Code Online (Sandbox Code Playgroud)

仅当我按 Delivery_id 过滤时才有效:

 with cte as (Select t1.Delivery_ShipmentDate,t2.ProviderService_DeliveryTime,row_number() over (partition by t2.providerservice_id order by t2.DCountry_ID desc, t2.DZone_Code desc) as num
    from Delivery t1
    inner join providerservice t2 on t1.providerservice_id = t2.providerservice_id
And (DCountry_ID = '0' or DCountry_ID = t1.Country_Code)
And (DZone_Code = '0' or (DZone_Code = left(t1.address_ZipCode,2) and t1.Country_Code='FR'))
where delivery_id = xxxx)


select * from cte where num = 1
Run Code Online (Sandbox Code Playgroud)

有人可以帮我吗?谢谢!!

Mik*_*ant 5

以下是 BigQuery 标准 SQL

#standardSQL
SELECT * EXCEPT(pos)
FROM (
  SELECT *
    ROW_NUMBER() OVER(PARTITION BY TO_JSON_STRING(t1) ORDER BY t2.col2 DESC, t2.col3 DESC) pos
  FROM t1 INNER JOIN t2
  ON t2.col1 = t1.col1
  AND (t2.col2 = '0' OR t2.col2 = t1.col2)
  AND (t2.col3 = '0' OR (t2.col3 = left(t1.col3)  AND t1.col4 = 'fr'))
)
WHERE pos = 1
Run Code Online (Sandbox Code Playgroud)

我希望你给我们一些数据来玩 - 但没有它 - 上面只是供你尝试的快速镜头

我对照客户和订单表的经典示例检查了这种方法(在 BigQuery 中实现 CROSS APPLY),并且它有效