Bring your best ideas to the AI Use Case Contest! Enter to win 40 hours of expert engineering support and bring your vision to life using the powerful combination of Alteryx + AI. Learn more now, or go straight to the submission form.
Start Free Trial

Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

IN-DB Iterative Value Matching Across Multiple Columns

tj
7 - Meteor

Will try to explain my needs, hopefully somebody has related knowledge

 

I have a database table (T1) with below columns/values

IDCOL1COL2COL3
1112233
2441155
3225566
4778844

 

I wanted to compare below table (T2) with above table (T1) in database. Process as follows

  • Check first value (11) in T2 against all 3 columns (COL1/2/3) in T1
  • If any match, pull the whole row in T1
  • Then add VALUE/MATCH columns from T2 to generate output rows
  • Iterate until all 3 values are compared
VALUEMATCH
11A
33B
44C

 

The output table (T3) should be something like below

IDCOL1COL2COL3VALUEMATCH
111223311A
244115511A
111223333B
244115544C
477884444C

 

As actual T1 table is quite big, so I'd like to utilize IN-DB tools as much as possible, before bring T3 table out as output results.

 

I'm thinking of using In-DB Macro but not sure how to accomplish it.

 

Appreciate any insights/help!

8 REPLIES 8
AkimasaKajitani
17 - Castor
17 - Castor

Hi @tj,

 

I tried to create the batch macro, but Macro Input In-DB tool can only be used in standard macros.

 

AkimasaKajitani_0-1643522793742.png

 

Please make the workflow without using macro. Let's give up this time.

gabrielvilella
14 - Magnetar

Hi @tj, try using a batch macro with the Dynamic Input tool, updating the SQL query dynamically. The only caveat is that you will have to know how to write the SQL code for joining tables.

tj
7 - Meteor

Thank you both for your advice. 

 

Let me try batch with dynamic input to see how far I can push it.

apathetichell
20 - Arcturus

can you use a pivot that's specific to your sql to isolate the rows - join those against your row id and then join the column with the matched values?

 

What type of SQL is your datasource?

tj
7 - Meteor

It's Snowflake so I assume standard ANSI SQL?

apathetichell
20 - Arcturus

Ill try to play around with it later today and see where I get. But this should be possible in Snowflake.

 

https://docs.snowflake.com/en/sql-reference/constructs/unpivot.html has information on what I'd use...

tj
7 - Meteor

Appreciate your time in working on it, let me know what you find!

apathetichell
20 - Arcturus

O.k. - Back to this - there is a fairly straight forward solution that I hadn't really thought of at the time.

 

1) Connect In-Db tool to pull 1 record from your data source - this will go to a datastream out. (select * from .... LIMIT 1)

2) Second branch from Connect In-DB to a dynamic In-DB tool. This will capture your query and connection.

3) You will decide what columns could contain your value - you can find all potential numeric columns via dynamic select looking for numeric columns (or string columns).

4) use a field info after the dynamic select to isolate your column names as name.

5) use a formula tool to change column name (now in the [name] field into something that would fit in your sql query (like [name] + = "'Best Column Ever'")

6) use a summarize tool to combine this column - making sure to use /n (new line) and "AND" in your seperator. Your prefix should be WHERE

7) union your SQL column code to your query/connection datastream. use manually layout columns to make sure your query and your concatenated columns are IN THE SAME COLUMN. use the bottom section of the union tool to choose which should go on top.

8) Use another summarize tool - take the max connection. concatenate your queries. again. use /n if you want to.

9) Use a formula tool - replace([concat_query],"LIMIT 1"," ") - you don't need to limit 1 anymore.

10) YOU ARE READY! DRAG A DYNAMIC CONNECT IN-DB ONTO YOUR CANVAS. Set up your connection and you query. let it run.

 

Troubleshoot as needed. Add browse tools after each summarize and formula to make sure that this looks like you'd expect it to.

NOTE: to get this in your ABC field. you would put this entire thing in a macro. you would then feed in the value you are matching with to step 5 to replace "Best Column Ever".

 

Labels
Top Solution Authors