Skip to content
Advertisement

JOINING the Same Tables in SQL

I have a table with 4 columns such as Customer ID, Person ID, Year, Unit Cost.

enter image description here

I want to join the same table with all the years from table and keep the all years for all customer ID and PErson ID’s. IF there is no data in the table for respective customer ID and PErson ID then I want the Cost as NULL. Expected Data:

enter image description here

Advertisement

Answer

If I understand correctly, you can use a cross join to generate the rows and a left join to bring in the existing data:

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