Search code examples
sqlamazon-redshiftamazon-redshift-spectrum

How to join 2 tabels in order to receive all the data needed


I have 2 queries:

/*+ ETLM
{
  depend:{
    replace:[
      {
        name:"table_1"
      }
    ]
  }
}
*/
SELECT 

case_id,
x,
x,
x,
x,
x

FROM table.1


WHERE resolved_date between TO_DATE ('2020/01/01', 'YYYY/MM/DD') and to_date('2020/12/31','YYYY/MM/DD')
AND ASSIGNED_TO_GROUP IN ('First Group')



and

/*+ ETLM
{
  depend:{
    replace:[
      {
      name:"table_2"
      }
    ]
  }
}
*/



SELECT

x,
x,
x,
case_id,
x,
x


FROM table_2

WHERE create_date between TO_DATE ('2020/01/01', 'YYYY/MM/DD') and to_date('2020/12/31','YYYY/MM/DD')

So basically I need all the info in the tables, case_id being the primary key on table_1. How can I join them to receive all the data and use only the WHERE's from the table_1?

PS: the tables are spectrum enabled on redshift

I tried

Select
x
x
x
FROM table_1 

JOIN table_2 ON table_1.case_id = table_2.case_id
Select 
x
x
x
FROM table_2

Where table_1.resolved_date between ...
AND table_1.assigned_group...

But it seems that I get an error on the second SELECT statement


Solution

  • Select
    table_1.x,
    table_1.x,
    table_1.x,
    table_2.x,
    table_2.x,
    table_2.x
    FROM table_1 
    
    JOIN table_2 ON table_1.case_id = table_2.case_id
    
    Where table_1.resolved_date between ...
    AND table_1.assigned_group...