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 |
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2006-11-06 : 14:59:51
|
I currently have the following:SELECT ISNULL(Babr, 'other') AS St, SicDescription, SUM(AccountMV) AS mvFROM dbo.SnapsRawWHERE (MonthEnd = '08/01/2006')GROUP BY Babr, SicDescriptionORDER BY BabrResults Look like this:FL | Amusement |2641940.7000FL | Engineering | 82802330.4000IN | Museums | 13418262.3900IN | Electric | 1141768.8400I would like to add a column that gives me the % of the overall marketvalue for the particular state, example:FL | Amusement |2641940.7000 | 20%FL | Engineering | 82802330.4000 | 2%IN | Museums | 13418262.3900 | 3%IN | Electric | 1141768.8400 |19%The number in which i need to divide by would be built fromselect branchstate, sum(accountmv) from snapsraw where monthend = '08/01/2006' group by branchstate Results look like this:Florida 1990129983.4800Indiana 962445383.5500How do i get the % number to show up in the extra column, making sure it divides the correct states overall market value. Apologizes if this sounds confusing, trying to phrase question best I can |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-06 : 15:32:25
|
Try a INNER JOIN with a derived table likeSELECT State, SUM(MarketValue) smv from ... gropu by StateAnd then in the select, divide by derived table's smv column.Peter LarssonHelsingborg, Sweden |
 |
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2006-11-07 : 08:22:45
|
Appreciate the reply and advice, somewhat new to the SQL world, could you expand upon what you said? I guess i'm confused on how I will divide each states market value and category by the overall market value for the particular state, is that where the inner join comes in? |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-07 : 08:28:24
|
Something like thisSELECT ISNULL(s.Babr, 'other') AS St, s.SicDescription, SUM(s.AccountMV) AS mv, SUM(s.AccountMV) / q.ps as PercentileFROM dbo.SnapsRaw sINNER JOIN ( select branchstate, sum(accountmv) ps from snapsraw where monthend = '08/01/2006' group by branchstate ) q on q.branschstate = s.babrWHERE s.MonthEnd = '08/01/2006'GROUP BY s.Babr, s.SicDescriptionORDER BY s.Babr It it does not work, please try to tweak the query before asking again...Peter LarssonHelsingborg, Sweden |
 |
|
duhaas
Constraint Violating Yak Guru
310 Posts |
Posted - 2006-11-07 : 08:39:14
|
WOW, I cant thank you enough, I really appreciate you spending the time and assisting me with his. |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-07 : 08:49:41
|
You could also try to use COMPUTE keyword.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|
|