Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Left join/keep between resident tables

Hi all,

I have the table structured as follows:

[HYP]:

n clientn carriercontractpolicypolicy_short
11112222253abc920/555GLOBAL
11112222253abc609/555GLOBAL
11112222253ghi258/555GENERAL
111122228965abc920/555
66667777253abc2920/555GLOBAL
66667777253ghi2875/555GENERAL
444455554986abc3609/555
444455554986abc3920/555

The last column [policy_short] is computed as follows:

if([n carrier] = '253', if(left([policy], 3)='920', 'Global', if(left([policy], 3)='609', 'Global', 'General'))) as [policy_short]

From the above, I have created another table structured as follows:

[TEMP]:

n clientn carrierFilter_Global
11112222253GLOBAL
66667777253GLOBAL

The script is:

LOAD [n client], [n carrier], 'GLOBAL' as [Filter_Global] resident [HYP] where [policy_short]='GLOBAL' and [n carrier] = '253';

Up to hear, great! (maybe I should put a group by somewhere above?)

If I load the script as it is, a Syn table is created between [n client] and [n carrier] on both tables, so that when I select 'GLOBAL' in the table [Filter_Global] I see all the contracts and policies belonging to those [n clients] that have at least one policy that has been marked as GLOBAL in the first table.

Next step is where I get stucked.

How can I build one unique table that holds all the values?

I'm looking for a table built as follows (I have dropped out the [policy_chort] field):

[HYP2]:

n clientn carriercontractpolicyFilter_Global
11112222253abc920/555GLOBAL
11112222253abc609/555GLOBAL
11112222253ghi258/555GLOBAL
111122228965abc920/555
66667777253abc2920/555GLOBAL
66667777253ghi2875/555GLOBAL
444455554986abc3609/555
444455554986abc3920/555


Now, how do I do this??? I believe the solution is a left join, but I just can't seem to make it work. How should I build the join? Is it a join or a keep?

I have built something like:

[HYP2]:

left join (HYP)

load * resident TEMP;

but the above duplicates rows and figures I haven't understood based on what. (I haven't written all the fields, but I have very many fields in the HYP table)

Thanks!!!!!

13 Replies
Luis_Cortizo
Former Employee
Former Employee

Well,

   For me, this kind of things are easier to see using a join.

If I reload using this code:

[hello]:

load

                    *

resident [HYP3];

left join (HYP3)

load

                    *

resident [temp];

 

//drop table [HYP3];

drop table [temp];

We don't drop the HYP3 table, because the first load is concatenating all the values... and we have the worst possible scenario.

Looking at a table object of the data we have this:

Codice  CapogruppoCodice GruppoNumero ContrattoPolizzapolizza_shortpremiumZ_Global
60067200025300156471106.1006032621106.1006032621750
600672000253001564710000000006100603262161006032621-750
60134900025300175842000000000000920A1521920A1521-19Z-Global
60134900025300175842000000000000920A1521920A152119.151Z-Global
60134900025300175842000000000000920A1521920A15216.500Z-Global
60134900025300175842000000000000920A1521920A15211.295Z-Global
60102300025300159645000000000000AXH32132AXH32132904
60244900025300173802000000000000920A8454920A8454235Z-Global
60244900025300173802000000000000920A8454920A845442.573Z-Global
60244900025300173802000000000000920A8454920A8454356Z-Global
60244900025300173802000000000000920A8454920A8454-356Z-Global
60244900025300173802000000000000920A8454920A84541.244Z-Global
60244900025300173802000000000000920A8454920A8454-1.912Z-Global
60070000025300156819000000000000209B7810209B7810120
60070000025300156830000000000000209C0451209C0451113
60070000025300156843000000000000209C1689209C1689111
03468000025320248328920B1161920B11613.272Z-Global
00477900025320248818000000000000209S9980209S9980404
00477900025320248818000000000000209S9980209S9980-404
00477900025320248668000000000000209U3340209U334088
00477900025320248668000000000000209U3340209U3340-88
60268700025300150666000000000000920A4627920A46270Z-Global
00874200025300153519000000000000209C2279209C227991

