Convert columns of a table into rows

0

On a system I am currently working on, there is a table this way

  Id_Sucursal   Id_Servicio F_Descarga  Id_Tecnico  Con_Costo_1 Id_Producto_1   Cantidad_1  Costo_1 Precio_1    Tipo_1  Oferta_1    Precio_Oferta_1 Cantidad_Oferta_1   Con_Costo_2 Id_Producto_2   Cantidad_2  Costo_2 Precio_2    Tipo_2  Oferta_2    Precio_Oferta_2 Cantidad_Oferta_2   Con_Costo_3 Id_Producto_3   Cantidad_3  Costo_3 Precio_3    Tipo_3  Oferta_3    Precio_Oferta_3 Cantidad_Oferta_3   Con_Costo_4 Id_Producto_4   Cantidad_4  Costo_4 Precio_4    Tipo_4  Oferta_4    Precio_Oferta_4 Cantidad_Oferta_4   Con_Costo_5 Id_Producto_5   Cantidad_5  Costo_5 Precio_5    Tipo_5  Oferta_5    Precio_Oferta_5 Cantidad_Oferta_5   Con_Costo_6 Id_Producto_6   Cantidad_6  Costo_6 Precio_6    Tipo_6  Oferta_6    Precio_Oferta_6 Cantidad_Oferta_6   Con_Costo_7 Id_Producto_7   Cantidad_7  Costo_7 Precio_7    Tipo_7  Oferta_7    Precio_Oferta_7 Cantidad_Oferta_7   Con_Costo_8 Id_Producto_8   Cantidad_8  Costo_8 Precio_8    Tipo_8  Oferta_8    Precio_Oferta_8 Cantidad_Oferta_8   Con_Costo_9 Id_Producto_9   Cantidad_9  Costo_9 Precio_9    Tipo_9  Oferta_9    Precio_Oferta_9 Cantidad_Oferta_9   Con_Costo_10    Id_Producto_10  Cantidad_10 Costo_10    Precio_10   Tipo_10 Oferta_10   Precio_Oferta_10    Cantidad_Oferta_10  Con_Costo_11    Id_Producto_11  Cantidad_11 Costo_11    Precio_11   Tipo_11 Oferta_11   Precio_Oferta_11    Cantidad_Oferta_11  Con_Costo_12    Id_Producto_12  Cantidad_12 Costo_12    Precio_12   Tipo_12 Oferta_12   Precio_Oferta_12    Cantidad_Oferta_12  Con_Costo_13    Id_Producto_13  Cantidad_13 Costo_13    Precio_13   Tipo_13 Oferta_13   Precio_Oferta_13    Cantidad_Oferta_13  Con_Costo_14    Id_Producto_14  Cantidad_14 Costo_14    Precio_14   Tipo_14 Oferta_14   Precio_Oferta_14    Cantidad_Oferta_14  Con_Costo_15    Id_Producto_15  Cantidad_15 Costo_15    Precio_15   Tipo_15 Oferta_15   Precio_Oferta_15    Cantidad_Oferta_15  F_Entrada   Usr_Entrada F_Salida    Usr_Salida  F_Ult_Mod   Usr_Ult_Mod Estatus
