[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
(
select
camp.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_date
from
(
select
distinct driver_id as driver_id
,group_id
,extractor_name
, '二期新手任務' as user_ltv
from beatles_strategy.mission_publiser_record_view_intern
where datetime='20180619'
and extractor_name='extractor_continuity_20180619'
and task_id in ('90','88','92')
)camp --活動分組資訊
left outer join
(
select
driver_id
from beatles_dwd.dwd_order_arrive_d_view_intern
where (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_id
where camp.driver_id is not null and t.driver_id is null
)camp --剔除了非新手的活動分組資訊
left outer join
(
select
concat_ws('-',year,month,day) as arrive_dt --完單日期
,cast(driver_id as bigint) as driver_id
,cast(order_id as bigint) as order_id
from beatles_dwd.dwd_order_arrive_d_view_intern
where (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
(
select
cast(relative_id as bigint) as order_id --訂單id
,sum(beatles_split) as beatles_split --單個訂單分賬
,max(total) as gmv --單個訂單gmv
from beatles_ods.payment_view_intern
where 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
相關文章
- mysql select欄位別名 不可以在select 或者where中使用 但是group by 與order by可以使用MySql
- sql語句修改欄位型別和增加欄位SQL型別
- SQL Server中獲取資料庫名、表名、欄位名和欄位註釋的SQL語句SQLServer資料庫
- [2020528]寫sql語句不要忘記給欄位加上表別名.txtSQL
- Django-ORM 之指定欄位別名DjangoORM
- mysql order by 和 group by 順序問題MySql
- GORM 如何讀取別名欄位的值GoORM
- 快排實現仿order by多欄位排序排序
- SQL Server 查詢表註釋和欄位SQLServer
- sql小筆記(增刪改查——新增列、修改表名、列的欄位型別等)SQL筆記型別
- 根據欄位查表名
- sql 統計多個欄位的和(如果欄位中含有 null 的處理)SQLNull
- group by 和 order by 一起使用,報錯 ORA-00979:不是 GROUP BY 表示式
- SQL改寫的方法,select group by sumSQL
- select * 和 select 所有欄位的區別
- Oracle 修改欄位型別和長度Oracle型別
- MYSQL SET型別欄位的SQL操作知識介紹MySql型別
- mysql group_concat 實現把分組欄位寫成一行的方法MySql
- SQL Server 別名(as)SQLServer
- DB2_更新SQL欄位DB2SQL
- SQL字元型欄位按數字型欄位排序實現方法SQL字元排序
- 如何利用BAPI SD_SALESDOCUMENT_CHANGE修改Sales Order的欄位API
- SQL中欄位比較型別不匹配錯誤:‘cannot be cast to’SQL型別AST
- SAP CRM Fiori應用如何啟用Sales Office和Sales Group兩個欄位
- SQL基礎 AUTO_INCREMENT 欄位SQLREM
- sql設定欄位預設值SQL
- MongoDB更改欄位型別MongoDB型別
- sql中別名as,不寫,以及使用雙引號總結SQL
- Oracle獲取所有表名資訊和獲取指定表名欄位資訊Oracle
- SQL -去重Group by 和Distinct的效率SQL
- SAP CRM One Order header資料庫表幾個和時間戳相關的欄位Header資料庫時間戳
- MySQL欄位新增註釋,但不改變欄位的型別MySql型別
- SQLServer索引優化(1):對於有order by欄位的建索引策略SQLServer索引優化
- [20201224]order by欄位順序與查詢條件為NULL.txtNull
- SAP WM中階儲存型別裡的Full stk rmvl 欄位和Return Storage type欄位型別
- sqlserver採集欄位的sql語句SQLServer
- ES Mapping ,1 欄位型別APP型別
- MySQL欄位型別最全解析MySql型別