hive中case..when和row_number()的使用

本文详细介绍了SQL中Case...When语句的应用,用于列值的条件筛选和转换,以及Row_Number函数的使用,该函数能根据指定列进行分组排序并为每组分配唯一编号,适用于数据分析和报表制作。

一、case..when

将列值进行条件筛选和转换。

select sex,case when sex = 1 then 'man'  
when sex = 2 then 'woman'
when sex = 3 then 'secret'
else 'other' end SEX
from big_data.big_data

结果如下:

将sex列中数据做了相应转换。

二、row_number

row_number() OVER (PARTITION BY COL1 ORDERBY COL2)表示根据COL1分组,在分组内部根据COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(该编号在组内是连续并且唯一的)

select  user uu,id nn,ware hg,row_number() over(partition by user,id 
order by ware)ranking 
from bigdata_data.big_data  where rt = 12131

 

结果如下:

扫描下方二维码关注领取程序员必备千套ppt模板,300本精选好书,丰富面经:

有酒有风

 

SQL报错: 2025-11-13 09:28:40 Error -1 (00000) : Error while executing SQL "/**/ with FH as( SELECT 'Y' AS edw_valid_flag, 'Y' AS edw_data_source, now () AS edw_create_date, now () AS edw_last_update, t2.contract_number, t2.product_type, t2.segment1, t2.description, t2.quantity_issued, t2.salesrep_name, t2.salesdep_name, t2.s40_date, t2.entity_name, t2.prod_line_name, t2.prod_team_name, t2.prod_group_name, t2.prod_class_check_name, t2.project_number, t2.project_name, t2.name, t2.overall_unit_name, t2.device_type, t2.unit_num, t2.dates, t2.yys, t2.overall_unit_name as l5_name, t4.l5_unit, t2.device_type as l4_name, t4.l4_unit, t4.l3_name, t4.l3_unit, t4.l2_name, t4.l2_unit, t4.l1_name, t4.l1_unit FROM ( SELECT contract_number, product_type, segment1, description, quantity_issued, salesrep_name, salesdep_name, s40_date, entity_name, prod_line_name, prod_team_name, prod_group_name, prod_class_check_name, project_number, project_name, name, overall_unit_name, device_type, unit_num, dates, yys FROM dm_plan_shipping_statistics_test2 where s40_date >= CAST( CONCAT (CAST(YEAR (CURRENT_DATE) - 5 AS STRING), '-01-01') AS DATE ) and s40_date <= CURRENT_DATE union all select contract_number_cor as contract_number, master_code as product_type, contract_gbom as segment1, gname as description, scan_qty as quantity_issued, salesrep_name, salesdep_name, issue_date as s40_date, entity_name, prod_line_name, prod_team_name, prod_group_name, prod_class_check_name, project_number, project_name, name, overall_unit_name, device_type, unit_num, dates, CASE WHEN SUBSTR (t3.salesdep_name, 1, 2) = '政企' THEN '政企' WHEN SUBSTR (t3.salesdep_name, 1, 2) = '电源' THEN '其他' WHEN SUBSTR (t3.name, 1, 2) = '中兴' THEN '其他' WHEN SUBSTR (t3.name, 1, 4) = '中国电信' THEN '中国电信' WHEN SUBSTR (t3.name, 1, 4) = '中国联合' THEN '中国联通' WHEN SUBSTR (t3.name, 1, 4) = '中国移动' THEN '中国移动' WHEN SUBSTR (t3.salesdep_name, 1, 2) NOT IN ('第三', '政企') THEN '国际' ELSE '其他' END AS yys from ( select b2b.contract_number_cor, b2b.master_code, b2b.contract_gbom, b2b.gname, b2b.scan_qty, b2b.salesrep_name, b2b.salesdep_name, b2b.issue_date, t1.entity_name, pcn.prod_line_name, pcn.prod_team_name, pcn.prod_group_name, pcn.prod_class_check_name, chc.project_number, cepi.project_name, soe.name, ouh.overall_unit_name, ouh.device_type, oud.unit_num, DATE_FORMAT (CAST(b2b.issue_date AS DATE), '%Y-%m') AS dates from dm_prod_domain_delivery_b2b_qd b2b LEFT join ods_mes_cpm_contract_entities t1 on t1.contract_line_id = b2b.contract_line_id left join ods_srm_op_prodclass_dp pcn on pcn.prod_class_name = b2b.master_code left join ods_mes_cdm_contract_lines ccl on b2b.contract_line_id = ccl.contract_line_id left join ods_mes_cdm_contract_headers cch on ccl.contract_header_id = cch.contract_header_id right join ods_cms_cdm_contract_baseinfo ccb on cch.contract_baseinfo_id = ccb.contract_baseinfo_id RIGHT JOIN ods_cms_cdm_hc_contracts chc ON ccb.hc_contract_id = chc.hc_contract_id LEFT JOIN ods_cms_cdm_ecc_project_info cepi ON cepi.project_number = chc.project_number LEFT JOIN ods_msm_pm_proj proj ON proj.proj_num = chc.project_number LEFT JOIN ods_scrm_pm_proj_customer pc ON pc.proj_id = proj.proj_id LEFT JOIN ods_scrm_s_org_ext soe ON soe.ou_num = pc.acnt_id LEFT JOIN ods_scrm_s_org_ext_x t2 ON soe.row_id = t2.row_id LEFT join ods_scp_ba_overall_unit_detail oud on oud.item_no = b2b.contract_gbom LEFT join ods_scp_ba_overall_unit_head ouh on ouh.head_id = oud.head_id where b2b.contract_line_id = ccl.contract_line_id and b2b.meaning = '发货完成' and ouh.enabled = 1 and oud.enabled = 1 and COALESCE(b2b.scan_qty, 0) != 0 and b2b.issue_date >= CAST( CONCAT (CAST(YEAR (CURRENT_DATE) - 5 AS STRING), '-01-01') AS DATE ) and b2b.issue_date <= CURRENT_DATE ) as t3 ) t2 left join ( SELECT oud.item_no, ouh.overall_unit_name AS tag_name, spt.tag_type, spt.tag_item_type, ouh.overall_unit_name AS l5_name, pm5.l5_unit, ouh.device_type AS l4_name, pm5.l4_unit, opm.product_model_class AS l3_name, pm5.l3_unit, pm5.l2_name, pm5.l2_unit, pm5.l1_name, pm5.l1_unit, ouh.prod_class, pcn.prod_class_check_name, pm5.prod_team_name, pm5.prod_line_name FROM ods_scp_ba_overall_unit_detail oud LEFT JOIN ods_scp_ba_overall_unit_head ouh ON ouh.head_id = oud.head_id AND ouh.enabled = 1 LEFT JOIN ods_scp_sop_product_tag spt ON spt.tag_id = ouh.product_tag_id AND spt.valid_flag = 'Y' LEFT JOIN ods_scp_aps_product_model opm ON opm.product_model_no = ouh.device_code AND opm.data_losed = 0 LEFT JOIN ( SELECT DISTINCT pd.pdm_prod_class_name, pd.prod_class_check_name FROM ods_srm_op_prodclass_dp pd WHERE pd.enabled_flag = 1 ) pcn ON pcn.pdm_prod_class_name = ouh.prod_class LEFT JOIN dm_product_model_5level pm5 ON pm5.prod_class_check_name = pcn.prod_class_check_name AND pm5.l3_name = opm.product_model_class WHERE ouh.enabled = 1 ) t4 ON t4.item_no = t2.segment1 and t4.prod_class_check_name = t2.prod_class_check_name ) select t5.edw_valid_flag, t5.edw_data_source, t5.edw_create_date, t5.edw_last_update, t5.contract_number, t5.product_type, t5.segment1, t5.description, t5.quantity_issued, t5.salesrep_name, t5.salesdep_name, t5.s40_date, t5.entity_name, t5.prod_line_name, t5.prod_team_name, t5.prod_group_name, t5.prod_class_check_name, t5.project_number, t5.project_name, t5.name, t5.overall_unit_name, t5.device_type, t5.unit_num, t5.dates, t5.yys, t5.l5_name, t5.l5_unit, t5.l4_name, t5.l4_unit, t5.l3_name, t5.l3_unit, t5.l2_name, t5.l2_unit, t5.l1_name, t5.l1_unit from FH t5": Remote driver error: OlapException: Query failed (#20251113_012840_07516_iezz7): line 3:6: Schema must be specified when session schema is not set -> SQLException: Query failed (#20251113_012840_07516_iezz7): line 3:6: Schema must be specified when session schema is not set -> FailureException: line 3:6: Schema must be specified when session schema is not set
11-14
mismatched input 'repurchase' expecting {<EOF>, ';'}(line 59, pos 8) == SQL == create table orca01_dr_data.ads_rpt_mini_loyalty_customer_t_1124 stored as parquet as with repurchase as( select t1.re_no, concat(t2.last_name,t2.first_name) as customer_full_name, t1.new_vin_17, t1.brand_name, t1.re_status, t1.create_date, case when count(distinct vin_17) < count(vin_17) then '是' else '否' end as is_vin_17_duplicate, case when t1.category = '公司购车' then t1.company_name else null end as company_name_result from dwc.dwc_dim_com_membership2_bz_repurchase_full_t t1 left join dwc_dim_cus_membership2_customer_full_t t2 on t1.cop_id = t2.cop_id where t1.brand_name = 'mini' and t1.create_date >= '2025-10-01' ), ordercenter as ( select a1.paid_amount, vin.status as order_status ,vin.vin_17 as vin_17 ,concat(t4.last_name,t4.first_name) as concat_name ,concat(vin.vin_17,t2.dealer_code) as concat_vin ,t4.name as customer_full_name from dwc.dwc_fact_sal_ordercenter_core_order_full_t t1 left join dwc_fact_sal_ordercenter_payment_full_t a1 on t1.order_no = a1.order_no left join dwc.dwc_fact_sal_ordercenter_vehicle_fulfillment_full_t vin on t1.order_no=vin.order_no left join dwc.dwc_fact_sal_ordercenter_customer_full_t t4 on t1.order_no=t4.order_no and t1.cid=t4.cid and t4.type='vehicle_owner' and t4.deleted != 0 left join (select order_no,create_date,status,row_number() over(partition by order_no order by create_date asc nulls last ) as rk from dwc.dwc_fact_com_ordercenter_core_order_log_full_t where type = 'payment' ) log1 on t1.order_no=log1.order_no and log1.rk=1 where t1.business_type='nc' and t1.deleted != 0 ) select distinct t1.re_no, customer_full_name, t1.new_vin_17, t1.brand_name, t1.re_status, t1.create_date, is_vin_17_duplicate, company_name_result, a1.paid_amount, order_status, from t1 repurchase --------^^^ left join t2 ordercenter on t1.new_vin_17 = t2.vin_17 执行的SQL语句: ```sql create table orca01_dr_data.ads_rpt_mini_loyalty_customer_t_1124 stored as parquet as with repurchase as( select t1.re_no, concat(t2.last_name,t2.first_name) as customer_full_name, t1.new_vin_17, t1.brand_name, t1.re_status, t1.create_date, CASE WHEN COUNT(DISTINCT vin_17) < COUNT(vin_17) THEN '是' ELSE '否' END AS is_vin_17_duplicate, CASE WHEN t1.category = '公司购车' THEN t1.company_name ELSE NULL END AS company_name_result from dwc.dwc_dim_com_membership2_bz_repurchase_full_t t1 left join dwc_dim_cus_membership2_customer_full_t t2 on t1.cop_id = t2.cop_id where t1.brand_name = 'MINI' and t1.create_date >= '2025-10-01' ), ordercenter as ( select a1.paid_amount, vin.status as order_status ,vin.vin_17 as vin_17 ,concat(t4.last_name,t4.first_name) as concat_name ,concat(vin.vin_17,t2.dealer_code) as concat_vin ,t4.name as customer_full_name from dwc.dwc_fact_sal_ordercenter_core_order_full_t t1 left join dwc_fact_sal_ordercenter_payment_full_t a1 on t1.order_no = a1.order_no left join dwc.dwc_fact_sal_ordercenter_vehicle_fulfillment_full_t vin on t1.order_no=vin.order_no left join dwc.dwc_fact_sal_ordercenter_customer_full_t t4 on t1.order_no=t4.order_no and t1.cid=t4.cid and t4.type='VEHICLE_OWNER' and t4.deleted != 0 left join (select order_no,create_date,status,row_number() over(partition by order_no order by create_date asc nulls last ) as rk from dwc.dwc_fact_com_ordercenter_core_order_log_full_t where type = 'PAYMENT' ) log1 on t1.order_no=log1.order_no and log1.rk=1 where t1.business_type='NC' and t1.deleted != 0 ) select distinct t1.re_no, customer_full_name, t1.new_vin_17, t1.brand_name, t1.re_status, t1.create_date, is_vin_17_duplicate, company_name_result, a1.paid_amount, order_status, from t1 repurchase left join t2 ordercenter on t1.new_vin_17 = t2.vin_17
最新发布
11-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值