This is my SQL query:
'{select id,value,
case when temp.country-id=2 then
interval 4 hour + cast(time as time ) else
interval 3 hour + cast(time as time ) end as TIME
from temp}'
This is the output I am getting for above query:
I want this output using SQL:
Related
I have Hours,Minute,Second, and days as results in the Postgres query. I want to convert everything into hours.
Example
Row 1 result: 19:53:45
Row 2 result: 1 day 05:41:58
Now I want to convert days into hours like below
Row 1 result:19:53:45
Row 2 result: 29:41:58
Can someone help me how to do it in the postgres sql?
cast(col as interval hour to minute) should work, according to Standard SQL.
Anyway, this seems to work:
col - extract(day from col) * interval '1' day -- remove the days
+ extract(day from col) * interval '24' hour -- and add back as hours
See fiddle
Presumably, you want the result as a string, because times are limited to 24 hours. You can construct it as:
select *,
(case when ar[1] like '%day%'
then (split_part(col, ' ', 1)::int * 24 + split_part(ar[1], ' ', 3)::int)::text ||
right(col, 6)
else col
end)
from (values ('19:53:45'), ('1 day 05:41:58')) v(col) cross join lateral
regexp_split_to_array(col, ':') a(ar);
You can also do this without a:
select *,
(case when col like '%day%'
then (split_part(col, ' ', 1)::int * 24 + (regexp_match(col, ' ([0-9]+):'))[1]::int)::text ||
right(col, 6)
else col
end)
from (values ('19:53:45'), ('1 day 05:41:58')) v(col) ;
I have the following table:
id | time
----+-------------
1 | 21:00:00+01
2 | 22:00:00+01
3 | 23:00:00+01
Column id is of type integer and time is time with timezone. I want to select all rows that fall within a specified interval, e.g.,
select *
from times
where time >= time '22:30' - interval '60 minutes' and time <= time '22:30' + interval '60 minutes';
However, if the intervall extends past midnight, i.e., when I select 23:30 as time argument, then I get an empty result set.
Is there a way to tell postgress to ignore the minutes that span past midnight?
You can use this logic:
select *
from times t cross join
(values ('22:30'::time - interval '60 minutes', '22:30'::time + interval '60 minutes')
) v(fromt, tot)
where (fromt <= tot and time >= fromt and time <= tot) or
(fromt > tot and (time >= fromt or time <= tot))
I have sql query which returns record by date specified
What I want to do is group them by 1 hour interval
My query returns a date and interval.
interval value looks like this
8:00,8:30,9:00,9:30,10:00
as you can see the interval has produce 5 value what I want to do is group them by this
8:00-9:00,9:00-10:00
I have designed a query:
SELECT DATEPART(HOUR,VC.DATE+ VC.INTERVAL) AS DATE
,DATEPART(HOUR,VC.INTERVAL) AS INTERVAL
FROM VMUK_Q1R_IB_CONSOLIDATED VC
But the problem with this it display like this 8,8,9,9,10
How to I achieve this?
What you need is to create a set of hourly values and join back to it based on the hour part of your value. This will make sure the missing 'buckets' are represented. The following CTE will give you the lookup for 24 hours - you could do the same thing with a static lookup table too.
with ranges
as
(
select 0 as value
union all
select r.value+ 1 from ranges r where r.value <= 24
)
select
r.value start
from ranges r
You could fix this by a calculation or formatting. I think formatting would be simpler for this example. Try this:
SELECT Convert(VarChar(20), DATEPART(HOUR,VC.INTERVAL)) + ':00' AS DATE
,DATEPART(HOUR,VC.INTERVAL) AS INTERVAL
FROM VMUK_Q1R_IB_CONSOLIDATED VC
If you want the full date + time shown, rounded down, try this:
SELECT Convert(VarChar(20), VC.Date, 101) + Convert(VarChar(20), DATEPART(HOUR,VC.INTERVAL)) + ':00' AS DATE
,DATEPART(HOUR,VC.INTERVAL) AS INTERVAL
FROM VMUK_Q1R_IB_CONSOLIDATED VC
If you want time ranges too, try this:
SELECT Convert(VarChar(20), VC.Date, 101) + Convert(VarChar(20), DATEPART(HOUR,VC.INTERVAL)) + ':00' AS DATE,
,DatePart(HOUR,VC.INTERVAL)) + ':00 - ' + DatePart(HOUR, DateAdd(HOUR, VC.INTERVAL, 1)) + ':00' AS TimeRange
,DATEPART(HOUR,VC.INTERVAL) AS INTERVAL
FROM VMUK_Q1R_IB_CONSOLIDATED VC
SELECT
DATEPART(HOUR,VC.DATE+ VC.INTERVAL) AS DATE,
case DATEPART(HOUR,VC.INTERVAL)
when 0 then '00:00-00:59'
when 1 then '01:00-01:59'
.
.
etc.
.
.
when 22 then '22:00-22:59'
when 23 then '23:00-23:59'
end AS INTERVAL
FROM VMUK_Q1R_IB_CONSOLIDATED VC
I have a table with over 100,000 rows that contain the following columns: ID, Time, and Boolean.
The time column tracks time down to the second.
I need a query that will find all instances of Boolean = 1 for every 5 minute interval of time from the start of the table to the end, then group the count by time interval.
The table represents 4 hours of data, so I should get 48 rows of results.
I'm using MS SQL Server.
I've tried a few approaches, but the time interval logic is giving me a hard time.
EDIT: I figured it out!
SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, timestamp)/5 * 5,0), COUNT(*)
FROM table
WHERE isrepeat = 1
GROUP BY by DATEADD(MINUTE, DATEDIFF(MINUTE, 0, timestamp)/5 * 5,0)
ORDER BY by 1
This should do. You can group by your results by an interval of time/5.
select
cast(to_char(Time, 'mi') as int) / 5 * 5 || ' - ' || cast(to_char(Time, 'mi') as int) / 5 * 5 + 5 as "Interval",
count(1)
from tableName
where
Boolean = 1
group by
cast(to_char(Time, 'mi') as int) / 5
select * from tableName where Time
BETWEEN DATEADD(minute, -5, current_timestamp)
AND current_timestamp
I want to have this query:
SELECT DATE(DATE_SUB(DATE('2010-09-10'), (CASE DATETYPE WHEN 'H' THEN INTERVAL 1 WEEK ELSE INTERVAL 1 YEAR END CASE))) AS wdt
MySQL says it's invalid
also I have tried
SELECT _DATE AS wdt
UNION ALL
CASE DATETYPE
WHEN 'H' THEN SELECT DATE(DATE_SUB(_DATE, INTERVAL 1 YEAR)) AS wdt ;
ELSE SELECT DATE(DATE_SUB(DATE('2010-09-10'), INTERVAL 1 WEEK )) AS wdt;
END CASE;
UNION ALL
SELECT DATE(DATE_SUB(_DATE, INTERVAL 2 WEEK)) AS wdt
which doesn't work also, error in query syntax.
How can I select interval 1 year for dates that are Holidays.
Using two DATE_SUB operations would work:
SELECT IF (DATETYPE = 'H',
DATE_SUB(DATE('2010-09-10'), INTERVAL 1 WEEK),
DATE_SUB(DATE('2010-09-10'), INTERVAL 1 YEAR))
as wdt;