sql >> Database teknologi >  >> RDS >> Mysql

Tilføj ny kolonne til resultat- og samletabellen

Jeg tror, ​​du har brug for union all :

select id, campaign, event_type, 'delivered' as sub_event_type, date_delivered
from logistics
where event_type = 'stored' and date_delivered is not null
union all
select id, campaign, event_type, 'recorded' as sub_event_type, date_recorded
from logistics
where event_type = 'stored' and date_recorded is not null
union all
select id, campaign, event_type, 'completed' as sub_event_type, date_completed
from logistics
where event_type = 'stored' and date_completed is not null
union all
select id, campaign, event_type, null as sub_event_type, date_offered
from logistics
where event_type = 'offered' 
union all
select id, campaign, event_type, null as sub_event_type, date_ordered
from logistics
where event_type = 'ordered' ;



  1. Opretter forbindelse til Postgresql i en docker-container udefra

  2. Tilføjelse af 'seriel' til eksisterende kolonne i Postgres

  3. 3 måder at finde rækker, der indeholder små bogstaver i SQLite

  4. Kroniske uaktuelle resultater ved hjælp af MySQLdb i Python