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 |
|
kloepper
Yak Posting Veteran
77 Posts |
Posted - 2004-01-05 : 17:09:39
|
| I'm reorganizing my tables/joins and need to re-write this.I'm trying to return revenue that is greater in the period Q3 than in Q2 and Q2 > Q1.I think the FROM is OK, but the HAVING portion is not correct because of the change in the join.Any ideas on what is correct?Thanks,PaulCREATE VIEW dbo.Improving_RevenueASSELECT ak_Period_Year_Quarter.Period, ak_Name_Symbol.Name, ak_Name_Symbol.Symbol, ak_Hyperlinks.MSN_10Qs, ak_Data_01.RevenueFROM ak_Data_01 INNER JOIN ak_Name_Symbol ON ak_Data_01.[Lookup to ak_Name_Symbol] = ak_Name_Symbol.Symbol INNER JOIN ak_Period_Year_Quarter ON ak_Data_01.[Lookup to ak_Period_Year_Quarter] = ak_Period_Year_Quarter.Period INNER JOIN ak_Hyperlinks ON ak_Data_01.[Lookup to ak_Hyperlinks] = ak_Hyperlinks.Yahoo_MainGROUP BY ak_Period_Year_Quarter.Period, ak_Name_Symbol.Name, ak_Name_Symbol.Symbol, ak_Hyperlinks.MSN_10Qs, ak_Data_01.RevenueHAVINGmax(case when ak_Period_Year_Quarter.Period = '2003_Q3' then ak_Data_01.Revenue else -2000000000 end)> max(case when ak_Period_Year_Quarter.Period = '2003_Q2' then ak_Data_01.Revenue else -2000000000 end)andmax(case when ak_Period_Year_Quarter.Period = '2003_Q2' then ak_Data_01.Revenue else -2000000000 end)> max(case when ak_Period_Year_Quarter.Period = '2003_Q1' then ak_Data_01.Revenue else -2000000000 end) |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2004-01-05 : 18:15:07
|
Simplifying things, your basic (without HAVING) select produces this:-------------------------QQ ... ... Revenue-------------------------Q3 5Q3 8Q3 4Q2 2Q2 6Q1 2Q1 3------------------------- What rows from this query would you like to get? |
 |
|
|
kloepper
Yak Posting Veteran
77 Posts |
Posted - 2004-01-05 : 18:54:44
|
-------------------------QQ Symbol ... Revenue-------------------------Q3 A 5Q3 B 8Q3 C 4Q2 A 2Q2 B 6Q1 A 2Q1 B 3-------------------------Symbol B qualifiesquote: Originally posted by Stoad Simplifying things, your basic (without HAVING) select produces this:-------------------------QQ ... ... Revenue-------------------------Q3 5Q3 8Q3 4Q2 2Q2 6Q1 2Q1 3------------------------- What rows from this query would you like to get?
|
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2004-01-05 : 20:03:06
|
| Think it is. Here Query is your view from your 1st post butwithout HAVING clause (btw, GROUP BY is also useless there).select Period, Revenue from Query where Period='Q1' andRevenue>(select max(Revenue) from Query where Period='Q2' andRevenue>(select max(Revenue) from Query where Period='Q1'))UNION ALLselect Period, Revenue from Query where Period='Q2' andRevenue>(select max(Revenue) from Query where Period='Q1')UNION ALLselect max(Period), max(Revenue) from Query where Period='Q1' |
 |
|
|
|
|
|
|
|