23 possible rows, but the =count(total Polizza) is returning 156.

But...

Modifying this line of code:

HYP3:

load DISTINCT *,

          if([Codice Gruppo] = '000253', if(left([polizza_short], 3)='920', 'Global', if(left([polizza_short], 3)='609', 'Global', 'General'))) as [3_cifre_polizza_short]

resident [HYP2];

drop table [HYP2];

We have the same table object but a total count of Polizza of 23 (as it should).

So, what's happening?

Well, I don't still see it clearly but it seems that at any point, the synthetic key is messing the data before executing the join instruction.

A Distinct load should do it, but I believe that for your purposes it would be really much easier to do something like:

[HYP]:

load

          *,

          if([Codice Gruppo] = '000253', if(left(mid(Polizza,findoneof(Polizza, '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\/')), 3)='920', 'Z-Global',

                    if(left(mid(Polizza,findoneof(Polizza, '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\/')), 3)='609', 'Z-Global'))) as [Z_Global] ;

load * inline [

Codice  CapogruppoCodice GruppoNumero ContrattoPolizzapolizza_shortpremiumZ_Global
60067200025300156471106.1006032621106.1006032621750
600672000253001564710000000006100603262161006032621-750
60134900025300175842000000000000920A1521920A1521-19Z-Global
60134900025300175842000000000000920A1521920A152119.151Z-Global
60134900025300175842000000000000920A1521920A15216.500Z-Global
60134900025300175842000000000000920A1521920A15211.295Z-Global
60102300025300159645000000000000AXH32132AXH32132904
60244900025300173802000000000000920A8454920A8454235Z-Global
60244900025300173802000000000000920A8454920A845442.573Z-Global
60244900025300173802000000000000920A8454920A8454356Z-Global
60244900025300173802000000000000920A8454920A8454-356Z-Global
60244900025300173802000000000000920A8454920A84541.244Z-Global
60244900025300173802000000000000920A8454920A8454-1.912Z-Global
60070000025300156819000000000000209B7810209B7810120
60070000025300156830000000000000209C0451209C0451113
60070000025300156843000000000000209C1689209C1689111
03468000025320248328920B1161920B11613.272Z-Global
00477900025320248818000000000000209S9980209S9980404
00477900025320248818000000000000209S9980209S9980-404
00477900025320248668000000000000209U3340209U334088
00477900025320248668000000000000209U3340209U3340-88
60268700025300150666000000000000920A4627920A46270Z-Global
00874200025300153519000000000000209C2279209C227991

];

I believe that it gives you exactly the same result, don't need to do three auxiliary tables and can get rid of the keep/join part (I always try to avoid doing joins if possible because of this kind of things )

Hope it helps

Not applicable
Author

Thanks man! I think the distinct did it!

To make it harder, try adding the following line in the first load:

00156471,    100,    00000000092006032621,    000253,    600672

This case should be marked as Z-Global, since al least one other policy for that same Codice Capogruppo starts either with 920. The only way I found to do this is via 3 tables. Otherwise your're right: everything can be computed in one load. But this is not the case.

Let me know what you think. Thanks anyway

Luis_Cortizo
Former Employee
Former Employee

It should be marked as Z_Global just for that new line, right?.

I understand that the rest of rows with the same Numero Contratto should remain uncategorized.

Doing everything in one load with the preceding load seems to do what we could expect

Numero Contratto premium Polizza Codice Gruppo Codice Capogruppo Z_Global
00156471 100 00000000092006032621 000253 600672 Z-Global
Not applicable
Author

Hi Luis,

no, not just that row. The logic is: if for all [polizza_short] belonging to one [Codice Capogruppo] there is al least one who's first 3 values are either 920 or 609, then the Z-Global tag needs to be applied to all of those policies.

On the other hand, if not even one [polizza_short] starts with either 920 or 609, then all policies belonging to that [Codice Capogruppo] should remain unmarked.

Suggestions?