0
SELECT

M.Id_x as Id 
max(case when SA.TYP = 'CHRG' then    SA.AMT end)  CHRG,

max(case when SA.TYP = 'NTCV' then SA.AMT end) NC,

max(case when SA.TYP = 'COV' then SA.SRC end) COV

FROM database.tableA M
LEFT OUTER JOIN  
database.tableB SA

On
(SA.Id_x = M.id_x
AND SA.date = m.date
AND SA.SRC=M.SRC)

Where M.date >= '2018-01-01'
And m.src = 'ox'
And sa.type IN ('CHRG', 'NTCV', 'COV')

Group by 
M.id_x 
M.date

Known NTCV/COV can = '?' Or numeric value

Background The reason I use a max case when argument is to work how the database associates each Id with a type and each type may or may not have a numeric value. I dont want the data to have so many rows as there are other tables joined. For simplicity I'm only showing these 2. Further more the max case when argument allows the data to result as a row versus multiple rows.

Issue At times I might need to query for multiple scenarios where the NTVC = COV but I am unsure as to how to that in the where clause. If I try to write it as such it gives me an error. If I try to call the same table 3 times then it uses to much CPU and spools.

I am interested how you would rewrite this query in order to accomplish

Where ntvc = cov for expected results of sometimes 1m rows

Technology used: teradata sql assistant

4

1 回答 1

0

我猜你想要一个HAVING子句,而不是一个WHERE子句,带有:

having (max(case when SA.TYP = 'NTCV' then SA.AMT end) = 
        max(case when SA.TYP = 'COV' then SA.SRC end) 
       )
于 2018-12-07T12:00:41.923 回答