简体   繁体   中英

How to select the last 12 months in sql?

I need to select the last 12 months. As you can see on the picture, May occurs two times. But I only want it to occur once. And it needs to be the newest one. Plus, the table should stay in this structure, with the latest month on the bottom.

在此处输入图片说明

And this is the query:

SELECT Monat2,
       Monat,
       CASE WHEN NPLAY_IND = '4P'
               THEN 'QuadruplePlay'
            WHEN NPLAY_IND = '3P'
               THEN 'TriplePlay'
            WHEN NPLAY_IND = '2P'
               THEN 'DoublePlay'
            WHEN NPLAY_IND = '1P'
               THEN 'SinglePlay'
       END AS Series,
       Anzahl as Cnt
FROM T_Play_n
where NPLAY_IND != '0P'
order by Series asc ,Monat

This is the new query

SELECT sub.Monat2,sub.Monat,
CASE WHEN NPLAY_IND = '4P'
    THEN 'QuadruplePlay'
     WHEN NPLAY_IND = '3P'
    THEN 'TriplePlay'
     WHEN NPLAY_IND = '2P'
    THEN 'DoublePlay'
    WHEN NPLAY_IND = '1P'
    THEN 'SinglePlay'
END
AS Series, Anzahl as Cnt FROM (SELECT ROW_NUMBER () OVER (PARTITION BY Monat2 ORDER BY Monat DESC)rn, 
                 Monat2,
                 Monat,
                 Anzahl,
                 NPLAY_IND
            FROM T_Play_n)sub
 where sub.rn = 1

It does only show the months once but it doesn't do that for every Series. So with every Play it should have 12 months.

在此处输入图片说明

In Oracle and SQL-Server you can use ROW_NUMBER .

name = month name and num = month number:

  SELECT sub.name, sub.num
    FROM (SELECT ROW_NUMBER () OVER (PARTITION BY name ORDER BY num DESC) rn,
                 name,
                 num
            FROM tab) sub
   WHERE sub.rn = 1
ORDER BY num DESC;
WITH R(N) AS
(
    SELECT 0
    UNION ALL
    SELECT N+1 
    FROM R
    WHERE N < 12
)

SELECT LEFT(DATENAME(MONTH,DATEADD(MONTH,-N,GETDATE())),3) AS [month]
FROM R

The With R(N) is a Common Table Expression.The R is the name of the result set (or table) that you are generating. And the N is the month number.

In SQL Server you can do It in following:

SELECT DateMonth, DateWithMonth -- Specify columns to select
FROM Tbl -- Source table
WHERE CAST(CAST(DateWithMonth AS INT) * 100 + 1 AS VARCHAR(20)) >= DATEADD(MONTH, -12,GETDATE()) -- Condition to return data for last 12 months
GROUP BY DateMonth, DateWithMonth -- Uniqueness
ORDER BY DateWithMonth -- Sorting to get latest records on the bottom

So it sounds like you want to select rows that contain the last occurrence of months. Something like this should work:

select * from [table_name]
where id in (select max(id) from [table_name] group by [month_column])

The last select in the brackets will get a list of id's for the last occurrence of each month. If the year+month column you have shown is not in descending order already, you might want to max this column instead.

You can use something like this(the table dbo.Nums contains int values from 0 to 11)

SELECT DATEADD(MONTH, DATEDIFF(MONTH, '19991201', CURRENT_TIMESTAMP) + n - 12, '19991201'),
       DATENAME(MONTH,DateAdd(Month, DATEDIFF(month, '19991201', CURRENT_TIMESTAMP) + n - 12, '19991201'))
FROM dbo.Nums

I suggest to use a group by for the month name, and a max function for the numeric component. If is not numeric, use to_number() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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