Remove Null Columns and combine Rows - sql-server-2008

I have data in table like
ID desc Year pid
0006845503 tes1 null null
0006845503 null 2017 null
0006845503 null null 90
0006845503 tes2 null null
0006845503 null 2018 null
0006845503 null null 100
I want the result like
ID desc year pid
0006845503 tes1 2017 90
0006845503 tes2 2018 100
Used Max function it will return only one row. i want this to be working on a dynamic way. Please help

If you don't want any records with null value then
ex: select * from table where column_name is not null
If you don't want NULL value then use ISNULL function
ex: select isnull(col_name,'') from table

Related

How to group by one column with multi column query in hive

Ihave the follwing data:
ID
Date1
Date2
Date3
1
2022/01/01
null
null
1
null
2021/04/01
null
2
2022/03/01
null
null
2
null
2021/06/01
null
3
2022/01/01
null
null
4
null
2021/04/01
null
and I'm trying to get the following result:
ID
Date1
Date2
Date3
1
2022/01/01
2021/04/01
null
2
2022/03/01
2021/06/01
null
3
2022/01/01
null
null
4
null
2021/04/01
null
trying group by ID, isn't working as I get the error expression not in group by key.
Any help is well received.
Aggregate by the ID column and then take the MAX() of the other three columns:
SELECT ID, MAX(Date1) AS Date1, MAX(Date2) AS Date2, MAX(Date3) AS Date3
FROM yourTable
GROUP BY ID;
Note that the MAX() aggregate function will by default ignore NULL values. Therefore, the non NULL values from each pair of similar ID records will be retained in the above query.

How can I select a grouped row where it only contains null values in another column?

It's a bit confusing, so I'll try to exemplify in the table below:
example-table
id
data1
data2
data3
1
NULL
NULL
NULL
1
NULL
NULL
NULL
2
1
NULL
1
2
1
NULL
NULL
2
1
NULL
1
3
1
NULL
NULL
3
1
NULL
1
4
NULL
NULL
NULL
So, grouping by the same ID, I only want to display those IDs that all their data, from all columns, are null. In the table example above, only IDs 1 and 4.
The code I'm trying to use - just for reference:
select id
from example-table
group by id, data1, data2, data3
having data1 is null and data2 is null and data3 is null;
Any suggestions?
You can group by id only and set the conditions in the HAVING clause:
SELECT id
FROM tablename
GROUP BY id
HAVING COALESCE(MAX(data1), MAX(data2), MAX(data3)) IS NULL;

Delete rows with same columns data

I am having a temp table with the following data:
tbl_t
id name_id date t1 t2 s1 s2
1 25 10/05/20 same same NULL NULL
2 23 11/05/21 same same home NULL
3 25 12/05/20 same NULL NULL NULL
4 25 13/06/20 NULL NULL NULL NULL
Desire output:
tbl_t
id name_id date t1 t2 s1 s2
2 23 11/05/21 same same home NULL
3 25 12/05/20 same NULL NULL NULL
I want to delete all rows where t1=t2 and s1=s1
I tried the following sql but i noticed that it is not working.
DELETE FROM tbl_t WHERE t1=t2 AND s1=s2
The problem are the NULL values. Use the NULL-safe comparison operator:
DELETE FROM tbl_t
WHERE t1 <=> t2 AND s1 <=> s2;
Almost any comparison with NULL results in NULL -- including NULL = NULL. And NULL values are treated as false in a WHERE clause (or equivalently WHERE clauses only keep rows where the condition evaluates unequivocally to true).

2 mysql queries at once

I have a table like this one..
[id][Col1][Col2][id_duplicated]
10 abc1 defg NULL
12 text text NULL
50 abc2 text NULL
90 NULL NULL 10
500 NULL NULL 10
620 NULL NULL 50
700 text text NULL
Id_duplicated is a value that marks that is a copy from the row 'id' on the same table.. if i select id 620 will display all values from id 50
The problem:
select id,col1 from table where col1 is like '%abc%'
Only will show the row id=10 and id=50 but also i need to display if there's any copies of those id's
i imagine i need a subquery that first find if there's any '%abc%' and then a second one to check if there's any copy on id_duplicated equal to 10 and 50 and etc etc....
So in conclusion i need one query that display this result
[id][Col1][Col2][id_duplicated]
10 abc1 defg NULL
50 abc2 text NULL
90 NULL NULL 10
500 NULL NULL 10
620 NULL NULL 50
Sorry my english
If you want additional rows you can use union and a select looking for the id of the result into the id_duplicated:
http://sqlfiddle.com/#!9/ad817/8
select id,col1, col2, id_duplicated
from table
where col1 like '%abc%'
UNION
select id, col1, col2, id_duplicated
FROM table
WHERE id_duplicated IN (select id from table where col1 like '%abc%')

Sql server Query to get the Table Altered Result

I have a table as below
ClientID AccountNumber BalanceOnDay0 BalanceOnDay1 BalanceOnDay2 BalanceOnDay3 BalanceOnDay4 BalanceOnDay5 BalanceOnDay6 BalanceOnDay7
ABC1 123 10 NULL NULL NULL NULL NULL NULL NULL
ABC1 123 NULL NULL NULL NULL NULL NULL NULL 3
I would like to see the result as beblow.
ClientID AccountNumber BalanceOnDay0 BalanceOnDay1 BalanceOnDay2 BalanceOnDay3 BalanceOnDay4 BalanceOnDay5 BalanceOnDay6 BalanceOnDay7
ABC1 123 10 NULL NULL NULL NULL NULL NULL 3
Please suggest!
You can use SUM() if you want to combine the balance values, if you have multiple records:
select clientid,
accountnumber,
sum(BalanceOnDay0) BalanceOnDay0,
sum(BalanceOnDay1) BalanceOnDay1,
sum(BalanceOnDay2) BalanceOnDay2,
sum(BalanceOnDay3) BalanceOnDay3,
sum(BalanceOnDay4) BalanceOnDay4,
sum(BalanceOnDay5) BalanceOnDay5,
sum(BalanceOnDay6) BalanceOnDay6,
sum(BalanceOnDay7) BalanceOnDay7
from table1
group by clientid, accountnumber
See SQL Fiddle with Demo