Why does Location=emptystring occupy two rows on "GROUP BY Location" on a data.stackexchange query?

36 Views Asked by At

Why does Location=emptystring occupy two rows on this data.stackexchange query? https://data.stackexchange.com/stackoverflow/query/1627756/test

SELECT 
SUM(Reputation) AS rep,
Location
FROM Users
GROUP BY Location
ORDER BY rep DESC

enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

If you try this verison you'll see Location has both blank string and null values

SELECT SUM(Reputation) AS rep, 
  Location, 
  IIF(location is null, 1, 0) NullLocation
FROM Users
GROUP BY Location, iif(location is null, 1, 0)
ORDER BY rep DESC;

You can IsNull / Coalesce to sum both together:

SELECT SUM(Reputation) AS rep, 
  ISNULL(Location, '') Location
FROM Users
GROUP BY isnull(Location, '')
ORDER BY rep DESC;