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)
 need help,.. how make column dinamically to rows

Author  Topic 

anaze
Starting Member

6 Posts

Posted - 2013-09-15 : 04:44:18
-- Create Table
CREATE TABLE [dbo].[fa_wcb_rpt_bak](
[urut] [int] IDENTITY(1,1) NOT NULL,
[mandiri1] [decimal](18, 0) NULL,
[mandiri2] [decimal](18, 0) NULL,
[bni] [decimal](18, 0) NULL,
[bri] [decimal](18, 0) NULL,
CONSTRAINT [PK_fa_wcb_rpt_bak] PRIMARY KEY CLUSTERED
(
[urut] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

--INSERT DATA
INSERT INTO fa_wcb_rpt_bak VALUES (100,200,300,400)
INSERT INTO fa_wcb_rpt_bak VALUES (500,600,700,800)

--VIEW DATA
select * from fa_wcb_rpt_bak
urut mandiri1 mandiri2 bni bri
1 100 200 300 400
2 500 600 700 800

--I WANT RESULT IS :

mandiri1 600
mandiri2 800
bni 1000
bri 1200

assumpsion column is dinamically,...
please help me, about pivot or others way..
Thanks alot before.

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2013-09-16 : 02:53:12
DECLARE @x TABLE
(
customer_code varchar(max),
Value decimal(10,2),
Value1 decimal(10,2)
);

INSERT @x SELECT 'mandiri1',100,500
UNION ALL SELECT 'mandiri2',200,600
UNION ALL SELECT 'bni',300,700
UNION ALL SELECT 'bri',400,800

select x.Customer_Code,(x.Value + x.Value1) as Value
from @x as x
inner join fa_wcb_rpt_bak as p
on x.value = p.bni
or x.value = p.bri
or x.value = p.mandiri1
or x.value = p.mandiri2
--or x.Value1 = p.bni
--or x.Value1 = p.bri
--or x.Value1 = p.mandiri1
--or x.Value1 = p.mandiri2
group by x.Value,x.Value1,x.customer_code


veeranjaneyulu
Go to Top of Page

anaze
Starting Member

6 Posts

Posted - 2013-09-25 : 11:07:03
Thanks you Mr. veeranjaneyulu
Go to Top of Page
   

- Advertisement -