January 1, 2009 at 3:33 am
Hi Gurus,
I have my data like seen below
Month Country Tran
Jul 2003 US 1271
Jun 2003 UK 1107
Aug 2003 FR 1096
Jul 2003 UK 1216
Jun 2003 US 1104
Aug 2003 UK 1094
Jul 2003 FR 1321
Jun 2003 FR 1210
Aug 2003 US 1109
I need a additional column that should give me percentage of contribution of country. I.e, For first record my column should contains
Tran / (All country tran for particular month).
How do i this?
January 1, 2009 at 3:55 am
Can you post a sample of what you expect your output to look like?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 1, 2009 at 4:08 am
Am looking for output like
Month Country Tran Column1
Jul 2003 US 1271 0.33 (1271/sum(jul2003-US+jul2003-Uk+jul2003-FR)
Jun 2003 UK 1107 0.32 (1107/sum(jun2003-US+jun2003-Uk+jun2003-FR)
Aug 2003 FR 1096 0.33
Jul 2003 UK 1216 0.32
Jun 2003 US 1104 0.32
Aug 2003 UK 1094 0.33
Jul 2003 FR 1321 0.35
Jun 2003 FR 1210 0.35
Aug 2003 US 1109 0.34
My DB is SQL 2005
January 1, 2009 at 4:27 am
I reckon this should do it.
[font="Courier New"]DROP TABLE #Temp
CREATE TABLE #Temp ([Month] CHAR(8), Country CHAR(2), [Tran] INT)
INSERT INTO #Temp ([Month], Country, [Tran])
SELECT 'Jul 2003', 'US', 1271 UNION ALL
SELECT 'Jun 2003', 'UK', 1107 UNION ALL
SELECT 'Aug 2003', 'FR', 1096 UNION ALL
SELECT 'Jul 2003', 'UK', 1216 UNION ALL
SELECT 'Jun 2003', 'US', 1104 UNION ALL
SELECT 'Aug 2003', 'UK', 1094 UNION ALL
SELECT 'Jul 2003', 'FR', 1321 UNION ALL
SELECT 'Jun 2003', 'FR', 1210 UNION ALL
SELECT 'Aug 2003', 'US', 1109
SELECT a.*, d.TranMonthTotal, a.[Tran]/(d.TranMonthTotal*1.00) AS CountryContribution
FROM #Temp a
INNER JOIN (SELECT [Month], SUM([Tran]) AS TranMonthTotal
FROM #Temp
GROUP BY [Month]) d
ON d.[Month] = a.[Month]
[/font]
Check the link below, it will show you how to create sample data and table structures to help others answer your questions.
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 1, 2009 at 4:40 am
The Best way to handle this is..
Instead of storing the value as column in table
Write a Function which will return the contribution percentage
example F_GET_COUNTRY_CONTRIBUTION ( country_name, Year)
By passing the country name and year you can get the value..
If you add more rows, everytime creating pivot is not practical
Venkat
January 1, 2009 at 5:14 am
Thanks for your help Gurus.
January 1, 2009 at 5:21 am
sarran.achari (1/1/2009)
Thanks for your help Gurus.
You're welcome. Is your problem solved?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
January 1, 2009 at 5:51 am
Yes. Actually I wanted that additional column is SSRS.. but couldn't do it there.. so thot of doing it in the query itself
January 1, 2009 at 1:55 pm
Venkat (1/1/2009)
The Best way to handle this is..Instead of storing the value as column in table
Write a Function which will return the contribution percentage
example F_GET_COUNTRY_CONTRIBUTION ( country_name, Year)
By passing the country name and year you can get the value..
If you add more rows, everytime creating pivot is not practical
Venkat
I don't see where a pivot was asked for.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply