JOIN with tables without relationship - sql

I have a report that contains many JOINS and the base of the report is a table called “table_class” (c). What I need is to compare a Date Time field of this table (table_class) with another table called “table_period”. The problem is that the table “table_period” has no relationship with the “table_class”. I need to show the PK of the “table_period” when the Start Date field (Date Time) of “table_class” is between the Start Date and End Date (both Date Time) from “table_period”.
Basically, I want to show the period by class according the period that the class occurs/occurred.
Important: the “table_period” is flexible at user level, because the period does not have fixed dates.
For example:
1st semester 2016: from Jan 01, 2016 to Jun 30, 2016
2nd semester 2016: from Jul 01, 2016 to Dec 31, 2016
1st semester 2017: from Feb 01, 2017 to Jul 31, 2017
2nd semester 2017: from Aug 01, 2017 to Jan 31, 2018
And go on…So, I can’t fix the period.
How can we do this? Is it possible to create a JOIN with this scenario or even solving with a CASE expression?
To clarify better, we can see an example of those tables bellow.
Table 1: table_class (c) (reminding that this table is the base of the report)
CLASS_ID | START_DTE | END_DTE
0001 | Jun 29, 2017, 8:00 AM | Jun 29, 2017, 3:30 PM
0002 | Jan 11, 2018, 8:00 AM | Jan 12, 2018, 3:30 PM
...
Table 2: table_period (p)
PERIOD_ID | START_DTE | END_DTE
1st semester 2016 | Jan 1, 2016, 3:00 AM | Jun 30, 2016, 1:00 AM
2nd semester 2016 | Jul 1, 2016, 1:00 AM | Dec 31, 2016, 3:00 AM
1st semester 2017 | Feb 1, 2017, 3:00 AM | Jul 31, 2017, 1:00 AM
2nd semester 2017 | Aug 1, 2017, 1:00 AM | Jan 31, 2018, 3:00 AM
...
What I expect
c.CLASS_ID | p.PERIOD_ID | c.START_DTE | c.END_DTE
0001 | 1st semester 2017 | Jun 29, 2017, 8:00 AM | Jun 29, 2017, 3:30 PM
0002 | 2nd semester 2017 | Jan 11, 2018, 8:00 AM | Jan 12, 2018, 3:30 PM
...
Thank you!

You can do a join like this:
select c.*, p.period_id
from table_class c left join
table_period p
on c.start_dte < p.end_dte and c.start_dte >= p.start_dte;
This assigns the period -- if any -- based on when the class starts.

Related

how do you extract a variable that appears multiple times in a table only once

I'm trying to extract the name of space organisations from a table but the closest i can get is the amount of times it appears next to the name of the organisation but i just want the name of the organisation not the amount of times it is named in the table.
if you can help me please leave a comment on my google colab.
https://colab.research.google.com/drive/1m4zI4YGguQ5aWdDVyc7Bdpr-78KHdxhR?usp=sharing
What I get:
variable number
organisation
time of launch
0
SpaceX
Fri Aug 07, 2020 05:12 UTC
1
CASC
Thu Aug 06, 2020 04:01 UTC
2
SpaceX
Tue Aug 04, 2020 23:57 UTC
3
Roscosmos
Thu Jul 30, 2020 21:25 UTC
4
ULA
Thu Jul 30, 2020 11:50 UTC
...
...
...
4319
US Navy
Wed Feb 05, 1958 07:33 UTC
4320
AMBA
Sat Feb 01, 1958 03:48 UTC
4321
US Navy
Fri Dec 06, 1957 16:44 UTC
4322
RVSN USSR
Sun Nov 03, 1957 02:30 UTC
4323
RVSN USSR
Fri Oct 04, 1957 19:28 UTC
etc
etc
etc
What I want:
organisation
RVSN USSR
Arianespace
CASC
General Dynamics
NASA
VKS RF
US Air Force
ULA
Boeing
Martin Marietta
etc

SQL group by 7am to 7am

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

ordering a column based on calculated value

I have below values in a column
Q1 2018
Q2 2018
Q3 2018
Q4 2018
feb 2018
mar 2018
Q1 2019
Q2 2019
Q3 2019
jan 2018
sep 2018
dec 2018
jan 2019
feb 2019
mar 2019
I have above values which gets calculated on some parameters. for some data this value comes with month and for some this comes as quarter.
Is there any way to order them using order by when all the values are on the same column, means monthly values and quarterly value both should be sorted.
output should be like
Q1 2018
Q2 2018
Q3 2018
Q4 2018
Q1 2019
Q2 2019
Q3 2019
jan 2018
feb 2018
mar 2018
sep 2018
dec 2018
jan 2019
feb 2019
mar 2019
I think this does what you want:
order by len(col), -- put the quarters first
substr(col, 4), -- order by year
(case when col not like 'Q%'
then to_date(col, 'MON YYYY')
end), -- order months by date
col -- order quarters by quarter

Changing date format for PostgreSQL query result

I have following query
select substring(listDate from '............$') as v_end_date,
substring(listDate from '^...............') as v_start_date
Now listDate value can be like
select substring('06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
' from '............$') as v_end_date,
substring('06 Jan 2014 to 12 Jan 2014,
13 Jan 2014 to 19 Jan 2014,
20 Jan 2014 to 26 Jan 2014
' from '^............') as v_start_date
Above query results in
V_END_DATE V_START_DATE
26 Jan 2014 06 Jan 2014
Now I need to have v_end_date and v_start_date format like yyyy-mm-dd and like
Mon 06 Jan 2014.
Convert your string to an actual date with to_date() and use to_char() to get pretty much any format you like.
Demo:
SELECT to_char(day, 'YYYY-MM-DD') AS format1
, to_char(day, 'Dy DD Mon YYYY') AS format2
FROM (SELECT to_date('26 Jan 2014', 'DD Mon YYYY') AS day) sub

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