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 |
|
mclam
Starting Member
6 Posts |
Posted - 2011-10-28 : 11:13:05
|
| Solved |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-28 : 11:17:47
|
| would you mind explaining what you're trying to achieve with above code?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
mclam
Starting Member
6 Posts |
Posted - 2011-10-28 : 11:37:54
|
| v |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2011-10-28 : 11:46:31
|
it would really help with at least a sample of the DDL..but..if I get your meaning...you want to normalize your data..right?Without samples we can only guess..and NO ONE is going to try and decipher your ...dynamic sql..something like this?CREATE TABLE myTable99(PK_Col varchar(10), Jan int, Feb int, Mar int)CREATE TABLE myTable98(PK_Col varchar(10), Month_Col char(3), Amt_Col int)GOINSERT INTO myTable99(PK_Col, Jan, Feb, Mar)SELECT 'xxx',1,2,3 UNION ALLSELECT 'xyz',1,2,3 UNION ALLSELECT 'zzz',1,2,3GOINSERT INTO myTable98(PK_Col, Month_Col, Amt_Col)SELECT PK_Col, 'Jan', JanFROM myTable99UNION ALLSELECT PK_Col, 'Feb', FebFROM myTable99UNION ALLSELECT PK_Col, 'Mar', MarFROM myTable99GOSELECT * FROM myTable98GODROP TABLE myTable99, myTable98GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
mclam
Starting Member
6 Posts |
Posted - 2011-10-28 : 12:05:02
|
| I |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-28 : 12:31:50
|
you can dispense with dynamic sql like this (assuming you're using sql 2005 or above)DECLARE @DateSK intSET @DateSK = 20110901DECLARE @year intSET @year = 2011INSERT INTO dbo.A([Date_SK], [CHRG_TYPE], [CD], [SVP],[Category], [J_Chrg], [F_Chrg], [M_Chrg])SELECT @DateSK,CASE WHEN FieldName LIKE '%Pln' THEN 'B' WHEN FieldName LIKE '%Act' THEN 'A' ELSE 'F'END,CASE WHEN FieldName LIKE '%Pln' THEN CAST (@year as varchar(4)) +'P' WHEN FieldName LIKE '%Act' THEN 'ACT ELSE '8+4'END,[SVP],[Category],MAX(CASE WHEN FieldName LIKE 'Jan_%' THEN FieldValue ELSE NULL END),MAX(CASE WHEN FieldName LIKE 'Feb_%' THEN FieldValue ELSE NULL END),MAX(CASE WHEN FieldName LIKE 'Mar_%' THEN FieldValue ELSE NULL END)FROM(SELECT[SVP],[Category],REPLACE(REPLACE(REPLACE(FieldName,'Jan_',''),'Feb_',''),'Mar_','') AS FieldType,FieldName,FieldValueFROM dbo.BUNPIVOT (FieldValue FOR FieldName IN (Jan_Act , Feb_Act , Mar_Act , Jan_8+4F ,Feb_8+4F ,Mar_8+4F,...))u)mGROUP BY [SVP],[Category],FieldType ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-28 : 13:15:43
|
if its 2000, it will be likeDECLARE @DateSK intSET @DateSK = 20110901DECLARE @year intSET @year = 2011INSERT INTO dbo.A([Date_SK], [CHRG_TYPE], [CD], [SVP],[Category], [J_Chrg], [F_Chrg], [M_Chrg])SELECT @DateSK, 'B',CAST (@year as varchar(4)) +'P',[SVP],[Category],Jan_Pln,feb_Pln,Mar_Pln FROM dbo.BUNION ALLSELECT @DateSK, 'A','ACT',[SVP],[Category],Jan_Act,feb_Act,Mar_Act FROM dbo.BUNION ALLSELECT @DateSK, 'F','8+4',[SVP],[Category],Jan_Jan_8+4F,feb_Jan_8+4F,Mar_Jan_8+4F FROM dbo.B ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mclam
Starting Member
6 Posts |
Posted - 2011-10-28 : 14:10:12
|
| y |
 |
|
|
mclam
Starting Member
6 Posts |
Posted - 2011-10-28 : 17:19:01
|
| T |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-29 : 00:47:00
|
| so you dont want any other values (SD,Category etc)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
mclam
Starting Member
6 Posts |
Posted - 2011-10-29 : 09:09:53
|
| There was no problem with other values like CD, category etc. The only issue is with the above result set (Jan_Chrg..Dec_Chrg) that I mentioned in the above response. Any ideas on how that can be achieved? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-29 : 09:39:36
|
| are CD and Category fields having different values for above set?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|