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.
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 rowsMy answer:SELECT C1.LastName+', '+ C1.FirstName AS FullName, C1.YearlyIncomeFROM dbo.DimCustomer AS C1JOIN (SELECT AVG(YearlyIncome) AS AVGYR, CustomerKey FROM dbo.DimCustomer ) AS C2ON C1.CustomerKey = C2.CustomerKeyWHERE 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 rowsMy answer:SELECT C1.LastName+', '+ C1.FirstName AS FullName, C1.YearlyIncomeFROM dbo.DimCustomer AS C1JOIN (SELECT AVG(YearlyIncome) AS AVGYR, CustomerKey FROM dbo.DimCustomer ) AS C2ON C1.CustomerKey = C2.CustomerKeyWHERE C1.YearlyIncome <= C2.AVGYR
|
|
|
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 explanationit should be thisSELECT *FROM(SELECT C1.LastName+', '+ C1.FirstName AS FullName,c1.Gender, C1.YearlyIncome,AVG(C1.YearlyIncome) OVER () AS AvgCustIncomeFROM dbo.DimCustomer AS C1)tWHERE Gender = 'Male'AND YearlyIncome <= AvgCustIncome ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
rv498
Yak Posting Veteran
60 Posts |
Posted - 2014-03-03 : 15:31:17
|
Thanks Visakh. Much obliged |
|
|
rv498
Yak Posting Veteran
60 Posts |
Posted - 2014-03-03 : 15:54:45
|
Thanks bitsmed, nice solution. |
|
|
|
|
|
|
|