I am working in snowflake and got stuck very badly when I need unique columns in my final result set .
For e.g I have Table A with below information :
+---------+----------|
| user_id | product |
+---------+----------|
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
| 3 | 2 |
+---------+----------|
TABLE B with below information :
+---------+---------- |
| user_id | Week |
+---------+-----------|
| 1 | 2020-01-02|
| 1 | 2020-01-02|
| 2 | 2020-01-03|
| 2 | 2020-01-03|
| 3 | 2020-01-04|
+---------+-----------|
when I am writing the query
Select * from a join b on a.user_id =b.userid
I am getting the user_id two times because I am using the "*" but I need User_Id only one time with output as :
+---------+----------|---------- |
| user_id | product | Week |
+---------+----------|-----------|
| 1 | 1 | 2020-01-02|
| 1 | 2 | 2020-01-02|
| 2 | 1 | 2020-01-03|
| 2 | 2 | 2020-01-03|
| 3 | 2 | 2020-01-04|
+---------+----------|-----------|
But I am getting this:
+---------+----------|---------- |---------+
| user_id | product | Week | user_id |
+---------+----------|-----------|---------+
| 1 | 1 | 2020-01-02| 1 |
| 1 | 2 | 2020-01-02| 1 |
| 2 | 1 | 2020-01-03| 2 |
| 2 | 2 | 2020-01-03| 2 |
| 3 | 2 | 2020-01-04| 3 |
+---------+----------|-----------|---------+
The above is just and example ,in actual scenario i have 355 columns in Table A and 410 columns in Table B.So i can't do
Select a.column_name_1,a.column_name_2 ....a.column_name_355,b.column_name_1,a.column_name_2 ....a.column_name_355
from table a join table b on a.uder_id = b.userid .
I have tried this but it is not working :
set name = (select listagg(column_name,',') as name from (
select row_number() over(order by column_name) column_id,column_name from information_schema.columns
where table_schema='DEV'
and table_name ='table_a'
and column_name!='WEEK')
);
select $name from
"DEV_DB"."DEV"."table_a" a inner join
"DEV_DB"."DEV"."table_b" b on a.WEEK = b.WEEK
Can anyone please help me !!
*
with the columns you need. – Larnu