SQL GROUP BY with a condition?

I am trying to copy a table named "CLOUD" to a new one called "t1_temp" when grouping rows based on a column named "tag". BUT I want this to happen only if the Neighborhood column is identical.

Request in progress:

INSERT INTO t1_temp (id, NeighborhoodID, power, tag) SELECT id, NeighborhoodID, SUM(power), tag FROM CLOUD GROUP BY tag ORDER BY NeighborhoodID 

So for example: enter image description here

The third entry should not be grouped with 1st and 4th cells, as the β€œNeighborhoodID” does not match.

I hope I understand, if not comment, thanks.

+4
source share
3 answers

You can group by several columns, therefore:

 INSERT INTO t1_temp (id, NeighborhoodID, power, tag) SELECT id, NeighborhoodID, SUM(power), tag FROM CLOUD GROUP BY tag, NeighborhoodID ORDER BY NeighborhoodID 
+2
source

I assume you want to add NeighborhoodID to the proposal group as well

  SELECT id, NeighborhoodID, SUM(power), tag FROM CLOUD GROUP BY tag, NeighborhoodID ORDER BY NeighborhoodID 
0
source
 INSERT INTO t1_temp (id, NeighborhoodID, power, tag) SELECT min(id), NeighborhoodID, SUM(power), tag FROM CLOUD GROUP BY NeighborhoodID, tag ORDER BY NeighborhoodID 

Does it help?

0
source

Source: https://habr.com/ru/post/1346619/


All Articles