I am trying to select all rows that are within a date range including the start and end day -
for example
Select *
from table
where timestamp between 2019-03-01 and 2019-03-08
I want all rows that are on 2019-03-01 and 2019-03-08 and all rows between the two dates as well
Thanks
You should use date() for timestamp and proper quote around the date value
SELECT *
FROM tbl_recordings
WHERE date(timestamp)
between str_to_date('2019-03-01', '%Y-%m-%d')
and str_to_date('2019-03-08', '%Y-%m-%d');
or
SELECT *
FROM tbl_recordings
WHERE date(timestamp) between '2019-03-01' and '2019-03-08';
Related
Can anyone help me with SSIS Expression
I have this query in the expression:
Select period, * from table
I want to add a where clause to get period = yesterday
But, period column is in julian date format.
at the end i want the same result like this query
Select period, * from table
Where getdate() - 1
Thank you
you only can use you table date time column to compare with the getdate
Select period,
dataDate, *
from table
Where dataDate = getdate() - 1
Say I have a query that selects all the sales from the past 90 days. I want to be able to isolate certain rows on a case/when basis, and can't quite figure out how to do this. The case statement is depending on dates, so: If the date falls between 3/1 and 5/31, then I want to select the sales from any month ends (3/31, 4/30, 5/31 and TODAY) otherwise, if the date is not between 3/1 and 5/31, then I just want to select the past 3 month-ends.
What I tried so far is inserting a Case/When statement in the WHERE clause, but that doesn't seem kosher. Is there another way to go about this?
For reference, the #monthends table contains the following single column:
monthends
2019-03-31
2019-02-28
2019-01-31
and the #insideRule table contains similarly:
insiderRule
2019-03-31
2019-04-22
The query:
SELECT *
FROM mytable
WHERE asofdate IN
CASE WHEN asofdate BETWEEN '3-1-2019' AND '5-31-2019' THEN
(SELECT * FROM #insideRule)
ELSE
(SELECT * FROM #monthends)
END
When I execute the above, I get syntax errors around "IN"
You want exists not case expression :
IF EXISTS (SELECT 1 FROM mytable WHERE aasofdate BETWEEN '2019-03-01' AND '2019-05-31')
SELECT *
FROM #insideRule
ELSE
SELECT *
FROM #monthends
I am thinking you want something like this:
SELECT ir.*
FROM #insideRule ir
WHERE getdate() >= '2019-03-01' AND
getdate() < '2019-06-01'
UNION ALL
SELECT me.*
FROM #monthends me
WHERE getdate() < '2019-03-01' OR
getdate() >= '2019-06-01';
This assumes that the two tables have the same columns in the same order with compatible types.
I know how I would do this with something like SAS, but if I wanted to create a table that had as many rows as there were month intervals derived from this statement:
cast((cast(2017-03-31 as date) - cast(2016-01-31 as date) month(4)) as int) as date_range
....to give an output like this:
2017-03-31
2017-02-28
2017-01-31
2017-12-31
2017-11-30
2017-10-31
2017-09-30
2017-08-31
2017-07-31
2017-06-30
2017-05-31
2017-04-30
What statement would I need to do this in Teradata?
Thanks
Are those dates calulated based on existing columns?
Or do you just need that list?
In both cases you can utilze Teradata's proprietary EXPAND ON feature:
SELECT BEGIN(pd)
FROM SYS_CALENDAR.CALENDAR -- your table here
WHERE calendar_date = DATE -- EXPAND requires FROM, so this is just to get a single row
EXPAND ON PERIOD(date '2016-01-31' -- start date
,date '2017-03-31' + 1 -- end date (+1 because it's not included in the date range)
) AS pd BY ANCHOR PERIOD MONTH_END -- one row for each month end within the period
It is safer to get the first of the month because adding months to the last day of the month can be problematic (if you started with '2016-02-29' you would get the 29th of succeeding months).
You can do what you want with a recursive cte:
with recursive cte(dte) as (
select cast('2016-02-01' as date)
union all
select add_months(cte.dte, 1)
from cte
where dte <= '2017-05-01'
),
dates as (
select dte - interval '1 day'
from cte
)
. . .
this is my table , name is resource_calendar.
i want to select resource_id which have effective date less then or equal to current date and most closest date to current date.
what will be the right query in postgresql?
query will
select effective date 22 for resource_id=3 and effective date 21 for resource_id=7
so result should be
id resource_id calendar_id applied_on effective_date version
19 3 6 2016-12-22 11:13:26.53 2016-12-22 0
26 7 5 2016-12-22 11:16:26.53 2016-12-21 0
SELECT t.*
FROM
(
SELECT id, resource_id, calendar_id, applied_on, effective_date, version,
MIN(ABS(EXTRACT(EPOCH FROM (current_timestamp - effective_date))))
OVER (PARTITION BY resource_id) AS diff
FROM resource_calendar
WHERE EXTRACT(EPOCH FROM (current_timestamp - effective_date)) > 0
) t
WHERE ABS(EXTRACT(EPOCH FROM (current_timestamp - t.effective_date))) = t.diff
This query forms a partition by resource_id on the resource_calendar. You can think of this partition as a logically grouping records together which have the same resource_id. For each such group of records, it computes the smallest difference between the effective_date and the current timestamp, where the effective_date be earlier than the current timestamp.
The outer query then identifies those records having this minimum timestamp difference.
Postgres has some reasonably helpful documentation on using window functions if you feel you need more information.
You can use this. A simple query
SELECT DISTINCT ON(resource_id) *
FROM planner.resource_calendar
WHERE effective_date <= CURRENT_DATE
ORDER BY resource_id, effective_date desc;
(Please note that I have seen a similar question on StackOverflow recently, however I can not find it anywhere - if anyone can find it please post the link)
I have a table that has a datetime field in it. For example:
Table data
DateTime date;
int number;
I need to find the difference between date in a row and date in the next row. I have tried with a query:
select date as current_date, date - (select top 1 date where date > current_date from data) as time_difference from date
however this won't work, because of "current_date". Is there a way I can do this with one query?
SELECT date AS current_date, next_date - date AS difference
FROM mytable mo
CROSS APPLY
(
SELECT TOP 1 date AS next_date
FROM mytable mi
WHERE mi.date > mo.date
ORDER BY
date
) q