Skip to content
Advertisement

Substring, As, Count issue

I need to extract part of an ID Field (ParcelID) and then use the count field to count the number of times it is in the database. This is what I did: I created a view with the following:

SELECT  ParcelID, SUBSTRING(ParcelID, 1, 12) AS PID, COUNT(PID) AS PIDCount
FROM    dbo.RptCDMGISExtract

It says that Invalid Column Name. I guess I understand (PID) does not exist. How can I complete this?

Advertisement

Answer

Based on your description, you seem to want:

SELECT SUBSTRING(ParcelID, 1, 12) AS PID, COUNT(*) AS PIDCount
FROM dbo.RptCDMGISExtract
GROUP BY SUBSTRING(ParcelID, 1, 12);

Note that many databases would use LEFT(ParcelID, 12) instead of the SUBSTRING() function.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement