sql >> Base de Datos >  >> RDS >> Mysql

MySQL Cómo asignar un valor nulo a columnas que no coinciden cuando se usa Agrupar por

Puede usar union adn tener .. tener recuento(*)> 1 insertar nulo de lo contrario insertar la ubicación

  insert into  SummaryTbl (Name,Location,Date,TicketRevenue,SnackRevenue,BeverageRevenue)
  select Name, NULL,Date,sum(TicketRevenue),sum(SnackRevenue), sum(BeverageRevenue)
  from MovieTheaterTbl
  group by Name
  HAVING COUNT(*) >1
  and count(location) >1
  UNION
  select Name, Location,Date,sum(TicketRevenue),sum(SnackRevenue), sum(BeverageRevenue)
  from MovieTheaterTbl
  group by Name
  HAVING COUNT(*) =1
  AND count(location) = 1