查询最后一次消费记录
/*
查詢最后一次消費記錄
表結(jié)構(gòu)如下
create table TESTSHIC
(
? NAME??????? VARCHAR2(50),
? CREATE_DATE DATE,
? DAL???????? NUMBER(10,1)
)
tablespace OMS
? pctfree 10
? initrans 1
? maxtrans 255
? storage
? (
??? initial 64K
??? next 1M
??? minextents 1
??? maxextents unlimited
? );*/
-- row_number() 是進行一個排序 over() 分析函數(shù)
-- (partition by t.name ) 對name 進行分組 條件是 日期從大到小
with a as
?(select t.*,
???????? row_number() over(partition by t.name order by t.create_date desc) rn
??? from testshic t)
select *
? from a
?where a.rn = 1
--oracle 寫法
with a as (select t.*,
????????????????? row_number()
????????????????? over(partition by t.name order by t.create_date desc) rn
???????????? from testshic t)
select *
? from a
?where a.rn = 1
---------------
--sqlserver 寫法
select *
? from testshic t
?where (t.name, t.create_date) in (select t.name, max(t.create_date)
???????????????????????????????????? from testshic t
??????????????????????????????????? group by t.name)
???????????????????????????????????
?????? select *
? from testshic t
?where? t.create_date? in (select? max(t.create_date)
???????????????????????????????????? from testshic t
??????????????????????????????????? group by t.name)
???????????????????????????????????
???????????????????????????????????
???????????????????????????????????
總結(jié)
以上是生活随笔為你收集整理的查询最后一次消费记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 集群篇-k8s介绍
- 下一篇: 去哪儿PMO Head赵云:项目管理背后