我正在尝试从具有不同的last_checked 日期(时间戳)的表中查找商品的最新竞争对手价格。
我正在尝试查找 OKJ 卖家的价格以及该商品在最新更新的 last_checked 日期的最低价格。
Isbb 表示最低价格
如果 amxcd 是最新时间戳更新中某个商品的卖家,那么我不应该选择该商品,此外,如果最新时间戳日期的商品 isfba =1,那么我不应该选择该商品
我还需要计算我选择的商品的卖家数量
如果 okj 是最新时间戳日期的竞争对手,那么我需要说"is",否则说“否”
--输入表格
create table #input
(itemid varchar(50),
seller varchar(30),
price int,
last_checked datetime2,
is_bb int,
isfba int
)
insert into #input
values
('bclpa','amxcd','89','2019-07-17 14:04:00.000','1','1'),
('bclpa','cvbd','100','2019-07-17 14:04:00.000','0','0'),
('lpatt','cvbd','78','2019-04-17 14:04:00.000','0','0'),
('lpatt','bnhj','60','2019-04-17 14:04:00.000','1','0'),
('lpatt','okj','90','2019-04-17 14:04:00.000','0','0'),
('lpatt','cvbd','70','2018-04-17 14:04:00.000','1','0'),
('lpatt','hjnk','90','2018-04-17 14:04:00.000','0','1'),
('cjatt','klmn','45','2019-04-17 14:04:00.000','1','0'),
('cjatt','njkl','89','2019-04-17 14:04:00.000','0','1'),
('cjatt','lket','40','2018-04-17 14:04:00.000','1','0'),
('cjatt','jklop','76','2018-04-17 14:04:00.000','0','0'),
('tilo','okj','56','2019-02-17 14:04:00.000','1','0'),
('tilo','jklop','70','2019-02-17 14:04:00.000','0','0'),
('tilo','hjkl','88','2019-02-17 14:04:00.000','0','0'),
('tilo','amxcd','32','2018-02-17 14:04:00.000','1','0'),
('jkatt','jklop','22','2018-02-17 14:04:00.000','1','0'),
('jkatt','amxcd','56','2018-02-17 14:04:00.000','0','0'),
('katt','hjkl','90','2019-02-17 14:04:00.000','1','0'),
('katt','lklo','99','2019-02-17 14:04:00.000','0','0'),
('katt','bnm','108','2019-02-17 14:04:00.000','0','0'),
('katt','okj','78','2018-02-17 14:04:00.000','1','0'),
('katt','nmsd','80','2018-02-17 14:04:00.000','0','0')
----输出表
create table #output
( itemid varchar(50),
okjprice int,
lowestprice int,
noofcompetitors int,
isokjacompetetior varchar(20)
)
insert into #output values
('lpatt','90','60','3','Yes'),
('tilo','56','56','3','yes'),
('katt','','90','3','No')
最佳答案
我认为这实现了您所描述的逻辑。但是,它还包括两个附加项目:
select itemid,
min(case when seller = 'okj' then price end) as okj_price,
min(price) as min_price,
sum(is_bb) as num_competitors,
max(case when is_bb = 1 and seller = 'okj' then 'yes' else 'no' end) as isokjcompetitor
from (select i.*,
first_value(seller) over (partition by itemid order by last_checked desc) as last_seller,
first_value(isfba) over (partition by itemid order by last_checked desc) as last_isfba
from input i
) i
where last_isfba <> 1 and last_seller <> 'amxcd'
group by itemid;
根据您的描述,“cjatt”和“kjatt”应该位于结果集中。
关于sql - 查找最新更新日期的竞争对手价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57369797/