【问题标题】:Cross apply MSSQL operator equivalent in bigquery在 bigquery 中交叉应用等效的 MSSQL 运算符
【发布时间】:2019-03-27 07:34:16
【问题描述】:

在 Sql server 语法或 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)

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

Delivery_ID,Delivery_ShipmentDate,Country_Code,address_ZipCode and the providerservice_id

名为'ProviderService'的t2包含有关providerservice的信息:

Providerservice_id,DCountry_ID , DZone_Code  as well as a numeric ProviderService_DeliveryTime.

所以每个 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

事实上,我需要先在 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

仅当我按 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

谁能帮帮我?谢谢!!

【问题讨论】:

  • cross apply 是 Microsoft 对标准 cross join lateral 的实现 - 也许适用于 BigQuery?
  • 不,很遗憾,横向交叉连接在 BigQuery 中不起作用

标签: sql-server google-bigquery


【解决方案1】:

以下是 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

我希望你给我们一些数据来玩 - 但是没有它 - 以上只是你尝试的快速拍摄

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

【讨论】:

  • 你有机会尝试吗?
  • 我用row_number窗口函数成功了!!谢谢米海尔
【解决方案2】:

相关子查询是否与交叉应用一样工作?

   select *
    from t1 , (select 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
    limit 1)

【讨论】:

  • 云,我用row_number窗口函数成功了..非常感谢
猜你喜欢
  • 1970-01-01
  • 2013-01-03
  • 1970-01-01
  • 2014-08-21
  • 2020-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多