CORDOBA 0000000001006   2013-03-23 14:56:10.107         1084    1   200.00  401.36  P   N   0.00    0.00        982 1   800.00  1800.32 P   N   0.00    0.00        933 2   87.49   700.26  P   N   0.00    0.00        968 1   219.68  520.00  P   N   0.00    0.00        1083    1   174.59  300.02  P   N   0.00    0.00        954 1   200.00  350.32  P   N   0.00    0.00        1010    5   88.00   1760.90 P   N   0.00    0.00        1222    1   472.00  865.08  P   N   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00    2013-03-23 14:56:10.107 TCAMANDIR   1900-01-01 00:00:00.000     2013-03-23 14:56:10.107 TCAMANDIR   A
CORDOBA 0000000002777   2013-07-31 11:10:41.543         960 1   152.00  300.01  P   N   0.00    0.00        1083    1   174.59  300.02  P   N   0.00    0.00        1084    1   200.00  401.36  P   N   0.00    0.00        982 1   800.00  1800.32 P   N   0.00    0.00        1222    1   472.00  865.08  P   N   0.00    0.00        1010    3   87.49   1050.00 P   N   0.00    0.00        979 1   630.00  1205.82 P   N   0.00    0.00        968 1   258.05  590.00  P   N   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00    2013-07-31 11:10:41.543 TCACBACAPTURA   1900-01-01 00:00:00.000     2013-07-31 11:10:41.543 TCACBACAPTURA   A
CORDOBA 0000000009229   2014-08-12 13:15:34.827         1089    1   1125.80 2300.00 P   N   0.00    0.00        1110    4   71.92   1400.00 P   N   0.00    0.00        1112    1   657.34  1150.00 P   N   0.00    0.00        1245    1   68.91   360.00  P   N   0.00    0.00        876 1   76.18   360.00  P   N   0.00    0.00        1091    1   102.62  490.00  P   N   0.00    0.00        890 1   260.13  490.01  P   N   0.00    0.00        1091    1   102.62  490.00  P   N   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00    2014-08-12 13:15:34.827 COMPRAS 1900-01-01 00:00:00.000     2014-08-12 13:15:34.827 COMPRAS A
CORDOBA 0000000002108   2013-06-29 11:29:05.357         1222    1   472.00  865.08  P   N   0.00    0.00        968 1   258.05  590.00  P   N   0.00    0.00        1084    1   200.00  401.36  P   N   0.00    0.00        960 1   152.00  300.01  P   N   0.00    0.00        1083    1   174.59  300.02  P   N   0.00    0.00        982 1   800.00  1800.32 P   N   0.00    0.00        1010    4   87.49   1400.00 P   N   0.00    0.00        1024    2   308.24  1019.98 P   N   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00            0   0.00    0.00    0   0   0.00    0.00    2013-06-29 11:29:05.357 TCACBACAPTURA   1900-01-01 00:00:00.000     2013-06-29 11:29:05.357 TCACBACAPTURA   A

I want to change the shape of the table, in a way that the info stored there does work, to something like this:

Id_Sucursal Id_Servicio F_Descarga          Id_Tecnico  Con_Costo_1 Id_Producto_1   Cantidad_1  Costo_1 Precio_1    Tipo_1  Oferta_1    Precio_Oferta_1 Cantidad_Oferta_1
CORDOBA         34991   18/11/2017 13:37                                    2856            2    20.58    59.7          P       N           0               0
CORDOBA         34991   18/11/2017 13:37                                    2861            4     2.59    15.04         P       N           0               0
CORDOBA         34991   18/11/2017 13:37                                    2867            4     3.88    22.52         P       N           0               0

Is it possible in sql server?

I read about pivot and unpivot, but I do not quite understand how it works, and therefore how I would implement it in this case.

    
asked by antonio_veneroso 28.12.2018 в 17:42
source

1 answer

2

One way to do this is to create a new table with the desired structure and fill it with the data from the original table by inserting with select for each set of different data that you want to convert into tuples or rows in the new table.

I give an example in which the original table has two sets of data (_1 and _2 per tuple) and in the new table each a a constitute a tuple.

Schema (PostgreSQL v10.0)

CREATE TABLE test (
  Id_Sucursal VARCHAR(50),
  Id_Servicio VARCHAR(50),
  Id_Tecnico VARCHAR(50),
  Con_Costo_1 VARCHAR(50),
  Id_Producto_1 INT,
  Cantidad_1 INT,
  Costo_1 DECIMAL,
  Precio_1 DECIMAL,
  Tipo_1 VARCHAR(2),
  Oferta_1 VARCHAR(2),
  Precio_Oferta_1 DECIMAL,
  Cantidad_Oferta_1 INT,
  Con_Costo_2 VARCHAR(50),
  Id_Producto_2 INT,
  Cantidad_2 INT,
  Costo_2 DECIMAL,
  Precio_2 DECIMAL,
  Tipo_2 VARCHAR(2),
  Oferta_2 VARCHAR(2),
  Precio_Oferta_2 DECIMAL,
  Cantidad_Oferta_2 INT
);
INSERT INTO test VALUES (
'CORDOBA', '0000000001006', '', 
'', 2856, 2, 20.58, 59.7, 'P','N', 0,0, 
'', 2857, 3, 40.53, 79.7, 'P','N', 0,0
);
INSERT INTO test VALUES (
  'CORDOBA', '0000000001007','',
  '', 2858, 2, 10.58, 39.7, 'P','N', 0,0,
  '', 2859, 3, 20.53, 89.7, 'P','N', 0,0
);

CREATE TABLE test2 (
  Id_Sucursal VARCHAR(50),
  Id_Servicio VARCHAR(50),
  Id_Tecnico VARCHAR(50),
  Con_Costo_1 VARCHAR(50),
  Id_Producto_1 INT,
  Cantidad_1 INT,
  Costo_1 DECIMAL,
  Precio_1 DECIMAL,
  Tipo_1 VARCHAR(2),
  Oferta_1 VARCHAR(2),
  Precio_Oferta_1 DECIMAL,
  Cantidad_Oferta_1 INT
);

INSERT INTO test2
(SELECT Id_Sucursal, Id_Servicio, Id_Tecnico,
  Con_Costo_1, Id_Producto_1, Cantidad_1, Costo_1, Precio_1,
  Tipo_1, Oferta_1, Precio_Oferta_1, Cantidad_Oferta_1
FROM test) ;
INSERT INTO test2
(SELECT Id_Sucursal, Id_Servicio, Id_Tecnico,
  Con_Costo_2, Id_Producto_2, Cantidad_2, Costo_2, Precio_2,
  Tipo_2, Oferta_2, Precio_Oferta_2, Cantidad_Oferta_2
FROM test);

Query # 1

SELECT * FROM test;

| id_sucursal | id_servicio   | id_tecnico | con_costo_1 | id_producto_1 | cantidad_1 | costo_1 | precio_1 | tipo_1 | oferta_1 | precio_oferta_1 | cantidad_oferta_1 | con_costo_2 | id_producto_2 | cantidad_2 | costo_2 | precio_2 | tipo_2 | oferta_2 | precio_oferta_2 | cantidad_oferta_2 |

| ----------- | ------------- | ---------- | ----------- | ------------- | ---------- | ------- | -------- | ------ | -------- | --------------- | ----------------- | ----------- | ------------- | ---------- | ------- | -------- | ------ | -------- | --------------- | ----------------- |

| CORDOBA     | 0000000001006 |            |             | 2856          | 2          | 20.58   | 59.7     | P      | N        | 0               | 0                 |             | 2857          | 3          | 40.53   | 79.7     | P      | N        | 0               | 0                 |
| CORDOBA     | 0000000001007 |            |             | 2858          | 2          | 10.58   | 39.7     | P      | N        | 0               | 0                 |             | 2859          | 3          | 20.53   | 89.7     | P      | N        | 0               | 0                 |

Query # 2

SELECT * FROM test2 ORDER BY id_servicio;

| id_sucursal | id_servicio   | id_tecnico | con_costo_1 | id_producto_1 | cantidad_1 | costo_1 | precio_1 | tipo_1 | oferta_1 | precio_oferta_1 | cantidad_oferta_1 |

| ----------- | ------------- | ---------- | ----------- | ------------- | ---------- | ------- | -------- | ------ | -------- | --------------- | ----------------- |

| CORDOBA     | 0000000001006 |            |             | 2856          | 2          | 20.58   | 59.7     | P      | N        | 0               | 0                 |
| CORDOBA     | 0000000001006 |            |             | 2857          | 3          | 40.53   | 79.7     | P      | N        | 0               | 0                 |
| CORDOBA     | 0000000001007 |            |             | 2858          | 2          | 10.58   | 39.7     | P      | N        | 0               | 0                 |
| CORDOBA     | 0000000001007 |            |             | 2859          | 3          | 20.53   | 89.7     | P      | N        | 0               | 0                 |

View on DB Fiddle

    
answered by 28.12.2018 / 19:53
source