Im trying to find out how many clients viewed a property within 14 days of May 20, 2004, either before or after. Not really sure at all how to go about this.
Im assuming i need to group it and use a having?
EDIT: I am using oracle now
select count(*)
from VIEWING
WHERE CLAUSE?
For a one time query with that specific date,
select count(*) clients
from yourtable
where yourdatefield >= {d'2004-05-06'}
and yourdatefield < {d'2004-06-08'}
You might want to consult a calendar to see if those dates are correct.
Edit #1, since you are using Oracle, you can use:
select count(*) TotalClients
from yourtable
where dt >= (to_date('2004-05-20', 'yyyy-mm-dd') - INTERVAL '14' DAY)
and dt <= (to_date('2004-05-20', 'yyyy-mm-dd') + INTERVAL '14' DAY)
See SQL Fiddle with Demo
Based on some of your previous questions you were using MySQL.
If you are using MySQL then you can use the DATE_ADD() function to get the date range and then use count(*) to return all records from those dates:
select count(*) TotalClients
from yourtable
where dt >= date_add(str_to_date('2004-05-20', '%Y-%m-%d'), INTERVAL -14 day)
and dt <= date_add(str_to_date('2004-05-20', '%Y-%m-%d'), INTERVAL 14 day)
Related
I have the following code:
Select * from table
where to_date <= ( select max(to_date)
FROM table)
and to_date >= (select (max(to_date)::date - interval '6 months')::date as to_date
FROM table)
Basically, I am trying to look at all the results between the max date and then 6 months in the past, and I tried doing that by making 2 sub queries.
I seem to get null, but oddly enough, if add the regular date that
(select (max(to_date)::date - interval '6 months')::date
is giving and paste it as >='yyyy-mm-dd', the query seems to be working fine. It is weird as both sub-queries are actually spitting out date format results and have no idea why its giving this.
You don't need both comparison:
select *
from table
where to_date >= (select (max(to_date)::date - interval '6 months')::date as to_date
from table
) ;
This is assuming that the table reference is the same in both the inner and outer query.
I can't really think of a reason why this wouldn't work, but you can rewrite the query to only run a single sub-query, which is also more efficient:
select t.*
from the_table t
cross join (
select max(the_date) as max_date
from the_table
) mt
where t.to_date <= mt.max_date
and t.to_date >= mt.max_date - interval '6 months'
Here is my postgresql statement.
select sum("Color") as color,round(avg("Color")) as avg_color
from "color_table"
How can I set the condition for avg part?
I done some study and i found interval seems very useful. But I dont know where to set the condition.
I just want to get the average for Color which trandate is in past 90 days from now.
Here is one approach:
SELECT
SUM(Color) AS color,
ROUND(AVG(CASE WHEN trandate < NOW() - INTERVAL '90 DAYS'
THEN Color END)) AS avg_color
FROM "color_table";
This interprets your requirement as wanting the average of records where the trandate is older than 90 days from now. If instead you want within the last 90 days, then reverse the inequality.
If you are using Postgres 9.4 or later, you may also use the FILTER clause:
SELECT
SUM(Color) AS color,
ROUND(AVG(Color) FILTER (WHERE trandate < NOW() - INTERVAL '90 DAYS')) AS avg_color
FROM "color_table";
you can try like below
with cte as
( select SUM(Color) AS color,0 as avg_color
FROM "color_table"
union all
select 0, avg(Color ) FROM "color_table"
where trandate < NOW() - INTERVAL '90 DAYS'
) select max(color) as color ,max(avg_color) as avg_color from cte
Oracle SQL:
I been trying to get this some snippet of query working. When you run it, it prompts you for an hour, and displays no records. I don’t want the query to have a static hour as it will need to be run 4 times a day.
So from the ‘&date’ input, I want it to show data for the past 24 hours. Is that possible?
dt_time = timestamp(6) field
select distinct to_char(dt_ time,'dd/mm/yyyy hh24'), fault_description
from order
where to_char(dt_time,'hh24') <= '&date' -24
order by to_char(dt_ time,'dd/mm/yyyy hh24');
Example, if you enter 10 (when query executed) it will show the data from 10(:00) through to 10(:00)next day
[Hope there is enough info for someone to answer, please]
Use a combination of between and interval:
where dt_time between
to_date(&date, 'ddmmyyyy hh24') and
to_date(&date, 'ddmmyyyy hh24') + interval '24' hour
You'll need to pass/parse the input date as a full date and not only the hour part, in order to prevent unexpected results.
You can use NUMTODSINTERVAL in a CTE to get the hour as user input.
WITH t_hour( h ) AS
( SELECT NUMTODSINTERVAL(&d,'HOUR' ) h FROM DUAL
)
SELECT DISTINCT TO_CHAR(dt_time,'dd/mm/yyyy hh24'),
fault_description
FROM ORDER
CROSS JOIN t_hour
WHERE dt_time BETWEEN TRUNC(SYSDATE) + h AND TRUNC(SYSDATE) + 1 + h
ORDER BY 1;
You need to make use of SYSDATE as you are fetching last one day data.
Also if you add or subtract number with a date column the offset will be number of days, not number of hours. So you need to use &date/24
Try this:
select distinct to_char(dt_time,'dd/mm/yyyy hh24'), fault_description
from order
where dt_time - &date / 24 between TRUNC(sysdate-1) and TRUNC(sysdate)
order by to_char(dt_ time,'dd/mm/yyyy hh24');
Please comment.
WHERE DATEDIFF(SYSDATE,DT_TIME) * 24 < &DATE AND DT_TIME < SYSDATE
On our PostgresSQL Database we would like to run a query.
Select (*)
Where
domain.blabla.date > 'yesterday'
AND
domain.blabla.date < 'current_date'
The goal is to get only yesterday's data.
The error we get is the following:
[Err] ERROR: date/time value "current" is no longer supported
Do you see a way to solve it?
Thank you very much!
Luca
For The goal is to get only yesterday's data:
Select (*)
from domain.blabla
Where date_trunc('day', date) = date_trunc('day', current_date - 1);
This query will fetch ONLY yesterday's value
select * from table_name where day > current_date- interval '1 day'
OR using yesterday
select * from table_name where day > current_date 'yesterday'
SQLFiddle
Refer to PostgreSQL Date/Time Documentation for more on this
P.S - You don't have to put the function in single quotes ' '
For the current date in SQL you can use
SELECT CONVERT(date, getdate())
For yesterday date in SQL you can use
SELECT dateadd(day,datediff(day,1,GETDATE()),0)
I have a table of 20000 records. each Record has a datetime field. I want to select all records where gap between one record and subsequent record is more than one hour [condition to be applied on datetime field].
can any one give me the SQL command code for this purpose.
regards
KAM
ANSI SQL supports the lead() function. However, date/time functions vary by database. The following is the logic you want, although the exact syntax varies, depending on the database:
select t.*
from (select t.*,
lead(datetimefield) over (order by datetimefield) as next_datetimefield
from t
) t
where datetimefield + interval '1 hour' < next_datetimefield;
Note: In Teradata, the where would be:
where datetimefield + interval '1' hour < next_datetimefield;
This can also be done with a sub query, which should work on all DBMS. As gordon said, date/time functions are different in every one.
SELECT t.* FROM YourTable t
WHERE t.DateCol + interval '1 hour' < (SELECT min(s.DateCol) FROM YourTable s
WHERE t.ID = s.ID AND s.DateCol > t.DateCol)
You can replace this:
t.DateCol + interval '1 hour'
With one of this so it will work on almost every DBMS:
DATE_ADD( t.DateCol, INTERVAL 1 hour)
DATEADD(hour,1,t.DateCol)
Although Teradata doesn't support Standard SQL's LEAD it's easy to rewrite:
select tab.*,
min(ts) over (order by ts rows between 1 following and 1 following) as next_ts
from tab
qualify
ts < next_ts - interval '1' hour
If you don't need to show the next timestamp:
select *
from tab
qualify
ts < min(ts) over (order by ts rows between 1 following and 1 following) - interval '1' hour
QUALIFY is a Teradata extension, but really nice to have, similar to HAVING after GROUP BY