sql >> Database >  >> RDS >> Mysql

Gebruik gecorreleerde subquery over meerdere kolommen

U kunt een subquery gebruiken en de gegevens per campagne groeperen:

SELECT campaign,MAX(event_type) AS event_type,
CASE WHEN MAX(sub_event_date)>MAX(main_event_date) THEN MAX(sub_event_date) ELSE MAX(main_event_date) END  AS event_date,
CASE WHEN MAX(sub_event_date)>MAX(main_event_date) THEN MAX(sub_event_quantity) ELSE MAX(main_event_quantity) END AS event_quantity
FROM (
SELECT 
campaign,
event_type,
coalesce(date_offered, date_ordered) as main_event_date,
coalesce(date_delivered, date_recorded, date_completed) as sub_event_date,
coalesce(quantity_offered, quantity_ordered) as main_event_quantity,
coalesce(quantity_delivered, quantity_recorded, quantity_completed) as sub_event_quantity
FROM logistics lg) l
GROUP BY campaign


  1. Stripslashes gebruiken na mysql_real_escape_string

  2. Hoe importeer ik bestaande *.sql-bestanden in PostgreSQL 8.4?

  3. python MySQLDb-insert met voorbereide Statements

  4. Converteer een MySQL-database van latijn naar UTF-8