It's for a school project. I got a table Consultation with the following data :
DoctorId integer,
PatientFile varchar2(20),
visitDate date,
Diagnostic varchar2(20) and
Prescription varchar2(20).
I want to create a query that will show the average number of consultation by month. I try :
SELECT AVG(count(*)) AS count, MONTH(dateVisit) as month
FROM consultation
GROUP BY month
I doesn't work : I can't use the month fonction on dateVisit.
My questions : how would you do a query that will show the average number of consultation by month ?
Many thanks in advance for your help
I found the solution :
select avg (distinct (extract(month from visitDate))) as month from
consultation;
So here's how it's working :
1- extract(month from table_name) as month from table_name. You can
also put year or day instead of month.
2- distinct = will count the total for each month (instead of showing every record).
3- avg = average of each month.
Related
I'm using oracle dbms and I have in Employe table a column Birthdate. I want to write a query that shows the employees who has a birthday next week.
Is this correct ?
select name
from employe
where to_char(birthdate,'DD-MM')=to_char(next_day(sysdate,1)+7,'DD-MM');
That is not the correct usage of next_day(): that function returns the date of the the next instance of a day. For example, to find the date of next Friday:
select next_day(sysdate, 'FRIDAY') from dual;
To find employees whose birthday is seven days from now, you need to just tweak your query a bit:
select name
from employe
where to_char(birthdate,'DD-MM') = to_char(sysdate+7,'DD-MM');
The correct solution would be
SELECT name
FROM employe
WHERE to_char(birthdate
/* "move" the birthdate to the current year
to get a reliable week number */
+ CAST((EXTRACT(year FROM current_date)
- EXTRACT(year FROM birthdate)) || '-0'
AS INTERVAL YEAR TO MONTH),
'IW')
= to_char(current_date + 7, 'IW');
The IW format returns the ISO week containing the date, which is probably what you are looking for. If you start your week on Sunday, add one to both dates.
We can assume that Yesterday will be the last non blank date in the Sales table
How do I add the above logic into the following measure?
Yesterday Sales =
CALCULATE(
SUM(Sales[SalesAmount])
, LASTDATE('Calendar'[Date])
)
Currently this measure returns the following:
This makes sense as the last date in the calendar table is not in the Sales table - so I think I need to change LASTDATE('Calendar'[Date]) so it only returns the last date that is in the Sales table - how do I do this?
note
There is a relationship between the Calendar and Sales table based on a Datekey but there is no Date column in the Sales table.
You can calculate it based on your date table and the today() function.
Yesterday Sales =
CALCULATE(
SUM(Sales[SalesAmount])
, 'Calendar'[Date] = today()-1
)
If you want to be based on your 'date' in the salestable, you could use a max of that field and compare with this field.
I have a table in a database in hive.
The table is partitioned based on year month and day.
My query looks something like this
select entity1,entity2
from table_t
INNER JOIN tab_roll.cha alias2
ON alias1.sid = alias2.sid
INNER JOIN net_roll.net alias3
ON alias2.id=alias3.id
where event= 'unknown'
and day >= 10 and day < 12
and month >= 5 and month < 11
and year = 2014
now I want to get results between say mm-dd-yyy HH : MM :SS and mm-dd-yyy HH : MM :SS, how should I do that?
Is is possible to have a pop up where the user chooses the date/time ranges?
Don't know if this helps but the data has about 500 million rows.
Thanks
I think Between should work for you. & to optimize this you can index that column too.
I have dates in this format in my database "01-APR-12" and the column is a DATE type.
My SQL statement looks like this:
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno AND s.salestype = 1
AND (s.salesdate BETWEEN '01-APR-12' AND '31-APR-12');
When I try to do it that way, I get this error -- ORA-01839: date not valid for month specified.
Can I even use the BETWEEN keyword with how the date is setup in the database?
If not, is there another way I can get the output of data that is in that date range without having to fix the data in the database?
Thanks!
April has 30 days not 31.
Change
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno AND s.salestype = 1
AND (s.salesdate BETWEEN '01-APR-12' AND '31-APR-12');
to
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno AND s.salestype = 1
AND (s.salesdate BETWEEN '01-APR-12' AND '30-APR-12');
and you should be good to go.
In case the dates you are checking for range from 1st day of a month to the last day of a month then you may modify the query to avoid the case where you have to explicitly check the LAST day of the month
SELECT DISTINCT c.customerno, c.lname, c.fname
FROM customer c, sales s
WHERE c.customerno = s.customerno
AND s.salestype = 1 AND (s.salesdate BETWEEN '01-APR-12' AND LAST_DAY(TO_DATE('APR-12', 'MON-YY'));
The LAST_DAY function will provide the last day of the month.
The other answers are missing out on something important and will not return the correct results. Dates have date and time components. If your salesdate column is in fact a date that includes time, you will miss out on any sales that happened on April 30 unless they occurred exactly at midnight.
Here's an example:
create table date_temp (temp date);
insert into date_temp values(to_date('01-APR-2014 15:12:00', 'DD-MON-YYYY HH24:MI:SS'));
insert into date_temp values(to_date('30-APR-2014 15:12:00', 'DD-MON-YYYY HH24:MI:SS'));
table DATE_TEMP created.
1 rows inserted.
1 rows inserted.
select * from date_temp where temp between '01-APR-2014' and '30-APR-2014';
Query Result: 01-APR-14
If you want to get all records from April that includes those with time-components in the date fields, you should use the first day of the next month as the second side of the between clause:
select * from date_temp where temp between '01-APR-2014' and '01-MAY-2014';
01-APR-14
30-APR-14
Hi I am new to Oracle. How do I do a simple statement, for example get product id from the last 30, or 20 days purchase date?
SELECT productid FROM product
WHERE purchase_date ?
SELECT productid FROM product WHERE purchase_date > sysdate-30
The easiest way would be to specify
SELECT productid FROM product where
purchase_date > sysdate-30;
Remember this sysdate above has the time component, so it will be purchase orders newer than 03-06-2011 8:54 AM based on the time now.
If you want to remove the time conponent when comparing..
SELECT productid FROM product where purchase_date > trunc(sysdate-30);
And (based on your comments), if you want to specify a particular date, make sure you use to_date and not rely on the default session parameters.
SELECT productid FROM product where
purchase_date >
to_date('03/06/2011','mm/dd/yyyy')
And regardng the between (sysdate-30) - (sysdate) comment, for orders you should be ok with usin just the sysdate condition unless you can have orders with order_dates in the future.
Pay attention to one aspect when doing "purchase_date>(sysdate-30)": "sysdate" is the current date, hour, minute and second. So "sysdate-30" is not exactly "30 days ago", but "30 days ago at this exact hour".
If your purchase dates have 00.00.00 in hours, minutes, seconds, better doing:
where trunc(purchase_date)>trunc(sysdate-30)
(this doesn't take hours, minutes and seconds into account).
Try this : Using this you can select data from last 30 days
SELECT
*
FROM
product
WHERE
purchase_date > DATE_SUB(CURDATE(), INTERVAL 1 MONTH)
SELECT COUNT(job_id) FROM jobs WHERE posted_date < NOW()-30;
Now() returns the current Date and Time.
select status, timeplaced
from orders
where TIMEPLACED>'2017-06-12 00:00:00'