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.