Delete rows with same columns data - mysql

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).

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.

MySql Between clause not returning expected values when used in a JOIN with Order By

I have two tables that I want to join and query. I want the query to return all records where the field columns value is between 2 values x, y.
Table 1:
id int(11) YES NULL
game int(11) YES NULL
numbers varchar(255) YES NULL
bonus varchar(255) YES NULL
multiplier varchar(255) YES NULL
created int(11) YES NULL
data text YES NULL
Table 2:
bundle varchar(128) NO MUL
deleted tinyint(4) NO PRI 0
entity_id int(10) unsigned NO PRI NULL
revision_id int(10) unsigned NO MUL NULL
langcode varchar(32) NO PRI
delta int(10) unsigned NO PRI NULL
field_secondary_prize_value_value varchar(255) NO NULL
And the query :
SELECT
t1.id,
t2.field_secondary_prize_value_value
FROM
table1 t1
INNER JOIN table2 t2 ON
t1.id = t2.entity_id AND t2.field_secondary_prize_value_value BETWEEN 0 AND 10
ORDER BY
t2.field_secondary_prize_value_value
DESC;
The result sets should start with records where t2.field_secondary_prize_value_value is 10 but this what I am getting:
ID. field_secondary_prize_value_value
1490476 5
1490496 5
1490531 5
1490596 5
1490636 5
1490651 5
1490666 5
1490676 5
1490756 5
1490761 5
If replace the between clause to equality: i.e:
INNER JOIN table2 t2 ON
t1.id = t2.entity_id AND t2.field_secondary_prize_value_value = 10
1490546 10
1490561 10
1490581 10
1490616 10
1490896 10
1491041 10
1491156 10
1491221 10
1491316 10
1491341 10
I tried casting varchar to int but I still got the same results.
If I order by a column on the base table I get results where field_secondary_prize_value_value shows different values within the range.
Perhaps I need to use a subquery.
The ordering is actually right, because the column is a varchar and values will be ordered alphanumeric. You can change it to a numeric sortorder by:
changing the datatype of the column to a numeric one if that is an option/applies
use type conversion in your queries (see link in Akina's comment for explanation and pitfalls)
More explanations of sorting types can be found here.
Applying this to your query (quick 'n dirty):
SELECT
t1.id,
t2.field_secondary_prize_value_value
FROM
table1 t1
INNER JOIN table2 t2 ON
t1.id = t2.entity_id AND t2.field_secondary_prize_value_value BETWEEN 0 AND 10
ORDER BY
(t2.field_secondary_prize_value_value * 1)
DESC;
Personally I think it is better in this case to do the type conversion as close to the source as possible so it's easier to stay consistent, but that of course also depends on what you're trying to accomplish.
My alternative would be:
SELECT
t1.id,
t2.prize_value_num
FROM
table1 t1
INNER JOIN (SELECT
entity_id,
(field_secondary_prize_value_value * 1) AS prize_value_num
-- or any other type conversion you see fit
FROM table2
) t2 ON
t1.id = t2.entity_id AND t2.prize_value_num BETWEEN 0 AND 10
ORDER BY
t2.prize_value_num
DESC;
Be aware: in both examples the value '5B' will also meet the condition BETWEEN 0 AND 10.

Remove Null Columns and combine Rows

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

Join Distinct Id on non-distinct id (MySql)

I'm trying to join distinct ID's from a subquery in a FROM onto a table which has the same ID's, but non-distinct as they are repeated to create a whole entity. How can one do this? All of my tries are continuously amounting to single ID's in the non-distinct-id-table.
For example:
Table 1
ID val_string val_int val_datetime
1 null 3435 null
1 bla null null
1 null null 2013-08-27
2 null 428 null
2 blob null null
2 null null 2013-08-30
etc. etc. etc.
Virtual "v_table" from SubQuery
ID
1
2
Now, if I create the query along the lines of:
SELECT t.ID, t.val_string, t.val_int, t.val_datetime
FROM table1 AS t
JOIN (subquery) AS v_table
ON t.ID = v_table.ID
I get the result:
Result Table:
ID val_string val_int val_datetime
1 null 3436 null
2 null 428 null
What I'd like is to see the whole of Table 1 based on this example. (Actual query has some more parameters, but this is the issue I'm stuck on).
How would I go about making sure that I get everything from Table 1 where the ID's match the ID's from a virtual table?
SELECT t.ID, t.val_string, t.val_int, t.val_datetime
FROM table1 AS t
LEFT JOIN (subquery) AS v_table
ON t.ID = v_table.ID
Sample fiddle

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