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

MySQL Come assegnare un valore nullo a colonne non corrispondenti quando si utilizza Raggruppa per

Puoi usare union adn avendo .. avendo count(*)> 1 inserisci null altrimenti inserisci la posizione

  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