SQL group by 7am to 7am - sql

How do I simply group by a 24 hour interval from 7am to 7am in a manner similar to:
select format(t_stamp,'yyyy-MMM')
from mytable
group by format(t_stamp,'yyyy-MMM')
if input is like
3,Wed Mar 23 20:40:40 EDT 2022
3,Wed Mar 23 20:40:39 EDT 2022
4,Wed Mar 23 03:36:10 EDT 2022
3,Wed Mar 22 15:46:44 EST 2022
3,Tue Mar 22 04:16:52 EST 2022
4,Sat Mar 22 03:13:08 EDT 2022
3,Sat Mar 22 03:13:05 EDT 2022
4,Sat Mar 21 04:10:36 EDT 2022
output should be like
6, Mar 23
7, Mar 22
10, Mar 21
4, Mar 20

Related

Databricks: replicate columns

Suppose I am having the following Dataframe :
YEAR MONTH Value
2019 JAN 100
2019 JAN 200
2019 MAR 400
2019 MAR 100
And I do the pivot group by YEAR. ( df.groupBy().pivot()....)
YEAR JAN MAR
2019 300 500
But I also wanted to replicate the column of the Months through out the year even there are no data in that month ...
which means I would like to have
YEAR JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
2019 300 0 500 0 0 0 0 0 0 0 0 0
Thanks

Convert or cast varchar rows like (Mon Jul 18 19:28:36 EDT 2018) To DateTime

I have a column varchar type with dates like:
Fri Mar 3 12:55:17 EST 2017
Thu Jul 27 10:12:07 EDT 2017
Fri Jul 21 12:11:35 EDT 2017
Wed Jan 31 13:15:34 EST 2018
And I would like to return just the date and time something like:
03/03/2017 12:55:17
07/27/2017 10:12:07
07/21/2017 12:11:35
01/31/2018 13:15:34
I tried several ways with substring and convert statement but nothing work.
Any assistance in this regard will be greatly appreciated.
Perhaps something like this
Example
Declare #YourTable table (SomeCol varchar(50))
Insert Into #YourTable values
('Fri Mar 3 12:55:17 EST 2017'),
('Thu Jul 27 10:12:07 EDT 2017'),
('Fri Jul 21 12:11:35 EDT 2017'),
('Wed Jan 31 13:15:34 EST 2018')
Select *
,AsDateTime = try_convert(datetime,substring(SomeCol,4,len(SomeCol)-11)+right(SomeCol,4))
From #YourTable
Returns
SomeCol AsDateTime
Fri Mar 3 12:55:17 EST 2017 2017-03-03 12:55:17.000
Thu Jul 27 10:12:07 EDT 2017 2017-07-27 10:12:07.000
Fri Jul 21 12:11:35 EDT 2017 2017-07-21 12:11:35.000
Wed Jan 31 13:15:34 EST 2018 2018-01-31 13:15:34.000

Select where day not in between of daytime and End_date

I have following data:
Table1:
Code code_id day
F23 123df 16 Jul 2016
F23 123df 17 Jul 2016
F23 123df 18 Jul 2016
F23 123df 19 Jul 2016
F23 123df 20 Jul 2016
F24 124df 16 Jul 2016
F24 124df 17 Jul 2016
F24 124df 18 Jul 2016
F24 124df 19 Jul 2016
F24 124df 20 Jul 2016
Table2:
Code code_id status daytime End_date
F23 123df down 16 Jul 2016 06:00 am 18 Jul 2016 08:00 pm
F23 123df down 19 Jul 2016 05:00 am 21 Jul 2016 03:45 pm
F23 123df down 23 Jul 2016 02:40 am
I need to select data from table1 where day not in between of daytime and End_date from table2 and if table1 day (like '16 Jul 2016' is the same day as '16 Jul 2016 06:00' from table2) is like table2 day from daytime or End_date fields then it still has to select data for this day. In case of End_date is null then it has to be sysdate - nvl(End_date,sysdate).
For example when select for day = '16 Jul 2016' from table1 it has to be:
F23 123df 16 Jul 2016
F24 124df 16 Jul 2016
But when select for '17 Jul 2016' then it has to be:
F24 124df 17 Jul 2016
When select for '18 Jul 2016' it has to be:
F23 123df 18 Jul 2016
F24 124df 18 Jul 2016
thanks,
S
SELECT *
FROM table1 t
WHERE NOT EXISTS (
SELECT 1
FROM table2 x
WHERE t.code = x.code
AND t.code_id = x.code_id
AND t.day >= x.daytime
AND t.day + INTERVAL '86399' SECOND <= x.end_date
);

presto sql: select the data that before or after a datetime

There is table called t1, and there are columns id, created_at, text, for example, as following table:
id created text
1 Thu Jun 30 01:00:57 +0000 2016 I like this movie1
2 Thu Jun 30 02:59:57 +0000 2016 I like this movie2
3 Thu Jun 30 03:49:57 +0000 2016 I like this movie3
4 Thu Jun 30 04:59:50 +0000 2016 I like this movie4
5 Thu Jun 30 05:39:57 +0000 2016 I like this movie5
6 Thu Jun 30 06:39:57 +0000 2016 I like this movie6
7 Thu Jun 30 06:29:57 +0000 2016 I like this movie6
8 Thu Jun 30 07:09:57 +0000 2016 I like this movie7
9 Thu Jun 30 07:39:57 +0000 2016 I like this movie8
10 Thu Jun 30 08:39:57 +0000 2016 I like this movie9
11 Thu Jun 30 09:39:57 +0000 2016 I like this movie10
12 Thu Jun 30 10:29:57 +0000 2016 I like this movie11
13 Thu Jun 30 11:29:57 +0000 2016 I like this movie12
12 Thu Jun 30 12:29:57 +0000 2016 I like this movie13
I want to select data separated by hour time.
For example, I want to select all the data that hour is less or equal 06, then I want to select the data that hour is more than 07. Since the data of column is datetime form: Thu Jun 30 12:29:57 +0000 2016, I don't know how to deal with this. Thanks for your help!
The sql is presto(presto sql):
select id, created, text from t1 where created_at <= 6
You could use datepart for this if you are using mssql:
select
id,
created,
text
from
t1
where
datepart(hour, created) <= 6
References:
DATEPART (Transact-SQL)
I done it, use the hour(datestamp)can solve it.
select id, created, text from t1 where hour(created_at) <= 6

Extract values from IN parameter and store it in local variables in postgreSQL

I have :listDate:String IN parameter that I'm passing to my proc which contains the dynamic values separated by ,
This :listDate:String is passed to proc from Java front end and it contains the value selected by the user
There can be many combinations
Case 1 listDate can have
1. 30 Dec 2013 to 05 Jan 2014
so v_start_date is 30 Dec 2013 and
v_end_date is 05 Jan 2014
Case 2 listdate can have
30 Dec 2013 to 05 Jan 2014,
06 Jan 2014 to 12 Jan 2014
so v_start_date is 30 Dec 2013 and
v_end_date is 12 Jan 2014
Case 3 listDate can have
06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
so v_start_date is 06 Jan 2014 and
v_end_date is 26 Jan 2014
How can I extract the values as shown above into v_start_date and v_end_date ?
I was able to resolve it by using
select substring(listDate from '............$') as v_end_date,
substring(listDate from '^...............') as v_start_date