I am using the below query to list the number of transactions by month. Does anyone know how can I list by year too. This means that the query returns all my transactions for the whole year except the current month.
That is if today it is the 29th August 2011 I need a yearly report grouped by month till the month of July (since august is not complete)
select to_char(date,'MONTH YYYY'), sum(number_of_transactions)
from header
group by date
order by date
select to_char(trunc(date,'yyyy'),'YYYY') as year, sum(number_of_transactions)
from header
where date < trunc(sysdate, 'mm')
group by trunc(date,'yyyy')
order by year
Is this what you need?
SELECT TO_CHAR(date, 'YYYY'), SUM(number_of_transactions)
FROM header
GROUP BY TO_CHAR(date, 'YYYY')
Related
I am trying to Calculate MTD sales on daily sales number but my month starts from 26th of previous month to 25th of next month.
data contains only 3 columns (date, vendor_id, total_sales).
Below Code is working fine for calculating months starting from the 1st. I tried to do this by using the below approaches but it does not works
date(date - interval '25 day') : Not working
Mapping table creation for each day, but will not work for 30/31 days month
Need suggestion on above.
SUM(sales) OVER (
PARTITION BY
vendor_id,
EXTRACT(YEAR FROM date),
EXTRACT(MONTH FROM date)
ORDER BY
date ROWS UNBOUNDED PRECEDING
) AS mtd_total_sales,
So, if the date is >= 26, then "it is the next month".
As a result, something like
CASE
WHEN EXTRACT(DAY FROM date) >= 26 THEN ADD_MONTHS(date, 1)
ELSE date
END
should suffice, since you extract the year and month anyway.
How can i select every 1./2./3./.4-5 week (interval) from the current months?
So not like:
select to_char(sysdate,'W') from dual
But i need an interval = week of the current month (for example 2.week of oktober, because it's october - sysdate). So, concretely:
select SUM(number)/((trunc(sysdate,'WW')/4) from my_table where date between ? and ?
Something like this should work for you.
SELECT TO_CHAR(datecol,'W') AS week,
SUM(countcol) AS sum_of_sales
FROM sales
WHERE TO_CHAR(datecol,'YYYYMM') = TO_CHAR(SYSDATE,'YYYYMM') --current year and month.
GROUP BY TO_CHAR(datecol,'W')
ORDER BY TO_CHAR(datecol,'W')
I'm working with Oracle SQL.
I'm trying to get all records from the current month and last year.
Example: This is August 12, so I want all records from 1-12 August 2017
The query gets me all records in the current month 2018.
BETWEEN trunc (sysdate, 'mm') AND SYSDATE
How to do the same for last year?
You can use ADD_MONTHS function to get 12 months before:
Between trunc(ADD_MONTHS(sysdate,-12),'mm') and trunc(ADD_MONTHS(sysdate,-11),'mm')
You can do:
where date >= trunc(sysdate, 'mon') - interval '12' month and
date < trunc(sysdate, 'mon') - interval '11' month
I need to get last day from next month in this select.
select (last_day(month from :date+1)||'.'||
(extract(month from :date)+1)||'.'||
extract(year from :date))
from dual;
Everything is ok with month and year but I have a problem with last day function.
Using (extract(month from :date)+1) will not work for a date in December as you will end up with a 13th month. Similarly, extract(year from :date) will get you the current year - which may not be correct if you are looking for the last day of the next month from December and the result should be the January of the following year.
You do not need to extract all the fields separately, you can do it all in a single statement:
SELECT TO_CHAR(
LAST_DAY( ADD_MONTHS( :date, 1 ) ),
'DD.MM.YYYY'
)
FROM DUAL
I am trying to format a Vertica date column into only the month.
I would need the final value in some sort of date datatype so in the report I can order the results by date/time, not order by text. So that February sorts after January etc.
select TO_DATE(TO_CHAR(purchase_date), 'Month')
from transactions
order by 1;
I am also tried:
select TO_DATE(TO_CHAR(MONTH(purchase_date)), 'Month')
from transactions
order by 1;
The above statements produce an error "Invalid value for Month"
Any ideas?
How about this?
select to_char(purchase_date, 'Month')
from transactions
order by purchase_date;
You can order by columns that are not in the select list.
EDIT:
If you want to combine months from multiple years, the above will not work quite right. This will:
select to_char(purchase_date, 'Month')
from transactions
order by extract(month from purchase_date);
Select TO_CHAR((NOW() - INTERVALYM '1 MONTH'), 'MON');
Output:
JUN
This will help you get only the name of the previous month.
We can directly use date_part to get a month from the timestamp.
SELECT DATE_PART('MONTH', purchase_date) purchase_date
TO_DATE gives the complete date, if you only provide MM in the parameter then Vertica set default year and day.
Let suppose the month number is 8.
SELECT TO_DATE(purchase_date, 'MM')
Output: (0001-08-01)