想要查询 在去年某段时间内销售的商品,今天某段时间内这些商品是否也有销售,如果没有,则列出是哪些商品

请指教:
access 数据库
商品名称 商品价格 购买时间
想要查询 在去年某段时间内销售的商品,今天某段时间内这些商品是否也有销售,如果没有,则列出是哪些商品


select * from
(select distinct 商品ID,'去年' qFlag from 订单表 where time >= '' and time <= '') b left join
(select distinct 商品ID,'今年' tFlag from 订单表 where time >= '' and time <= '') a
on b.商品ID = a.商品ID  where tFlag is null;


SELECT 
    * 
FROM
    table
WHERE
     商品名称 IN (SELECT DISTINCT(商品名称) FROM table WHERE 购买时间 BETWEEN 去年开始时间 AND 去年结束时间)
AND
    商品名称 NOT IN (SELECT DISTINCT(商品名称) FROM table WHERE 购买时间 BETWEEN 今天开始时间 AND 今天结束时间)
    

SELECT * from 
(SELECT * from 订单表 where 购买时间 BETWEEN '时间1' and '时间1') a
LEFT JOIN (SELECT * from 订单表 where 购买时间 BETWEEN '时间1' and '时间1') b 
on a.商品id = b.商品id where b.商品id is NULL

我本地举个例子
我有张test表,里面有id、name、age字段,数据有age从15到19,如图:

img

SELECT * from 
(SELECT * from test where age BETWEEN 15 and 18 ORDER BY age) a
LEFT JOIN (SELECT * from test where age BETWEEN 16 and 19 ORDER BY age) b 
on a.id = b.id where b.id is NULL

查询结果:

img

我这sql查询的就是a里面有的并且过滤掉了b里的。结合你的需要替换主表和附表

p_id:商品编码
p_name : 商品名称
p_price_ture : 商品售价
time_count :销售次数
total_count:销售总价
quanity_count:销售数量
p_price_final_avg:销售均价
还需要统计这些
而且不是单一商品,就是去年或者过去某短时间有售商品,然后判断目前这些是否有售,如果没有销售,则列出去年或者过去的数据

select * from (select p_id,p_name,p_img,p_price_true,count(*) as times_count,sum(p_total) as total_count,sum(p_quanity) as quanity_count,avg(p_price_true_final) as p_price_final_avg from class_analyse where order_from > 0 and order_starttime not between #2021-09-11# and #2022-09-21 23:59:59# or order_starttime between #2021-08-01# and #2021-08-11 23:59:59# group by p_id,p_name,p_img,p_price_true) order by times_count desc

去年得商品和今年得左关联,取出右边结果是null就是没有卖过得商品