用多个case-when语句激发sql


0

我在sparksql中使用我的dataframe创建了一个临时表mydf.createOrReplaceTempView(“combine_table”)。所有字段数据类型都显示为字符串。

    val d1=spark.sql(""" SELECT cast(PV as  FloatType),cast(mxM as FloatType),
    cast(mnM as FloatType ) , cast(procuredValue+ mxM as FloatType) as 1st_case, 
    cast(PV+ mnM as FloatType) as 2nd_case,
    case 
    WHEN 1st_case < price THEN 1st_case
    WHEN 2ndcse < price THEN 2ndcse 
    WHEN PV <price && saleevent = 'Sp' THEN 'price'
    WHEN price < 'PV'  && saleevent = 'Sp' && sclass = 'VH' THEN 0.9* PV
    ELSE PV 
    END AS Final_price 
    FROM combine_table""")

3 答案


0

你的查询怎么了?

SELECT *, 
       CASE 
              WHEN Sum(i.procuredvalue + i.maxmargin) < min_val_seller.q THEN Sum(i.procuredvalue + i.maxmargin)
              WHEN Sum(i.procuredvalue + i.maxmargin) < min_val_seller.q THEN min_val_seller.q 
              WHEN Sum(i.procuredvalue < min_val_seller.q) and e.saleevent = 'Special' THEN min_val_seller.q
              WHEN min_val_seller.q < i.procuredvalue and e.saleevent = 'Special' and Min(min_val_seller.q) and s.netvalue = 'VeryHigh' THEN 0.9*i.procuredvalue
              ELSE i.procuredvalue 
       END AS final_price 
  FROM ecom_competitor_data e, 
       internal_product_data i, 
       min_val_seller, 
       seller_data s 
 WHERE e.productid = i.productid 
   AND s.sellerid = i.sellerid
    SQL格式化程序

0

这么多问题。。。

    不能用双引号包围查询,也不能使用双引号限定查询中的字符串(没有正确的转义)。

-

val myquery = """
select ...
from ...
where ...
"""   

val 1st_case=spark.sql(myquery)

P、 习惯用一个引号来表示SQL字符串。它适用于所有SQL方言,不像双引号。

    'min_val_seller.Q'是字符串文字

0
    val d1=spark.sql(""" SELECT price,PV,
    case
    WHEN cast(PV + mxM as Float) < cast(price as Float) THEN PV + mxM
    WHEN cast(PV + mnM as Float) < cast(price  as Float)THEN PV + mnM
    WHEN cast(PV as Float) < cast(price  as Float) And saleevent =     'Sp' THEN price
    WHEN cast(price as Float) < cast(PV as Float)  And saleevent =   'Sp' And sclass = "VH" THEN 0.9*PV
    ELSE PV
    END AS price
    FROM combine_table""");

谢谢@David上面的查询对我有用。


我来回答

写文章

提问题

面试题