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
 I'm stuck please help

Author  Topic 

rv498
Yak Posting Veteran

60 Posts

Posted - 2014-03-03 : 13:59:57
Hi, my first post here in this forum.
I am stuck with this for last hour. Please help.

Question:
Find all males in the customers table with an income less than or the same as the average
-- income of all customers. List last name, a comma and space, and first name in one column and yearly income.
-- 4404 rows


My answer:
SELECT C1.LastName+', '+ C1.FirstName AS FullName, C1.YearlyIncome
FROM dbo.DimCustomer AS C1
JOIN (SELECT AVG(YearlyIncome) AS AVGYR, CustomerKey FROM dbo.DimCustomer ) AS C2
ON C1.CustomerKey = C2.CustomerKey
WHERE C1.YearlyIncome <= C2.AVGYR

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2014-03-03 : 14:15:49
quote:
Originally posted by rv498

Hi, my first post here in this forum.
I am stuck with this for last hour. Please help.

Question:
Find all males in the customers table with an income less than or the same as the average
-- income of all customers. List last name, a comma and space, and first name in one column and yearly income.
-- 4404 rows


My answer:
SELECT C1.LastName+', '+ C1.FirstName AS FullName, C1.YearlyIncome
FROM dbo.DimCustomer AS C1
JOIN (SELECT AVG(YearlyIncome) AS AVGYR, CustomerKey FROM dbo.DimCustomer ) AS C2
ON C1.CustomerKey = C2.CustomerKey
WHERE
C1.YearlyIncome <= C2.AVGYR

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-03-03 : 14:51:29
Where are you looking for males condition?
as per your explanation
it should be this

SELECT *
FROM
(
SELECT C1.LastName+', '+ C1.FirstName AS FullName,c1.Gender, C1.YearlyIncome,AVG(C1.YearlyIncome) OVER () AS AvgCustIncome
FROM dbo.DimCustomer AS C1
)t
WHERE Gender = 'Male'
AND YearlyIncome <= AvgCustIncome


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

rv498
Yak Posting Veteran

60 Posts

Posted - 2014-03-03 : 15:31:17
Thanks Visakh. Much obliged
Go to Top of Page

rv498
Yak Posting Veteran

60 Posts

Posted - 2014-03-03 : 15:54:45
Thanks bitsmed, nice solution.
Go to Top of Page
   

- Advertisement -