Merge two row results into one row MYSQL - mysql

I have a table having following structure:
| pid | email | email_type |
| 1 | x | 1 |
| 1 | y | 2 |
| 1 | z | 3 |
| 2 | ab | 1 |
| 3 | cd | 2 |
Now I want my result for the pid parameter in format for email_type[1 & 2] only:
Case pid=1
| pid | email_p | email_w |
| 1 | x | y |
Case pid=2
| 2 | ab | NULL |
Case pid=3
| 3 | NULL | cd |
Here email_p represents email_type=1 & email_w represents email_type=2
I am using following query, and its working fine except for a case pid=2. Case I & Case III are successfully fetched. Please provide some solution with good explanation(if possible).
SELECT `e`.`pid`, `e`.`email` AS `email_p`, `e1`.`email` AS `email_w` FROM `table1` AS `e` LEFT JOIN `table1` AS `e1` ON e.pid=e1.pid AND e1.email_type=2 WHERE (e.pid IN (1) AND e.email_type=1)
It fails when e.pid = 2 and it returns empty result set & please provide solution e.pid IN (1,2,3) holds good for required format.#MYSQL

Try This
SELECT pid,
MAX(CASE WHEN email_type=1 THEN email END ) as email_p ,
MAX(CASE WHEN email_type=2 THEN email END ) as email_w
FROM tableName
WHERE email_type IN (1,2)
GROUP BY pid
SQL Fiddle DEMO

Related

Nested JOIN to create custom dynamic columns

