I've got a Table
CREATE TABLE [dbo].[newTable](
[EBELN] [nvarchar](20) NOT NULL,
[EBELP] [nvarchar](10) NOT NULL,
[VGABE] [nvarchar](2) NOT NULL,
[MENGE] [numeric](15, 3) NULL,
[DMBTR] [numeric](15, 2) NULL
)
It has these records
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('3000000004', '0001', '1', 1 , 27.95 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('3000000004', '0001', '2', 1 , 27.95 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('3000000004', '0002', '1', 1 , 10.95 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('3000000004', '0002', '2', 1 , 10.95 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('3000000010', '0001', '1', 1 , 22.95 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('3000000010', '0001', '2', 1 , 22.95 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('3000000010', '0002', '1', 1 , 32.95 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('3000000010', '0002', '2', 1 , 32.95 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('4151516119', '0001', '1', 1 , 400.00 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('4151516119', '0001', '1', 1 , 400.00 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('4151516119', '0001', '2', 1 , 400.00 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('4151516119', '0002', '1', 1 , 200.00 )
Go
insert into dbo.newTable(EBELN, EBELP, VGABE, MENGE , DMBTR) values('4151516119', '0002', '2', 1 , 200.00 )
Go
Thats the SELECT *
EBELN EBELP VGABE MENGE DMBTR
-------------------- ---------- ----- -------------- ---------------------------------------
3000000004 0001 1 1.000 27.95
3000000004 0001 2 1.000 27.95
3000000004 0002 1 1.000 10.95
3000000004 0002 2 1.000 10.95
3000000010 0001 1 1.000 22.95
3000000010 0001 2 1.000 22.95
3000000010 0002 1 1.000 32.95
3000000010 0002 2 1.000 32.95
4151516119 0001 1 1.000 400.00
4151516119 0001 1 1.000 400.00
4151516119 0001 2 1.000 400.00
4151516119 0002 1 1.000 200.00
4151516119 0002 2 1.000 200.00
3000000004 0001 2 1.000 27.95
3000000004 0002 1 1.000 10.95
3000000004 0002 2 1.000 10.95
3000000010 0001 1 1.000 22.95
3000000010 0001 2 1.000 22.95
3000000010 0002 1 1.000 32.95
3000000010 0002 2 1.000 32.95
4151516119 0001 1 1.000 400.00
4151516119 0001 1 1.000 400.00
4151516119 0001 2 1.000 400.00
4151516119 0002 1 1.000 200.00
4151516119 0002 2 1.000 200.00
4151516177 0002 6 1.000 111.00
4151516177 0002 8 1.000 111.00
What I need and want is a dynamic pivot which generates this result
+------------+-------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
| EBELN | EBELP | c_DMBTR_1 | c_MENGE_1 | c_DMBTR_2 | c_MENGE_2 | c_DMBTR_6 | c_MENGE_6 | c_DMBTR_8 | c_MENGE_8 |
+------------+-------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
| 3000000004 | 0001 | 27.95 | 1 | 27.95 | 1 | NULL | NULL | NULL | NULL |
| 3000000004 | 0002 | 10.95 | 1 | 10.95 | 1 | NULL | NULL | NULL | NULL |
| [...] | | | | | | | | | |
| 4151516119 | 0001 | 800.00 | 1 | 400.00 | 1 | NULL | NULL | NULL | NULL |
| 4151516177 | 0002 | NULL | NULL | NULL | NULL | 111.00 | 1 | 111.00 | 1 |
+------------+-------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
I tried several solution but none is getting the result i want to get.
What I need is the values in VGABE which are (1,2,6,7,8,9,P,R) to be concatenated to the string names of c_DMBTR and c_MENGE. But it's possible that for example P or 7 is not used, so I will not want to have the Columns for that. I think making it dynamic is the only possible way to do so.
The Value from VGABE should be added to the Column name like 'c_MENGE_'+VGABE and it must be in order, which means it has to start with lowest value from VGABE with all columns and then the next value till every used value from vgabe.
If there is for one position (EBELP) more than one Value from VGABE like two times 1 for POS 0001 as you can see by EBELN 4151516119 I have to SUM(DMBTR) for this position and VGABE Value. The same I have to do with MENGE.
Is it possible to do in one query or in a stored procedure. I don't know how to get this result, I'm stuck right now. Or is there another way to do which I'm not aware of?