How do I convert Months from Numeral to String Form in SQL Query Containing GROUP BY? - sql

So, I'm creating a View that pulls a bunch of data (with a SUM aggregate on one field). The query contains GROUP BY clause and when I do the month conversion in the query as follows, I get an %invalid character% error.
SELECT Code, Name, Products, SUM(Costs) AS TOTAL,
Year, to_char(to_date(PurchaseMonth),'DD-Mon-yyyy') AS MonthProductPurchased
FROM ProductCatalog
<BUNCH OF JOINS HERE>
WHERE ABC.Code=DEF.Code AND Products IS NOT null
GROUP BY Code, Name, Products, Year, MonthProductPurchased;
So, for instance, I want to convert '1' in the Month field to 'January', '2' to February, so on and so forth.

I see, you have something called PurchaseMonth and it takes the values 1, 2, 3, etc. You want to convert these to month names. How about this?
to_char(to_date('2000-' || PurchaseMonth || '-01'), 'YYYY-MM-DD'), 'MONTH')

Related

Combine "Year", "Month", and "Day" columns in Biqquery to a Date column

I am new to the bigquery. I was looking into the public dataset gsod, which has three different columns, i.e., year, month, and day. Can I change this to a single column called date in format yyyy-mm-dd?
I saw you could do something like
SELECT
DATE(2016, 12, 25) AS date_ymd
But how do I pass columns inside DATE instead of a single value? I want to combine the query with the following simple query and get a column called date.
SELECT
*,
FROM `bigquery-public-data.samples.gsod`
Use below
SELECT date(year, month, day) as date_ymd, *
FROM `bigquery-public-data.samples.gsod`

How to write SQL statement to select for data broken up for each month of the year?

I am looking for a way to write an SQL statement that selects data for each month of the year, separately.
In the SQL statement below, I am trying to count the number of instances in the TOTAL_PRECIP_IN and TOTAL_SNOWFALL_IN columns when either column is greater than 0. In my data table, I have information for those two columns ("TOTAL_PRECIP_IN" and "TOTAL_SNOWFALL_IN") for each day of the year (365 total entries).
I want to break up my data by each calendar month, but am not sure of the best way to do this. In the statement below, I am using a UNION statement to break up the months of January and February. If I keep using UNION statements for the remaining months of the year, I can get the answer I am looking for. However, using 11 different UNION statements cannot be the optimal solution.
Can anyone give me a suggestion how I can edit my SQL statement to measure from the first day of the month, to the last day of the month for every month of the year?
select monthname(OBSERVATION_DATE) as "Month", sum(case when TOTAL_PRECIP_IN or TOTAL_SNOWFALL_IN > 0 then 1 else 0 end) AS "Days of Rain" from EMP_BASIC
where OBSERVATION_DATE between '2019-01-01' and '2019-01-31'
and CITY = 'Olympia'
group by "Month"
UNION
select monthname(OBSERVATION_DATE) as "Month", sum(case when TOTAL_PRECIP_IN or TOTAL_SNOWFALL_IN > 0 then 1 else 0 end) from EMP_BASIC
where OBSERVATION_DATE between '2019-02-01' and '2019-02-28'
and CITY = 'Olympia'
group by "Month"```
Your table structure is too unclear to tell you the exact query you will need. But a general easy idea is to build the sum of your value and then group by monthname and/or by month. Sice you wrote you only want sum values greater 0, you can just put this condition in the where clause. So your query will be something like this:
SELECT MONTHNAME(yourdate) AS month,
MONTH(yourdate) AS monthnr,
SUM(yourvalue) AS yoursum
FROM yourtable
WHERE yourvalue > 0
GROUP BY MONTHNAME(yourdate), MONTH(yourdate)
ORDER BY MONTH(yourdate);
I created an example here: db<>fiddle
You might need to modify this general construct for your concrete purpose (maybe take care of different years, of NULL values etc.). And note this is an example for a MYSQL DB because you wrote about MONTHNAME() which is in most cases used in MYSQL databases. If you are using another DB type, maybe you need to do some modifications. To make sure that answers match your DB type, tag it in your question, please.

Display By month using select statement

SELECT SUM(Total_A ) FROM Materials_List
This is the snippet of code that I have.
I need it to calculate by month and display by month using SQL.
I also would like it to be a code I can use for any month in the year not just one month at a time.
You seem to be looking for simple aggregation:
select
year(materials_datetime) yr,
month(materials_datetime) mn,
sum(total_a) sum_total_a
from materials_list
group by
year(materials_datetime),
month(materials_datetime)
order by yr, mn
This assumes that column materials_datetime contains the date/time that you want to use to aggregate the data.

View data by date after Format 'mmyy'

I'm trying to answer questions like, how many POs per month do we have? Or, how many lines are there in every PO by month, etc. The original PO dates are all formatted #1/1/2013#. So my first step was to Format each PO record date into 'mmyy' so I could group and COUNT them.
This worked well but, now I cannot view the data by date... For example, I cannot ask 'How many POs after December did we get?' I think this is because SQL does not recognize mm/yy as a comparable date.
Any ideas how I could restructure this?
There are 2 queries I wrote. This is the query to format the dates. This is also the query I was trying to add the date filter to (ex: >#3/14#)
SELECT qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy") AS [Date]
FROM qryALL_PO
GROUP BY qryALL_PO.POLN, Format([PO CREATE DATE],"mm/yy");
My group and counting query is:
SELECT qryALL_PO.POLN, Sum(qryALL_PO.[LINE QUANTITY]) AS SUM_QTY_PO
FROM qryALL_PO
GROUP BY qryALL_PO.POLN;
You can still count and group dates, as long as you have a way to determine the part of the date you are looking for.
In Access you can use year and month for example to get the year and month part of the date:
select year(mydate)
, month(mydate)
, count(*)
from tableX
group
by year(mydate)
, month(mydate)
You can format it 'YYYY-MM' , and then use '>' for 'after' clause

Show data that is only from a specific quarter

I need to show the data that is only from the 2nd quarter of any year. I am trying to use to_date in the where clause but it is giving me an error. Not sure what I am doing wrong. (Using Oracle 10g)
Code:
SELECT product_name, name
FROM a_product p JOIN a_item i
ON p.product_id=i.product_id
JOIN a_sales_order so ON i.order_id=so.order_id
JOIN a_customer c ON so.customer_id=c.customer_id
WHERE regexp_like(product_name, 'Transducer') AND order_date=to_date(2, 'Q')
The AND portion is where I am having trouble, it is giving me a "format code cannot appear in date input format" order_date is the field within the table that I use to find out what sales were done in quarter 2 of any year.
You have the arguments backwards. You want that to be:
AND to_char(order_date, 'Q') = '2'
The function to_char() (in this context) takes a date and formats it as a string. What string? A string with the quarter in it. You then want to compare it to the value you care about.