I have a table veicoli (vehicles) like this:
-------------------------------
| ID | Modello | Targa |
-------------------------------
| 1 | IVECO | XA123WE |
-------------------------------
| 2 | IVECO | CF556XD |
-------------------------------
| 3 | FIAT | AS332ZZ |
-------------------------------
| 4 | GOLF | GF567YU |
-------------------------------
For each vehicle I have none, one or multiple revisioni_veicolo (revisions) (the one with bigger DateExpiring is the one I need to check if revision is still valid or not based on today date)
-------------------------------------------------------------------
| ID | veicoli_ID | DateExpiring | Pass_Success |
-------------------------------------------------------------------
| 1 | 1 | 2019-07-01 | 1
------------------------------------------------------------------
| 2 | 1 | 2020-10-01 | 0
-------------------------------------------------------------------
| 3 | 2 | 2019-11-25 | 1
-------------------------------------------------------------------
| 4 | 2 | 2018-10-20 | 1
-------------------------------------------------------------------
| 5 | 4 | 2017-10-20 | 1
-------------------------------------------------------------------
Based on my example above (today is 2019-10-29):
Vehicle: ID = 1 has a revision still active (2020-10-01) but not passed (Pass_success = 0)
Vehicle: ID = 2 has a revision still active (2019-11-25) and passed (Pass_success = 1)
Vehicle: ID = 3 has no revision yet
Vehicle: ID = 4 has revision, but no active revision (last expired on 2017-10-20) but the last one passed the check (Pass_success = 1)
What I need is to have 3 new custom columns created dynamically on my query result:
-------------------------------------------------------------------------------------------
| ID | Modello | Targa | RevisionPresent | RevisionStillActive | LastRevisionPassed |
-------------------------------------------------------------------------------------------
| 1 | IVECO | XA123WE | true | true | false
-------------------------------------------------------------------------------------------
| 2 | IVECO | CF556XD | true | true | true
-------------------------------------------------------------------------------------------
| 3 | FIAT | AS332ZZ | false | false | false
-------------------------------------------------------------------------------------------
| 4 | GOLF | GF567YU | true | false | true
-------------------------------------------------------------------------------------------
I tried to start with my old post: MYSQL INNER JOIN to get 3 types of result
But I'm very confused using nested JOIN
I tried starting a fiddle but i'm stuck on syntax error: http://sqlfiddle.com/#!9/3c70bf/2
You need a LEFT JOIN of the tables and conditional aggregation:
select v.ID, v.Modello, v.Targa,
max(r.DataScadenzaRevisione is not null) RevisionPresent,
coalesce(max(r.DataScadenzaRevisione >= current_date()), 0) RevisionStillActive,
max(case when r.DataScadenzaRevisione = g.maxdate then r.EsitoPositivo else 0 end) LastRevisionPassed
from veicoli v
left join revisioni_veicolo r on r.veicoli_ID = v.id
left join (
select veicoli_id, max(DataScadenzaRevisione) maxdate
from revisioni_veicolo
group by veicoli_id
) g on g.veicoli_ID = v.id
group by v.ID, v.Modello, v.Targa
See the demo.
Results:
| ID | Modello | Targa | RevisionPresent | RevisionStillActive | LastRevisionPassed |
| --- | ------- | ------- | --------------- | ------------------- | ------------------ |
| 1 | IVECO | XA123WE | 1 | 1 | 0 |
| 2 | IVECO | CF556XD | 1 | 1 | 1 |
| 3 | FIAT | AS332ZZ | 0 | 0 | 0 |
| 4 | GOLF | GF567YU | 1 | 0 | 1 |
...
LEFT JOIN (SELECT a.veicoli_ID, a.EsitoPositivo AS StatoUltimaRevisione,
a.DataScadenzaRevisione FROM revisioni_veicolo) a
...
There's two things wrong with this.
The alias a is defined for this subquery, so you can't reference it inside the subquery. But you don't need to qualify the columns in this subquery anyway - you didn't do this in other subqueries, so I'm not sure why you did it in this case.
You don't have any join condition for this join. MySQL is a little bit inconsistent about when join conditions are required. But in this case, you need one.
After I tested the query with these two corrections, it works.
Basically you just need to look at the last revision of each vehicule to produce that resultset.
You can do the filtering with a correlated subquery:
select
v.ID,
v.Modello,
v.Targa,
(DataScadenzaRevisione >= now()) RevisionPresent,
(DataScadenzaRevisione >= now() and EsitoPositivo = 1) RevisionStillActive,
(EsitoPositivo = 1) LastRevisionPassed
from
veicoli v
left join revisioni_veicolo r
on r.veicoli_ID = v.ID
and r.DataScadenzaRevisione = (
select max(DataScadenzaRevisione)
from revisioni_veicolo r1
where r1.veicoli_ID = v.ID
)
You can check the results with your sample data in this db fiddle.
Or you can use a window function (this requires MySQL 8.0):
select
v.ID,
v.Modello,
v.Targa,
(DataScadenzaRevisione >= now()) RevisionPresent,
(DataScadenzaRevisione >= now() and EsitoPositivo = 1) RevisionStillActive,
(EsitoPositivo = 1) LastRevisionPassed
from (
select
v.*,
r.*,
row_number() over(partition by ID order by r.DataScadenzaRevisione desc) rn
from veicoli v
left join revisioni_veicolo r on r.veicoli_ID = v.ID
) where coaelesce(rn, 1) = 1

MySql: Join same table twice where a record doesn't exist

I have a table data:
+-------+-----------+------+-------+-------------+
| id | client_id | type | name | status |
+-------+-----------+------+-------+-------------+
| 523 | 2 | one | a | verified |
| 526 | 1 | one | a | verified |
| 527 | 1 | one | b | verified |
| 527 | 1 | two | b | verified |
+-------+-----------+------+---------------+-----+
I need to get the client_id's for those that where type='one' and name='a' and status='verified'' AND DON'T have a record where type=one and name=b
In my table above, the result would include client_id = 2.
How do I write a query like this?
You could use a correlated suqbuery with a NOT EXISTS condition to filter out the unwanted records (ie those for which another record exists with the same client_id, with type = 'one' and name = 'b')
SELECT *
FROM data d
WHERE
type = 'one'
AND name = 'a'
AND status = 'verified'
AND NOT EXISTS (
SELECT 1
FROM data d1
WHERE d1.client_id = d.client_id AND d1.type = 'one' and d1.name = 'b'
)

mysql join only if a field has a particular value

