Calculate Head Cout

  • USE AdventureWorks2012;

    GO

    SELECT p.FirstName, p.LastName

    ,ROW_NUMBER() OVER (ORDER BY a.PostalCode) AS "Row Number"

    ,RANK() OVER (ORDER BY a.PostalCode) AS Rank

    ,DENSE_RANK() OVER (ORDER BY a.PostalCode) AS "Dense Rank"

    ,NTILE(4) OVER (ORDER BY a.PostalCode) AS Quartile

    ,s.SalesYTD

    ,a.PostalCode

    FROM Sales.SalesPerson AS s

    INNER JOIN Person.Person AS p

    ON s.BusinessEntityID = p.BusinessEntityID

    INNER JOIN Person.Address AS a

    ON a.AddressID = p.BusinessEntityID

    WHERE TerritoryID IS NOT NULL AND SalesYTD <> 0;

    In the query above, I want to know how do I calculate the head count, as in How many people stay in the same Postal Code area?

  • Try the following

    COUNT(1) OVER (Partition BY a.PostalCode)

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply