What I'm trying is to generate a SQL statement that finds me in the rows that have repeated data, and at the same time in different columns.
My table is something like this:
ID DATE ONE TWO TRES status
-- ---------- --- --- --- -----
1 2018-02-18 21 22 23 B
2 2018-02-18 01 21 44 B
3 2018-02-18 55 66 77 B
What I have to do first is find the records that have the same date ... I have no problem.
The problem is when I try to compare columns ONE TWO and TRES to see if it has a value in common.
It is logical that I will have to go comparing from ONE to TWO, ONE to THREE and TWO to THREE.
In the example, row 1 and 2 have the same date and Column ONE has the same value as DOS and therefore it would have to update its status to E (error) ..
'UPDATE `table` SET `status` = "E" WHERE `DATE` = `DATE`'
Thank...
This query below will give you your desired result:
UPDATE TableName a
INNER JOIN
(
SELECT a.ID, b.ID AS ID2
FROM TableName a
INNER JOIN TableName b
ON a.Date = b.Date
AND
(
a.One = b.Two
OR a.One = b.Tres
OR a.Two = b.Tres
)
) b ON a.ID = b.ID OR a.ID = b.ID2
SET Status = 'E'
basically, what it does is it joins that table to a subquery which conditions you mentioned above to get the invalid rows and the resulting rows will be join again to itself and update its status to E.
Here's a Demo.
Related
I am having two tables say table_a and table_b with following structure.
table_a : ID(primary key), value_one
table_b: ID, value_two
Note that id in table_b is not primary and contains multiple records to same id.
now i want a third table which displays a record for every id in table_a with columns being
id column_count
the column _count will display number of records (count) in table_b with value_two = 'c'. and i want to iterate this to all records of table_a.
For example lets say our table_a is like this:
id value_one
1 20
2 40
3 50
table_b
id value_two
1 10
1 20
1 10
2 40
2 10
3 40
3 10
I want records with value_two = 10 so my new table would look like
id count
1 2
2 1
3 1
Since id 1 has two records with value_two = 10 and id 2 and id 3 have one record each with value_two = 10
One way of doing this uses a correlated subquery:
select a.id,
(select count(*) from table_b b where b.id = a.id and b.value_two = 10) as cnt_10
from table_a a;
Another method uses a left join:
select a.id, count(b.id)
from table_a a left join
table_b b
on b.id = a.id and b.value_two = 10
group by a.id;
In your example data, this works:
select b.id, count(*)
from table_b b
where b.value_two = 10
group by b.id;
This is equivalent under the following circumstances:
All ids in a are in b.
All ids have at least one value of 10.
If these two conditions are true, then use this simpler query.
You can do conditional aggregation :
select id, sum(value_two = 10) as count
from table_b tb
group by id;
If you want matching ids then add INNER JOIN. This will show 0 count whereas value_two = 10 not found. You can add where clause to find only value_two = 10 count.
I have three tables A, A_B and B.
Since mySQL doesn't support many to many relationships, tables A and B are related to each other by intermediate table A_B.
Table A: Table A_B: Table B:
aId aCount aId bId bId bCo
1 10 1 1 1 4
2 15 1 2 2 5
3 20 2 1 3 6
3 2 4 7
3 4
Now I want to add bCount of each bId to each related aCount.
Desired result:
Table A: Remark:
aId aCount (Current aCount + all related bCounts)
1 19 (10 + 4 + 5)
2 19 (15 + 4)
3 32 (20 + 5 + 7)
My mySQL knowledge is too basic.
I can create a SELECT query which gives me a result table like i want table A to look like after update, but I can't create the update statement. Everything I tried led to syntax errors or undesired results.
I also had one statement which might had work, but it took way to long to execute. Now I'm just confused about what to do.
Desired Result looks like:
SELECT `aId`, aCount + SUM(`bCount`) as `aCount` from `A`
INNER JOIN `A_B` ON `A_B`.`aId` = `A`.`aId`
INNER JOIN `B` ON `B`.`bId` = `A_B`.`bId`
GROUP BY `aId`;
Is an update like I want even possible with a single query or do I need multiple (performance wise)?
Can anyone help me out with an approach or with creating statements?
Try below: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=0b6a287d41eed001710e94612c4228ba
select x.id,x.val,sum(tableb.val)+x.val as vals from
(select * from tablea inner join tableab on tablea.id=tableab.id1) x inner join
tableb on x.id2=tableb.id
group by x.id,x.val,id1
Output:
id val vals
1 10 19
2 15 19
3 20 32
To update tablea:
UPDATE tablea INNER JOIN
(SELECT x.id, x.val, SUM(tableb.val) + x.val as vals FROM
(SELECT * FROM tablea INNER JOIN tableab ON tablea.id = tableab.id1) x INNER JOIN
tableb ON x.id2 = tableb.id
GROUP BY x.id, x.val, id1)Z ON tablea.id=Z.id
SET tablea.val = Z.vals;
For example I have a table A and B with the following data:
A:
user_name date1 count1 count2
X 15 1 1
X 30 1 3
Y 04 1 3
B:
user_name date1 count3 count4 status
X 15 11 1 Y
X 30 13 3 N
Y 04 16 3 NA
How to join these 2 tables for each feedname with max date.
I need the output like these:
username date1 count1 count4 status
X 30 1 3 N
like these way.
Can anyone plz help in these situation.
Since according to your comment every combination (user_name, date1) exists in both tables, you can use e.g.
select a.*, b.count3, b.count4, b.status
from tableA as a
join tableB as b
on a.user_name = b.user_name and
a.date1 = b.date1
where not exists
(select 1 from tableA as a1
where a1.user_name = a.user_name
and a1.date1 > a.date1);
You want to have an index on (user_name, date1) to speed it up.
As a side note: If every entry in tableA has exactly 1 entry in tableB and vice-versa (it's not clear from your description if that is the case, but it looks like it), and thus (user_name, date1) would be a primary key in both tables, you absolutely should add the columns count3, count4 and status to tableA and get rid of tableB. You can still use the above code (without join) to find only the max entry per user.
newbie here to SQL. So I have two tables, let's take for example the two tables below.
Table A
set_num s_id s_val
100 3 AA
100 5 BB
200 3 AA
200 9 CC
Table B
s_id s_val phrase seq
1 DD 'hi' 'first'
3 AA 'hello' 'first'
6 EE 'goodnight' 'first'
5 BB 'world' 'second'
9 CC 'there' 'second'
4 FF 'bye' 'first'
I want to join Table A with Table B on two columns, like a composite key (s_id, s_val), and I want to return
set_num from Table A and the concatenation of phrases in Table B (which we will call entire_phrase, concat(...) AS entire_phrase).
The concatenation should also follow an order in which the phrases are to be concatenated. This will be determined by seq column in Table B for each phrase. "First" will indicate this phrase needs to come first and "Second", well comes next. I will like to do this with a SELECT query but not sure if this is possible without it getting to complex. Can I do this in SELECT or does this call for another approach?
Expected Output:
set_num entire_phrase
100 'hello world'
200 'hello there'
And not
set_num entire_phrase
100 'world hello'
200 'there hello'
Any help/approach will be greatly appreciated!
You can do it like this:
select temp1.set_num, concat(phrase1,' ',phrase2) as entire_phrase
from (
(
select set_num, b.phrase as phrase1
from TableA as A
join TableB as B
on a.s_id = b.s_id
and a.s_val = b.s_val
and b.seq = 'first'
) as temp1
join
(
select set_num, b.phrase as phrase2
from TableA as A
join TableB as B
on a.s_id = b.s_id
and a.s_val = b.s_val
and b.seq = 'second'
) as temp2
on temp1.set_num = temp2.set_num
)
Running here: http://sqlfiddle.com/#!9/d63ac3/1
TableA (id int, match char(15), multiple char(10))
int match multiple
1 100
2 101
3 102
4 103
TableB (match char(15), match2 char(10))
match match2
100 ABC
100 NBG
101 NYF
102 NHW
102 UYQ
103 WOT
Now, I want to populate TableA.multiple = "YES" if in TableB for corresponding match, there exists more than one match2.
Expected result.
int match multiple
1 100 YES
2 101 NULL
3 102 YES
4 103 NULL
Thanks in advance !
My FAILED try:
Update A
SET multiple = 'YES'
From tableA A
Inner join tableB B ON A.match = B.match
WHERE (Select count(distinct(B.match2)) from TableB) > 2
Start with an extra-verbose version, just for its clarity:
UPDATE TableA
SET multiple = 'YES'
WHERE match in (
-- isolate the multiples
SELECT match from (
-- count the matches
SELECT count(*) as c, match from TableB
GROUP BY match ) x
WHERE c > 1
)
With the HAVING clause, you can change this...
SELECT match from (
SELECT count(*) as c, match from TableB
GROUP BY match ) x
WHERE c > 1
...to this:
SELECT match from TableB
GROUP BY match
HAVING count(*) > 1
So now we have:
UPDATE TableA
SET multiple = 'YES'
WHERE match in (
SELECT match from TableB
GROUP BY match
HAVING count(*) > 1
)
I'm sure it can be made more compact, but I personally get confused by UPDATE statements containing non-obvious JOIN clauses, especially in the middle of the night when I get the call that "the database isn't working!"
Don't Make Me Think applies to coding, too.
UPDATE tableA
SET multiple = 'YES'
FROM TableA AS a
JOIN (SELECT match FROM tableB GROUP BY match HAVING COUNT(*) > 1) AS b ON a.match = b.match
UPDATE TableA a
SET multiple='YES'
FROM Tablea a,(SELECT match FROM Tableb GROUP BY match HAVING COUNT(*)>1)b
WHERE a.match=b.match