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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在bigquery中交叉应用MSSQL运算符等效相关的知识,希望对你有一定的参考价值。

是否有相当于在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)

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,根据此表中的其他列,我们可以有几个不同的ProviderService_DeliveryTime(DCountry_ID,DZone_Code)

所以最终的查询将是:

    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 syntaxe中编写相同的查询,然后在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

有人能帮帮我吗?谢谢!!

答案

相关子查询的工作方式与交叉应用相同吗?

   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)
另一答案

以下是BigQuery Standard 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)与客户和订单表的经典示例,并且它有效

以上是关于在bigquery中交叉应用MSSQL运算符等效的主要内容,如果未能解决你的问题,请参考以下文章

Mysql 是不是与 mssql 中的 @@ROWCOUNT 等效?

MySQL 中基于 MSSQL 语法按 YEAR 选择日期范围的等效语法是啥?

Bigquery 函数中是不是有等效的 pandas split(expand=True) ?

合并语句的 BigQuery 等效项

如何从 MSSQL 连接查询中的字段中获取表名? (mysql_field_table 等效)

BigQuery 中是不是有与标准 SQL 等效的表通配符函数?