[SQL]group by和order by是否能写字段别名

2024-04-16 09:08

本文主要是介绍[SQL]group by和order by是否能写字段别名,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、错误实例

--剔除污染之后每一组人数不同
select camp.group_id as group_id --实验分组,int((datediff(arrive.arrive_dt,'2018-06-20')-1)/7)+1 as ob_week --观察周,count(distinct camp.driver_id) as group_cnt --组人数,count(distinct arrive.order_id) as arrive_cnt --完单数,sum(beatles_split) as beatles_split --分账,sum(gmv) as gmv --gmv
from(selectcamp.driver_id as driver_id,camp.group_id as group_id,camp.extractor_name as extractor_name,camp.user_ltv as user_ltv,'2018-06-20' as start_datefrom( select  distinct driver_id as driver_id,group_id,extractor_name, '二期新手任务' as user_ltvfrom beatles_strategy.mission_publiser_record_view_internwhere datetime='20180619'and extractor_name='extractor_continuity_20180619'and task_id in ('90','88','92'))camp --活动分组信息left outer join (select driver_idfrom beatles_dwd.dwd_order_arrive_d_view_internwhere (is_test!=1 or is_test is null)--and status in (2,3,4,5,6,13,31)and concat_ws('-',year,month,day)  between '2018-06-15' and  '2018-06-20' and (to_date(arrive_time)) between '2018-06-15' and  '2018-06-20'group by driver_id)t  --剔除非新手的车主on camp.driver_id = t.driver_idwhere camp.driver_id is not null and t.driver_id is null)camp --剔除了非新手的活动分组信息
left outer join(selectconcat_ws('-',year,month,day) as arrive_dt --完单日期,cast(driver_id as bigint) as driver_id,cast(order_id as bigint) as order_idfrom beatles_dwd.dwd_order_arrive_d_view_internwhere (is_test!=1 or is_test is null)and concat_ws('-',year,month,day) between '2018-06-20' and '${end_date}'group by cast(driver_id as bigint),cast(order_id as bigint),concat_ws('-',year,month,day))arrive --完单情况
on camp.driver_id=arrive.driver_id
left outer join (selectcast(relative_id as bigint) as order_id --订单id,sum(beatles_split) as beatles_split  --单个订单分账,max(total) as gmv --单个订单gmvfrom beatles_ods.payment_view_internwhere concat_ws('-',year,month,day) between '2018-06-20' and '${end_date}'group by cast(relative_id as bigint))beatles_split
on arrive.order_id=beatles_split.order_id
group by group_id,int((datediff(arrive.arrive_dt,'2018-06-20')-1)/7)+1
order by group_id,int((datediff(arrive.arrive_dt,'2018-06-20')-1)/7)+1

 

这样在hive下是解释不通的,回顾一下SQL的执行顺序https://blog.csdn.net/TOMOCAT/article/details/81586789。order by是在SQL的最后执行的,从而应该改成:

二、修改:

 

--剔除污染之后每一组人数不同
select camp.group_id as group_id --实验分组,int((datediff(arrive.arrive_dt,'2018-06-20')-1)/7)+1 as ob_week --观察周,count(distinct camp.driver_id) as group_cnt --组人数,count(distinct arrive.order_id) as arrive_cnt --完单数,sum(beatles_split) as beatles_split --分账,sum(gmv) as gmv --gmv
from
--skip--
group by group_id,int((datediff(arrive.arrive_dt,'2018-06-20')-1)/7)+1
order by group_id,ob_week

 

这篇关于[SQL]group by和order by是否能写字段别名的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/908363

相关文章

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

MySQL 衍生表(Derived Tables)的使用

《MySQL衍生表(DerivedTables)的使用》本文主要介绍了MySQL衍生表(DerivedTables)的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学... 目录一、衍生表简介1.1 衍生表基本用法1.2 自定义列名1.3 衍生表的局限在SQL的查询语句select