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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Issue with dot in sql pivot query

Author  Topic 

cijojohn
Starting Member

3 Posts

Posted - 2009-10-12 : 14:14:37
I have table like the following and I need to show the data in the following format.
SPM_ID TAM_V_ACCOUNT_NAME Size %
1 CHINA 14.5 10
1 CHINA M 20
1 CHINA L 30
1 CHINA XL 40


SPM_ID TAM_V_ACCOUNT_NAME 14.5 M L XL
1 China 10% 20% 30% 40%


I’m using a pivot query to convert row in to columns.
I have an issue in using Dot (‘.’) in my pivot query.
When using 14.5 in my query is failing. The query runs fine when using anything other than Dot.
Can we use Dot in a [pivot query?

SELECT
SPM_ID,
TAM_V_ACCOUNT_NAME,
txtTotal,
[14.5],
[M],
[L],
[XL]
FROM
(
SELECT
SPM_ID,
TAM_V_ACCOUNT_NAME,
SR_DESC,
100 as txtTotal,
SPM_SIZE_DISTRO_PERCENT
FROM
TB_SIZE_PROFILES_MAPPING

WHERE
SPM_STATUS = 'A'
) as TB_SIZE_PROFILES_MAPPING PIVOT ( sum(SPM_SIZE_DISTRO_PERCENT) FOR SR_DESC IN ([14.5],[M],[L],[XL]) )as PivotedTable

Any help is appreciated.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-10-12 : 14:44:48
Your code worked for me (after I corrected the inconsistencies in your sample data column names)

declare @TB_SIZE_PROFILES_MAPPING table
(SPM_ID int, TAM_V_ACCOUNT_NAME varchar(10), SR_DESC varchar(10), [SPM_SIZE_DISTRO_PERCENT] int)
insert @TB_SIZE_PROFILES_MAPPING
select 1, 'CHINA', '14.5', 10
union all select 1, 'CHINA', 'M', 20
union all select 1, 'CHINA', 'L', 30
union all select 1, 'CHINA', 'XL', 40

SELECT SPM_ID,
TAM_V_ACCOUNT_NAME,
txtTotal,
[14.5],
[M],
[L],
[XL]
FROM
(
SELECT SPM_ID,
TAM_V_ACCOUNT_NAME,
SR_DESC,
100 as txtTotal,
SPM_SIZE_DISTRO_PERCENT
FROM @TB_SIZE_PROFILES_MAPPING
--WHERE SPM_STATUS = 'A'
) as TB_SIZE_PROFILES_MAPPING
PIVOT ( sum(SPM_SIZE_DISTRO_PERCENT) FOR SR_DESC IN ([14.5],[M],[L],[XL])
) as PivotedTable

output:
SPM_ID TAM_V_ACCOUNT_NAME txtTotal 14.5 M L XL
----------- ------------------ ----------- ----------- ----------- ----------- -----------
1 CHINA 100 10 20 30 40


Be One with the Optimizer
TG
Go to Top of Page

StevePD
Starting Member

2 Posts

Posted - 2009-10-23 : 22:19:04
Hey!
You may want to try the code found at this webpage:
http://sites.google.com/site/sqlsimple/home/solutions
It shows how to do your pivot dynamically.

You also might want to try using keyword "QUOTENAME" to automatically put you field names in brackets [].

Hope this helps

StevePD
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-10-24 : 06:02:08
Hey!
You just want to increase the traffic on that webpage with lots of google ads.
Please stop that shit!


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -