Question

I have a small query below which i want to have a Count of each HOUSE_ID and Grouped By LOCATION_ID however it is not grouping by LOCATION_ID because the HOUSE_ID's are different. I want it to count HOUSE_ID's by LOCATION_ID's regardless of the HOUSE_ID.

QUERY

SELECT 
COUNT(HOUSE_ID) AS Count,
LOCATION_ID,
ZONE,
AREA
FROM TABLE
WHERE SITE_ID = 'ABC'
AND LOCATION_ID NOT LIKE ('%LAND%')
GROUP BY LOCATION_ID, HOUSE_ID, ZONE, AREA

Expected Result

_____________________________
|Count|LOCATION_ID|ZONE|AREA|
|¯¯¯¯¯|¯¯¯¯¯¯¯¯¯¯¯|¯¯¯¯|¯¯¯¯|
|  4  |  LOCA     | 2  |  1 |
|  7  |  LOCB     | 6  |  2 |
|  3  |  LOCC     | 3  |  1 |
|  9  |  LOCD     | 5  |  7 |
|  6  |  LOCE     | 7  |  4 |
|  2  |  LOCF     | 2  |  1 |
|  8  |  LOCG     | 7  |  5 |
|  7  |  LOCH     | 9  |  1 |
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

Actual Result

_____________________________
|Count|LOCATION_ID|ZONE|AREA|
|¯¯¯¯¯|¯¯¯¯¯¯¯¯¯¯¯|¯¯¯¯|¯¯¯¯|
|  1  |  LOCA     | 2  |  1 |
|  1  |  LOCA     | 6  |  2 |
|  1  |  LOCA     | 3  |  1 |
|  1  |  LOCA     | 5  |  7 |
|  1  |  LOCA     | 7  |  4 |
|  1  |  LOCA     | 2  |  1 |
|  1  |  LOCA     | 7  |  5 |
|  1  |  LOCA     | 9  |  1 |
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Was it helpful?

Solution 2

Got it, Needed to Count(*)!!

SELECT 
COUNT(*) AS Count,
SUM(AREA) AS AREA
LOCATION_ID,
ZONE,
FROM TABLE
WHERE SITE_ID = 'ABC'
AND LOCATION_ID NOT LIKE ('%LAND%')
GROUP BY LOCATION_ID, ZONE

OTHER TIPS

You have to remove HOUSE_ID from group by clause. Without source data I can only guess that you also need agregate functions for ZONE and AREA column, MAX fro example. Try below solution:

SELECT 
COUNT(HOUSE_ID) AS Count,
LOCATION_ID,
MAX(ZONE),
MAX(AREA)
FROM TABLE
WHERE SITE_ID = 'ABC'
AND LOCATION_ID NOT LIKE ('%LAND%')
GROUP BY LOCATION_ID
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top