I want to select a date from oracle table formatted like select (to_char(req_date,'MM/YYYY')) but I also want to order the result set on this date format.
I want them to be ordered like dates not strings.
Like this
09/2009
10/2009
11/2009
12/2009
01/2010
02/2010
03/2010
04/2010
05/2010
06/2010
07/2010
08/2010
09/2010
10/2010
11/2010
12/2010
Not like
01/2010
02/2010
03/2010
04/2010
05/2010
06/2010
07/2010
08/2010
09/2009
09/2010
10/2009
10/2010
11/2009
11/2010
12/2009
12/2010
Any way to do this in sql?
Full SQL is:
SELECT (to_char(req_date,'MM/YYYY')) as monthYear, count(req_id) as count
FROM REQUISITION_CURRENT t
GROUP BY to_char(req_date,'MM/YYYY')
Thanks
Try this. It works and it's efficient, but looks a little messy.
select to_char(trunc(req_date, 'MM'),'MM/YYYY') as monthYear
,count(req_id) as count
from requisition_current
group
by trunc(req_date, 'MM')
order
by trunc(req_date, 'MM');
Try this
select monthyear,yr,month,count(req_id)
from
(
SELECT (to_char(req_date,'MM/YYYY')) as monthYear, to_char(req_date,'YYYY') yr, to_char(req_date,'mm') month, req_id
FROM REQUISITION_CURRENT t
) x
GROUP BY monthyear,yr,month
order by yr, month
Please try
Select req_date, (to_char(req_date,'MM/YYYY')) from MY_TABLE order by req_date
You are free to add additional sort fields, even if they are the same field.
Just use order by req_date instead of order by to_char(req_date,'MM/YYYY').
Try
SELECT ...
ORDER BY MIN(req_date)
That'll get around Oracle's rules about what can be selected after a GROUP BY.
I am considerably late to the party, but the most intuitive way I've found to achieve this is the following:
SELECT DISTINCT
to_char(req_date,'MM/YYYY') as monthYear,
count(req_id) as count
FROM
REQUISITION_CURRENT t
GROUP BY
to_char(req_date,'MM/YYYY')
ORDER BY
to_date(monthYear,'MM/YYYY')
It's may not the most computationally efficient method since it converts the date to a character and then back to a date, but that is precisely what you are asking a query like this to do. It also saves you from adding support columns or nesting subqueries.
Related
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.
I'm working with a large transaction data set and would like to group a count of individual customer transactions by month. I am unable to use the timestamp function in the GROUP BY and return the following error:
BAD_QUERY (expression STRFTIME_UTC_USEC([DATESTART], '%b') in GROUP BY is invalid)
Is there a simple workaround to achieve this or should I build a calendar table (which may be the simplest option)?
You have to use an alias:
SELECT STRFTIME_UTC_USEC(DATESTART, '%b') as month, COUNT(TRANSACTION)
FROM datasetId.tableId
GROUP BY month
#Charles is correct but as an aside you can also group by column number.
SELECT STRFTIME_UTC_USEC(DATESTART, '%b') as month, COUNT(TRANSACTION) as count
FROM [datasetId.tableId]
GROUP BY 1
ORDER BY 2 DESC
This is my sum clause
Select *,(sum(current_bal-curr_bal_now)/
current_bal from base
Group by month
This gives me an error because I'm not using current_bal in the group by.
Is there a way of not using group by current_bal aswell as month as it completely messes up the output layout.
Thanks
Another guess...
SELECT *,
( sum(current_bal) OVER (PARTITION BY month) ) / current_bal
FROM base
The problem is that the sum will return you only a value, so, in your all select, Current_bal is different. Which one should choose ?
If what you want is add every division, something like this:
Select sum(current_bal/current_bal)
from base
Group by month
will work
I want to select rows according to the month of a date or timestamp column like this:
SELECT id, name, birthday
FROM employee.person
WHERE Month(birthday) > 10;
But I only get error messages in PostgreSQL.
How can this be done?
You can use EXTRACT function, like this:
SELECT id, name, birthday FROM employee.person
WHERE EXTRACT(MONTH FROM birthday) > 10;
Your problem comes from the fact that there is no such thing as Month function in PostgreSQL. Check online documentation here to see what you can get instead. Extract should be enough.
If you want you can also extract the month name using the following function.
SELECT TO_CHAR(DATE(REPORT_DATE), 'Month') FROM TABLE_NAME
SELECT
siteapplications.Application, Count(visits.VisitId) AS CountOfVisitId
FROM
visits,
siteapplications
WHERE
visits.SiteApplicationId=siteapplications.ApplicationID
and Month([visits.VisitTime])= month and Year([visits.VisitTime])= year
GROUP BY
siteapplications.Application
ORDER BY
CountOfVisitId DESC;
Maybe a stupid question but with when I run this query I need to fill in the month, year AND CountOfVisitId??
But for CountOfVisitId I need that it is calculated (hence the query)
I don't have much experience with query's but I need this one in java
Can anyone explain or solve ....
Often, depending on your brand and version of SQL, you cannot group by a column alias, or sort by a column alias. So you might try doing ORDER BY 2 DESC instead.
As #MJB mentioned, it is fairly common that you cannot sort by a column alias. Try this (notice the change to the ORDER BY):
SELECT
siteapplications.Application, Count(visits.VisitId) AS CountOfVisitId
FROM
visits,
siteapplications
WHERE
visits.SiteApplicationId=siteapplications.ApplicationID
and Month([visits.VisitTime])= month and Year([visits.VisitTime])= year
GROUP BY
siteapplications.Application
ORDER BY
Count(visits.VisitId) DESC;