DATA Step, Macro, Functions and more

Queries

Reply
Super Contributor
Posts: 673

Queries

What is the difference betwee the two queries:

create table _master
as
                 select
                        a.ACCT_NO
                        ,max(a.ACCT_FIRST_NM) as ACCT_FIRST_NM
                        ,max(a.ACCT_MID_NM) as ACCT_MID_NM
                        ,max(a.ACCT_LAST_NM) as ACCT_LAST_NM
                        ,max(case when b.CUST_NAT_ID_CD ne ' '  then catx('-',d.iso_cntry_cd,b.CUST_NAT_ID_CD) else ' ' end) as CUST_NAT_ID
                        ,max(a.BIRTH_DT) as BIRTH_DT
      ,max(a.CUST_ID) as a_amex
      ,max(a.MKT_ID_cd) as MKT_ID_cd

from outdata1.CRPS311 a left join outdata1.crps311_canada b on a.acct_no=b.acct_no
left join outdata1.isocodes d on b.mkt_id_cd=d.mkt_id_cd
where a.ACCT_FIRST_NM ne '' or a.ACCT_MID_NM ne  '' or a.ACCT_LAST_NM ne ''
group by a.ACCT_NO

create table _mas
as
                 select
                        a.ACCT_NO
                       ,max(case when a.ACCT_FIRST_NM ne '' then a.acct_first_nm end) as ACCT_FIRST_NM
                      ,max(case when a.ACCT_MID_NM ne '' then a.ACCT_MID_NM end) as ACCT_MID_NM
                      ,max(case when a.ACCT_LAST_NM ne '' then a.ACCT_LAST_NM end) as ACCT_LAST_NM
                        ,max(case when b.CUST_NAT_ID_CD ne ' '  then catx('-',d.iso_cntry_cd,b.CUST_NAT_ID_CD) else ' ' end) as CUST_NAT_ID
                        ,max(a.BIRTH_DT) as BIRTH_DT
      ,max(a.CUST_ID) as a_amex
      ,max(a.MKT_ID_cd) as MKT_ID_cd

from outdata1.CRPS311 a left join outdata1.crps311_canada b on a.acct_no=b.acct_no
left join outdata1.isocodes d on b.mkt_id_cd=d.mkt_id_cd
group by a.ACCT_NO

Super User
Posts: 5,438

Re: Queries

First, quite odd to do max() on names...?

Since there are else in the case, the result would be the same as if you omit the case (since if name is " " won't be true for the then, and the expression will result in " " anyway).

And yes, I think that the result of two queries can differ.

The where clause will filter out accounts that doesn't have any name, the second one will return all accounts, independent on the existence of any names.

What is the purpose?

Data never sleeps
Super Contributor
Posts: 673

Re: Queries

The purpose is to get a single row for each acct_no

Super User
Posts: 5,438

Re: Queries

Then don't use case and where.

Data never sleeps
Ask a Question
Discussion stats
  • 3 replies
  • 174 views
  • 0 likes
  • 2 in conversation