Mysql
 sql >> Base de données >  >> RDS >> Mysql

MySQL Comment attribuer une valeur nulle aux colonnes non correspondantes lors de l'utilisation de Group By

Vous pouvez utiliser union adn ayant .. ayant count(*)> 1 insérez null sinon insérez l'emplacement

  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