how do you get a column of your table to say the name of the month and the year for which some events happened?
I want to display data in rows by the last six months
so for the first column June 2021, July 2021...November 2021
SQL function Month returns the number for the month given a date.
I don't see a function for naming the month.
Then the second part is, does each row need to be a separate query?
because I want to get totals, but each of them per month.
I am using transaction group, and it is early so I can change my group too.
I have a tstamp, I have a column for the number of the month of an event, but I need it for the ranges
ahhh edit
I think I need case statement
still confused about how I would get the data for the ranges into different rows though
I think you are describing a PIVOT operation, in addition to wanting the name of the month. In MS SQL Server, youâd use FORMAT(someDate, âMMMMâ) for that part.
That tells me nothing. SQL is a generic name. Specific DB brands would be something like "MS SQL Server", "MySQL", "MariaDB", "PostgreSQL", or "Oracle". Or random others. Syntax and available functions do vary.
I don't think that will work, Year-Month is not a built-in MS-SQL function. Also, I don't think you can group by an aliased column. You would probably need
I canât tell if it worked because I only have data in November lol
My formatting came out as 2021-November
it did not like Year-Month, used Year_Month
select FORMAT(t_stamp, 'yyyy-MMMM') as Year_Month, sum(someColumn) as Total
from tableName
where (t_stamp>DATEADD(month, -6, GETDATE()))
group by FORMAT(t_stamp, 'yyyy-MMMM')
going to have to add some data to the table to test it
select FORMAT(t_stamp, 'yyyy-MMM') as Year_Month, sum(someColumn) as Total
from tableName
where (t_stamp>DATEADD(month, -6, GETDATE()))
group by FORMAT(t_stamp, 'yyyy-MMM')
well I have a new issue
it is ordering the months alphabetically
jun
nov
sep
instead of by the sequential date
However, it is counting and grouping them correctly.
I tried adding desc to the end of an order by FORMAT(t_stamp, 'yyyy-MMM') desc
sep
nov
jun
edit
oh I think I get it, I donât use format on orderby
Push your query into a subquery that exposes a t_stamp for each month, then order by that in the outer SELECT. Something like this:
SELECT Year_Month, Total
FROM (
select min(t_stamp) AS earliest, FORMAT(t_stamp, 'yyyy-MMM') as Year_Month, sum(someColumn) as Total
from tableName
where (t_stamp>DATEADD(month, -6, GETDATE()))
group by FORMAT(t_stamp, 'yyyy-MMM')
) subq
ORDER BY earliest
I got an error for when adding t_stamp.
Said it was not in my group by or my select.
The subquery worked.
It seems very useful that the subquery allows organizing by data that is not shown in the final display.
Thanks for showing me this.
You don't need a subquery just for that. Any data exposed by a FROM clause when not grouping is available for use in ORDER BY, whether in the SELECT list or not. When using a subquery, only items specified in the inner SELECT list are available to the outer FROM.
So I noticed that I only get values for the months that have data.
I found an old post that used coalesce(datathatisnull,0) and this should help me fill in the zeros.
SELECT Year_Month, coalesce(Total,0)
FROM (
select min(t_stamp) AS earliest, FORMAT(t_stamp, 'yyyy-MMM') as Year_Month, sum(someColumn) as Total
from tableName
where (t_stamp>DATEADD(month, -6, GETDATE()))
group by FORMAT(t_stamp, 'yyyy-MMM')
) subq
ORDER BY earliest
this did not work
also tried the coalesce on the sum(someColumn)
did not work
edit
as I think about it, I donât have values of timestatmps for those months to be null to set them to 0
I just have a lack of any data in those months at all
so I need to somehow check each month, see the null, and then coalesce that value to 0 I think
looks like the internet says create a table of all months, left join it
Yes, in MS SQL Server, that is probably the easiest. In more capable databases, I'd use a table generating function to dynamically supply a range of months. In PostgreSQL, it would be the generate_series() function.