34,593
社区成员
发帖
与我相关
我的任务
分享
create table tb(price DECIMAL(18,2), RecordDate DATETIME)
--临时数据
insert into tb
select 1.23000,'2013-04-24 00:00:00'
union all select 1.25000, '2013-04-25 00:00:00'
union all select 1.25000 ,'2013-04-25 00:00:00'
union all select 1.25000 ,'2013-04-25 00:00:00'
union all select 1.25000 ,'2013-04-26 00:00:00'
union all select 1.28000 ,'2013-04-26 00:00:00'
union all select 1.25000 ,'2013-04-27 00:00:00'
union all select 1.25000 ,'2013-05-06 00:00:00'
union all select 1.24000 ,'2013-05-06 00:00:00'
union all select 1.24000 ,'2013-05-12 00:00:00'
union all select 1.24000 ,'2013-05-12 00:00:00'
union all select 1.24000 ,'2013-05-12 00:00:00'
union all select 1.30000 ,'2013-10-26 00:00:00'
union all select 1.31000 ,'2013-10-26 00:00:00'
SELECT * FROM tb
--求价格变化趋势Sql,并记录变化的日期,
--如果同一日期则记录最大值,
--记录变化日期的最早日期,
--如4-27和5-6同价则记录4-27
--期望结果如下:
--1.23 2013-04-24 00:00:00.000
--1.25 2013-04-25 00:00:00.000
--1.28 2013-04-26 00:00:00.000
--1.25 2013-04-27 00:00:00.000
--1.24 2013-05-06 00:00:00.000
--1.31 2013-10-26 00:00:00.000
create table #tb(price DECIMAL(18,2), RecordDate DATETIME)
--临时数据
insert into #tb
select 1.23000,'2013-04-24 00:00:00'
union all select 1.25000, '2013-04-25 00:00:00'
union all select 1.25000 ,'2013-04-25 00:00:00'
union all select 1.25000 ,'2013-04-25 00:00:00'
union all select 1.25000 ,'2013-04-26 00:00:00'
union all select 1.28000 ,'2013-04-26 00:00:00'
union all select 1.25000 ,'2013-04-27 00:00:00'
union all select 1.25000 ,'2013-05-06 00:00:00'
union all select 1.24000 ,'2013-05-06 00:00:00'
union all select 1.24000 ,'2013-05-12 00:00:00'
union all select 1.24000 ,'2013-05-12 00:00:00'
union all select 1.24000 ,'2013-05-12 00:00:00'
union all select 1.30000 ,'2013-10-26 00:00:00'
union all select 1.31000 ,'2013-10-26 00:00:00'
SELECT * FROM #tb
select MAX(price),RecordDate from #tb
group by RecordDate
order by RecordDate
price RecordDate
---------------------------------
1.23 2013-04-24 00:00:00.000
1.25 2013-04-25 00:00:00.000
1.28 2013-04-26 00:00:00.000
1.25 2013-04-27 00:00:00.000
1.25 2013-05-06 00:00:00.000
1.24 2013-05-12 00:00:00.000
1.31 2013-10-26 00:00:00.000
create table #tb(price DECIMAL(18,2), RecordDate DATETIME)
--临时数据
insert into #tb
select 1.23000,'2013-04-24 00:00:00'
union all select 1.25000, '2013-04-25 00:00:00'
union all select 1.25000 ,'2013-04-25 00:00:00'
union all select 1.25000 ,'2013-04-25 00:00:00'
union all select 1.25000 ,'2013-04-26 00:00:00'
union all select 1.28000 ,'2013-04-26 00:00:00'
union all select 1.25000 ,'2013-04-27 00:00:00'
union all select 1.25000 ,'2013-05-06 00:00:00'
union all select 1.24000 ,'2013-05-06 00:00:00'
union all select 1.24000 ,'2013-05-12 00:00:00'
union all select 1.24000 ,'2013-05-12 00:00:00'
union all select 1.24000 ,'2013-05-12 00:00:00'
union all select 1.30000 ,'2013-10-26 00:00:00'
union all select 1.31000 ,'2013-10-26 00:00:00'
select price,RecordDate from (
SELECT ROW_NUMBER()over(partition by price order by RecordDate)id,* FROM #tb
)a where id=1
price RecordDate
--------------------------------------- -----------------------
1.23 2013-04-24 00:00:00.000
1.24 2013-05-06 00:00:00.000
1.25 2013-04-25 00:00:00.000
1.28 2013-04-26 00:00:00.000
1.30 2013-10-26 00:00:00.000
1.31 2013-10-26 00:00:00.000
(6 行受影响)
create table test(id int identity(1,1) not null,price DECIMAL(18,2), RecordDate DATETIME)
insert test(price,RecordDate)
SELECT price,RecordDate from tb
select price,RecordDate
FROM test t
WHERE not EXISTS (
SELECT *
FROM test
WHERE price = t.price AND id=t.id-1
)
/*
1.23 2013-04-24 00:00:00.000
1.25 2013-04-25 00:00:00.000
1.28 2013-04-26 00:00:00.000
1.25 2013-04-27 00:00:00.000
1.24 2013-05-06 00:00:00.000
1.30 2013-10-26 00:00:00.000
1.31 2013-10-26 00:00:00.000
*/
--如果同一日期则记录最大值,
--记录变化日期的最早日期,
--如4-27和5-6同价则记录4-27
--以上没看懂啥意思?
select MAX(price),RecordDate from tb
group by RecordDate
order by RecordDate
;with t
as
(
select *,
ROW_NUMBER() over(order by recorddate) rownum,
ROW_NUMBER() over(PARTITION by price order by recorddate) as rn
from tb
)
select price,recorddate
from
(
select *,
row_number() over(partition by price,rownum - rn order by rownum) rownum2
from t
)t
where rownum2 = 1
order by recorddate
/*
price recorddate
1.23 2013-04-24 00:00:00.000
1.25 2013-04-25 00:00:00.000
1.28 2013-04-26 00:00:00.000
1.25 2013-04-27 00:00:00.000
1.24 2013-05-06 00:00:00.000
1.30 2013-10-26 00:00:00.000
1.31 2013-10-26 00:00:00.000
*/