Skip to content
Advertisement

SQL to create data as table when there is no relation in SQL

I have following tables

Table 1 i have rates

Table 2 i have Dates

Table 3 i have data which show price of rates which are applicable between start and end

i want to create table 4 with SQL query in which i have every

rate (from table 1) + every date (from table 2) + if that date exist between start and end price other wise 0 display as table 4 below

i dont know how its achievable in SQL query as i am new in SQL and how to link if there are no relation between table

Table 1

Rate ID
ConfD1 46
ConfD2 47

Table 2

Dates
15-09-2018
16-09-2018
17-09-2021
18-09-2021
19-02-2022

Table 3

Rate ID startdate enddate price
ConfD1 46 01-01-2021 31-10-2021 111
ConfD1 46 01-11-2021 01-03-2022 222
ConfD2 47 01-01-2021 31-10-2021 333
ConfD2 47 01-11-2021 01-03-2022 444
ConfD3 48 01-01-2021 31-10-2021 555
ConfD3 48 01-11-2021 01-03-2022 666

Table 4

Rate date price
ConfD1 15-09-2018 0
ConfD1 16-09-2018 0
ConfD1 17-09-2021 111
ConfD1 18-09-2021 111
ConfD1 19-02-2022 222
ConfD2 15-09-2018 0
ConfD2 16-09-2018 0
ConfD2 17-09-2021 333
ConfD2 18-09-2021 333
ConfD2 19-02-2022 444

Advertisement

Answer

Nice job sharing data and desired output. It would be easier if next time you make your data consumable. Something like this.

This query will return the data you are looking for.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement