简体   繁体   English

SQL Select 最后 6 个月来自在列中保存年份和月份的表

[英]SQL Select last 6 month from a table that saves year and month in columns

I have a table that saves year and month in columns and I need to get the top 5 grouping by an ID in the last 6 month.我有一个在列中保存年份和月份的表,我需要在过去 6 个月内按 ID 获得前 5 个分组。

I can easily get the information for a particular month an year but how can I get the same ranking of the top 5 for the last 6 month?我可以很容易地获得一年中特定月份的信息,但是我怎样才能获得过去 6 个月的前 5 名的相同排名呢?

CREATE TABLE mytable (
table_id INT,  
some_id INT, 
some_other_id INT, 
mt_month INT, 
mt_year INT);

INSERT INTO mytable VALUES
("101","17","370","12","2021"),
("102","17","370","1","2021"),
("104","14","371","2","2021"),
("105","14","371","11","2021"),
("107","14","371","12","2021"),
("108","12","372","2","2021"),
("109","12","372","3","2021"),
("111","13","372","12","2021"),
("113","18","373","10","2021"),
("115","18","373","1","2021"),
("117","18","373","4","2021"),
("119","11","373","5","2021"),
("120","11","373","5","2021"),
("121","12","373","6","2021"),
("122","13","373","6","2021"),
("123","13","373","6","2021"),
("124","12","373","7","2021"),
("125","11","373","7","2021"),
("126","11","373","7","2021"),
("127","12","373","8","2021"),
("128","11","373","8","2021"),
("129","11","373","8","2021"),
("130","13","373","9","2021"),
("131","11","373","10","2021"),
("132","11","373","11","2021"),
("133","11","373","12","2021");

select some_id, min(mt_month), min(mt_year), count(table_id) as number from mytable group by some_id order by number DESC LIMIT 5;

https://www.db-fiddle.com/f/iRmvLZs9L8Tk1zJFgTMusX/0 https://www.db-fiddle.com/f/iRmvLZs9L8Tk1zJFgTMusX/0

Following approach should work:以下方法应该有效:

  1. Filter the data for the last 6 months in the subquery (can be done by concatenating year and month and applying the relevant filter)在子查询中过滤最近 6 个月的数据(可以通过连接年份和月份并应用相关过滤器来完成)
  2. Use your logic of grouping in the outer query.在外部查询中使用您的分组逻辑。

This is what I came up with quickly.这是我很快想到的。 Made a variable to hold the wanted info and applied your same logic.创建一个变量来保存想要的信息并应用相同的逻辑。 Just specify time manually.只需手动指定时间。

WITH a AS (
SELECT * FROM mytable
WHERE mt_year = 2021 and
mt_month BETWEEN 7 and 12 
)
select some_id, min(mt_month), min(mt_year), count(table_id) as number from a 
group by some_id
order by number DESC LIMIT 5;

This might not run but something like this should do the trick for february.这可能不会运行,但像这样的东西应该可以在 2 月完成。

WITH a AS (
SELECT * FROM mytable
WHERE mt_year = 2020 and
mt_month BETWEEN 9 and 12 
),
b AS (  
SELECT * FROM mytable
WHERE mt_year = 2021 and
mt_month BETWEEN 1 and 2 
)
select some_id, min(mt_month), min(mt_year), count(table_id) as number from a 
UNION ALL
select some_id, min(mt_month), min(mt_year), count(table_id) as number from b
group by some_id order by number DESC LIMIT 5;

Easiest way is to use a regular date format that sql understands from the beginning.最简单的方法是使用 sql 从一开始就理解的常规日期格式。

The below query returns the last 6 months' values based on the current date.以下查询返回基于当前日期的最近 6 个月的值。 If I understand correctly you are trying something like:如果我理解正确,你正在尝试类似的东西:

SELECT table_id, some_id, some_other_id,mt_year,mt_month
FROM (
SELECT table_id, some_id, some_other_id, 
  mt_year , 
  mt_month ,
  @order_rank := IF(@current_month = mt_month,
  @order_rank + 1, 1) AS order_rank,
  @current_month := mt_month 
FROM mytable
ORDER BY   mt_year ,mt_month , some_id DESC ) ranked_orders
WHERE order_rank <= 5
and
mt_month BETWEEN MONTH((CURRENT_DATE() - INTERVAL 6 MONTH)) AND MONTH(CURRENT_DATE());

Demo: https://www.db-fiddle.com/f/iRmvLZs9L8Tk1zJFgTMusX/2演示: https://www.db-fiddle.com/f/iRmvLZs9L8Tk1zJFgTMusX/2

Try on your data and let me know.试试你的数据,让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM