SQL self join to eradicate duplicate keys - sql

I have a table with below columns:
Column1
Column2
Column3
A
Hello
NULL
A
NULL
WORLD
I want the above table to transform like below:
Column1
Column2
Column3
A
Hello
WORLD
I'm using Snowflake DataWarehouse. Need help in the above transformation using SQL

select column1,
max(column2) as column2,
max(column3) as column3
from your_table
group by column1;

Related

Removing duplicates of column2 then group them based on column1 , then sum the values of column3 in sql

The table looks like
column1 column2 column3
400196 2021-07-06 33
400196 2021-07-06 33
400196 2021-08-16 33
I want to get the sum of column3 values based on grouping of column 1 but the duplicate values of date should not be added
The desired output is:
column1 column3
400196 66
The query I wrote is
select sum(column3)
from table_name
group by column1
But this gives me result 99
You can remove duplicate values in a subquery:
select t.column1, sum(t.column3)
from (select distinct t.column1, t.column2, t.column3
from t
) t
group by t.column1;
Note: This sort of problem can arise when you are joining tables together. Removing duplicates may not always be the right solution. Often it is better to do the calculation before joining, so you don't have duplicate values to deal with.
You could use a two step process here, first remove duplicates, then aggregate and sum:
SELECT column1, SUM(column3) AS column3
FROM (SELECT DISTINCT column1, column2, column3 FROM yourTable) t
GROUP BY column1;
Demo

SQL Script using sum(column1$)/count(distinct(column2person)

trying to use sum and count distint function & not getting results
Column1 column2 column3 column4 (3dividedby2)
personid count distinct sum$ sum$/count(distinct)
Above is the output i'm trying to get and what i see is this
Column1 column2 column3 column4 (3dividedby2)
1234 20 20,000 20,000
instead i would want to see this
Column1 column2 column3 column4 (3dividedby2)
1234 20 20,000 1,000
What am i doing wrong..
here is the query
select column1, count(distinct(column2)) as X, Sum(column3) as "COST"
, cost/ x as "Avg of column1 "
from table.table1
group by column1;
thanks!
You cannot re-use aliases in the select. Just repeat the expressions:
select column1, count(distinct column2) as X, Sum(column3) as cost,
sum(column3) / count(distinct column2) as avg_column1
from table.table1
group by column1;

Create new record if column contains value

Wanted to know if I could "artificially" insert new records when a record contains a value for a specific column. For example say I have this table in my database with the following two records:
Column1 Column2 Column3
-------------------------
DataA1 DataA2 null
DataB1 DataB2 DataB3
Now Column3 is the column I want to trigger an extra row if there is a value. Column3 is essentially Column2 but with another value (this is non-normalized and I can't change it so I need to resort to a query instead). So I want to create a query that returns 3 rows using the example above and it should come out like this:
DataA1 DataA2
DataB1 DataB2
DataB1 DataB3
How do I write my sql to return the results above?
Use union all:
SELECT Column1, Column2
FROM TableName
WHERE Column3 IS NULL
UNION ALL
SELECT Column1, Column3
FROM TableName
WHERE Column3 IS NOT NULL
Not totally sure what you want here but I think you are looking for something like this.
select Column1
, Column2
from SomeTable
where Column2 is not null
UNION ALL
select Column1
, Column3
from SomeTable
where Column3 is not null
You could use a UNION statement to merge a result set that uses the third column as the second column when the third column is not null:
SELECT column1, column2
FROM Sample
UNION
SELECT column1, column3
FROM Sample
WHERE column3 IS NOT NULL
http://sqlfiddle.com/#!9/42ca15/6

Changing column in set update

Can I use a case statement in Set Column? I have multiple columns that need to be updated but the statement is quite similar. The only difference is what I'm selecting.
UPDATE TABLE1 A
SET A.COLUMN2 = (SELECT....
I want to update column2 to Column1 without repeating the same block of code.
Note: I'm using LISTAGG
Is there any way I could distinct both of the columns without trying to separate it in one query the make a subquery
I'm using this query and I know that listagg don't have the capabilities to distinct unless you distinct it first before using listagg
SELECT LISTAGG(COLUMN1 , ', ') WITHIN GROUP (ORDER BY COLUMN1) AS COLUMN1 ,
LISTAGG(COLUMN2 , ', ') WITHIN GROUP (ORDER BY COLUMN2) AS COLUMN1
FROM (SELECT COLUMN1 , COLUMN2 FROM TABLE2 B
WHERE A.COLUMN3 = B.COLUMN3
GROUP BY COLUMN1 , COLUMN2);
COLUMN1 COLUMN2
EGG PIE
EGG BREAD
Expected output
COLUMN1 COLUMN2
EGG PIE; BREAD
Do you mean
UPDATE table1
SET (column1, column2 ...) = (SELECT col1, col2 ...)

select min value in same row in sql

I want to select min value of dates in same row of different columns.
e.g.
column1 column2 column3
2017-01-26 2017-01-28 2017-01-27
in above three columns i would like to select min date i.e. result of select should be 2017-01-26
Most databases support least() and greatest():
select least(column1, column2, column3) as min_column,
greatest(column1, column2, column3) as max_column
In any database, you can use ANSI standard case for the logic:
select (case when column1 >= column2 and column1 >= column3 then column1
when column2 >= column3 then column2
else column3
end) as max_column
(And then similar logic for the min.)
If using SQL Server, you can use this approach:
SELECT (SELECT MIN(columnX) FROM (VALUES(column1), (column2), (column3)) x(columnX))
FROM ...