Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 GROUP BY Question

Author  Topic 

rv498
Yak Posting Veteran

60 Posts

Posted - 2014-03-17 : 14:38:04
So I have this problem and I looked at it for a while and don't know where to start. I sense I need GROUP BY clause.

List customer occupations (use EnglishOccupation) and the number of customers having each occupation.
-- First check to see if there are any customers without an occupation.
-- Add the count returned to see if it makes sense.

below is the table structure

[dbo].[DimCustomer](
[CustomerKey] [int] IDENTITY(1,1) NOT NULL,
[GeographyKey] [int] NULL,
[CustomerAlternateKey] [nvarchar](15) NOT NULL,
[Title] [nvarchar](8) NULL,
[FirstName] [nvarchar](50) NULL,
[MiddleName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[NameStyle] [bit] NULL,
[BirthDate] [date] NULL,
[MaritalStatus] [nchar](1) NULL,
[Suffix] [nvarchar](10) NULL,
[Gender] [nvarchar](1) NULL,
[EmailAddress] [nvarchar](50) NULL,
[YearlyIncome] [money] NULL,
[TotalChildren] [tinyint] NULL,
[NumberChildrenAtHome] [tinyint] NULL,
[EnglishEducation] [nvarchar](40) NULL,
[SpanishEducation] [nvarchar](40) NULL,
[FrenchEducation] [nvarchar](40) NULL,
[EnglishOccupation] [nvarchar](100) NULL,
[SpanishOccupation] [nvarchar](100) NULL,
[FrenchOccupation] [nvarchar](100) NULL,
[HouseOwnerFlag] [nchar](1) NULL,
[NumberCarsOwned] [tinyint] NULL,
[AddressLine1] [nvarchar](120) NULL,
[AddressLine2] [nvarchar](120) NULL,
[Phone] [nvarchar](20) NULL,
[DateFirstPurchase] [date] NULL,
[CommuteDistance] [nvarchar](15) NULL,

rv498
Yak Posting Veteran

60 Posts

Posted - 2014-03-17 : 15:02:38
I got it.

SELECT EnglishOccupation, COUNT(LastName+', '+FirstName) AS NumberOfCustomers
FROM dbo.DimCustomer AS C
GROUP BY EnglishOccupation
Go to Top of Page
   

- Advertisement -