I am trying to fetch a table on certain conditions with join. My table is:
tab_registrations
--------------------------------------------
reg_id |familyid| familyname | parent_id |
| | | |
-------|--------|-------------|-----------|
1 | 2 | null | null |
-------|--------|-------------|-----------|
2 | others | abc | 3 |
-------|--------|-------------|-----------|
3 | 3 | null | null |
-------|--------|-------------|-----------|
4 | others | def | 2 |
-------|--------|-------------|-----------|
tab_family
-------------------------------------
family_id | family_name | parent_id |
| | |
-------------------------------------
1 | tyu | 0 |
-------------------------------------
2 | xyz | 1 |
-------------------------------------
3 | mno | 2 |
-------------------------------------
I want to join these tables on:
if tab_registrations.family not equal to null, then select corresponding parent_id from tab_family
SELECT tab_registration.*,tab_family.family_id,tab_family.parent_id
FROM `tab_registration`
join tab_family on tab_registration.family_id = tab_family.family_id
WHERE reg_id = 1
if tab_registrations.family is equal to 'others', then select tab_registrations.familyname and tab_registrations.parent_id
When I try the above query if tab_registrations.family = 'others', no rows fetched
How can I achieve this? Can anyone help me?
Change to LEFT JOIN with the condition that tab_registration.familyid is not equal to others. Also, you can use conditional CASE..WHEN statements to get the familyname and parent_id values.
SELECT tr.*,
CASE WHEN tr.familyid = 'others' THEN tr.familyname
ELSE tf.family_name
END AS familyname,
CASE WHEN tr.familyid = 'others' THEN tr.parent_id
ELSE tf.parent_id
END AS parent_id
FROM tab_registration tr
LEFT JOIN tab_family tf
ON tr.family_id = tf.family_id AND
tr.familyid <> 'others'
WHERE tr.reg_id = 1
For multi-table queries, it if preferable to use Aliasing for code clarity and readability.
may be useful this query
SELECT tr.*,tf.family_id,tf.parent_id,
IF(tr.familyid='others',tr.familyname,tf.family_name) as fname
IF(tr.familyid='others',tr.parent_id,tf.parent_id) as parentId
FROM `tab_registration` tr
left join tab_family tf on tr.family_id = tf.family_id

MySQL : Update Table upon certain conditions

I have 2 tables , master and current table( refreshed very hr).
Both the table shave same structure:
Chk | description |state | date
I need to update / append ( add the new row ) into the master table if :
1) rows that have new IDs or
2) if a particular variable ( 'state' in this case) has changed. I tried to do it using below without success :
INSERT into AGILE_TICKETS_DLY
SELECT * FROM CURR_AGILE_TICKETS curr
WHERE EXISTS (SELECT * FROM AGILE_TICKETS_DLY mstr
WHERE (curr.chk != mstr.chk) OR ( curr.chk = mstr.chk and
mstr.state != curr.state))
Any pointers on how to achieve this ?
You can try this pair of queries:
-- insert new rows
insert into agile_tickets_dly
select * from curr_agile_tickets
where chk not in (select chk from agile_tickets_dly);
-- update updated rows
update agile_tickets_dly x
join
(
select b.chk chk,b.description description,b.state state,b.date date
from agile_tickets_dly a, curr_agile_tickets b
where
a.chk=b.chk and
(a.description != b.description or a.state != b.state or a.date != b.date)
) y
on x.chk=y.chk
set x.description = y.description, x.state= y.state, x.date = y.date;
Illustration:
select * from agile_tickets_dly;
+------+-------------+---------+------------+
| chk | description | state | date |
+------+-------------+---------+------------+
| 0 | desc-0 | state-1 | 01-01-2017 |
| 1 | desc-1 | state-1 | 01-01-2018 |
| 2 | desc-2 | state-2 | 01-02-2018 |
| 3 | desc-3 | state-3 | 01-03-2018 |
+------+-------------+---------+------------+
-- one new row with chk=4, three updated rows with chk=1,2,3
select * from curr_agile_tickets;
+------+----------------+-----------------+----------------+
| chk | description | state | date |
+------+----------------+-----------------+----------------+
| 0 | desc-0 | state-1 | 01-01-2017 |
| 1 | desc-1 | state-1 | date-1-updated |
| 2 | desc-2-updated | state-2 | 01-02-2018 |
| 3 | desc-3 | state-3-updated | 01-03-2018 |
| 4 | desc-4 | state-4 | 01-04-2018 |
+------+----------------+-----------------+----------------+
-- after executing the two queries
select * from agile_tickets_dly;
+------+----------------+-----------------+----------------+
| chk | description | state | date |
+------+----------------+-----------------+----------------+
| 0 | desc-0 | state-1 | 01-01-2017 |
| 1 | desc-1 | state-1 | date-1-updated |
| 2 | desc-2-updated | state-2 | 01-02-2018 |
| 3 | desc-3 | state-3-updated | 01-03-2018 |
| 4 | desc-4 | state-4 | 01-04-2018 |
+------+----------------+-----------------+----------------+
I tried to do this in 2 separate steps:
1) First I append all new rows with IDs : this worked
INSERT into AGILE_TICKETS_DLY
SELECT * FROM CURR_AGILE_TICKETS curr
WHERE not EXISTS (SELECT * FROM AGILE_TICKETS_DLY mstr
WHERE (curr.chk = mstr.chk));
But then, I tried to do below got an error
2) Then replace the 'State' variable with new value:
INSERT into AGILE_TICKETS_DLY_1 (state)
SELECT state
from CURR_AGILE_TICKETS_1 curr
where exists ( select * from AGILE_TICKETS_DLY_1 mstr where curr.chk =
mstr.chk);
But this gives me an error :
SQL Error (1364) : Field 'chk' doesn't have a default value.
What does that mean ?

MySQL: joining tables with multi results rows to one row

I have 4 tables:
secu_content
| id | created | modified |
| 910 | 26/12/1982 | 28/12/1984 |
| 911 | 24/12/1982 | 25/12/1984 |
secu_data
| element_id | field_id | data |
| 1 | 1 | 25/12/1984 |
| 2 | 1 | 26/12/1984 |
| 3 | 1 | 27/12/1984 |
| 4 | 1 | 25/12/1984 |
| 4 | 2 | google.com |
secu_elements
| id | item_id |
| 1 | 891 |
| 2 | 711 |
| 3 | 204 |
| 4 | 911 |
secu_fields
| id | type |
| 1 | date |
| 2 | input |
Table secu_content, contains many articles, where the id is the article id.
The other 3 tables gives additional information and I want to join them.
I want to get results that includes all secu_content rows and all the columns + calc_date + calc_link
calc_date <- the data column from secu_data where field_id=1 (see secu_fields)
calc_link <- the data column from secu_data where field_id=2 (see secu_fields)
The problem is that I get 2 rows where secu_content id=911 (one row with the correct calc_date and second row with the correct calc_link), and I need one row with both.
This is my SQL:
SELECT a.id
, a.created
, a.modified
, fe.item_id AS calc_date_item_id
, fd.data AS calc_date
, CASE WHEN fd.data IS NOT NULL AND ff.type = "date" THEN fd.data
WHEN a.modified = '0000-00-00 00:00:00' THEN a.created ELSE a.modified
END as calc_date
, CASE WHEN fd.data IS NOT NULL AND ff.type = "input" THEN fd.data
END as calc_link
FROM secu_content AS a
LEFT
JOIN secu_fieldsandfilters_elements AS fe
ON fe.item_id = a.id
AND fe.content_type_id=1
LEFT
JOIN secu_fieldsandfilters_data AS fd
ON fd.element_id = fe.id
LEFT
JOIN secu_fieldsandfilters_fields as ff
ON ff.id = fd.field_id
ORDER BY a.id DESC;
Thanks in advance
Israel
Fast and dirty solution is to use second join to secu_data like that (simplified, add logic you need)
SELECT id, d1.data as `calc_date`, d2.data as `calc_link`
FROM secu_content
LEFT JOIN secu_data d1 ON secu_content.id = d1.element_id AND field_id = 1
LEFT JOIN secu_data d2 ON secu_content.id = d2.element_id AND field_id = 2