Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

MySQL Como atribuir um valor nulo a colunas não correspondentes ao usar Group By


Você pode usar union adn tendo .. tendo count(*)> 1 insert null else insira o local
  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