como usar Pivot Table.

10/12/2009 - 16:42 por fer | Informe spam
Hola,
Tengo una tabla definida de la siguiente manera.

Machine | Product1 | Product2 | Product3 | Product4 | Product5
-
2 | 5001 | 5002 | 5003 | 5004
| 5005
3 | 5011 | 5022 | 5033 | 5044
| 5055
4 | 5021 | 5032 | 5043 | 5054
| 5065
5 | 5031 | 5042 | 5053 | 5064
| 5075

Me han dicho que lo haga con un pivot pero no tengo ni idea,
Necesito dejarla de la siguiente manera.

| 2 | 3 | 4 |
5
-
Product1 | 5001 | 5002 | 5003 | 5004
|
Product2 | 5011 | 5022 | 5033 | 5044
|
Product3 | 5021 | 5032 | 5043 | 5054
|
Product4 | 5031 | 5042 | 5053 | 5064
|



Muchas Gracias de Antemano.

Preguntas similare

Leer las respuestas

#6 fer
11/12/2009 - 21:41 | Informe spam
Perdona, me equivoqué, los campos product1,product2,etc debende ser las filas

"fer" wrote:

El valor de machine deben de ser columnas, estos campos siempre van a ser los
mismos, 2,3,4,5,6, y los campos de product1 product2, etc. serán variables y
deben de ser las columnas

Un saludo.

"Alejandro Mesa" wrote:

> Corrección,
>
> > Ahora, quieres transponer los valores de la columna [Machine] a filas, y los
> > valores de las columnas [Product?] as filas, correcto?
>
> Ahora, quieres transponer los valores de la columna [Machine] a columnas, y
> los
> valores de las columnas [Product?] a filas, correcto?
>
> AMB
>
>
> "Alejandro Mesa" wrote:
>
> > fer,
> >
> > > Yo parto de este select
> >
> > > SELECT Maquine, Product1, Product2, Product3, Product4, Product5
> > > FROM tablamachie
> >
> > Si usas el script que adjunte a mi post, y ejecutas:
> >
> >
> > SELECT Machine, Product1, Product2, Product3, Product4, Product5
> > FROM @T;
> >
> > Obtendras:
> >
> > Machine Product1 Product2 Product3 Product4 Product5
> > 2 5001 5002 5003 5004 5005
> > 3 5011 5022 5033 5044 5055
> > 4 5021 5032 5043 5054 5065
> > 5 5031 5042 5053 5064 5075
> >
> > Ahora, quieres transponer los valores de la columna [Machine] a filas, y los
> > valores de las columnas [Product?] as filas, correcto?
> >
> > Primero debemos transponer las columnas [Product?] hacia filas, para lo cual
> > usamos el operador "unpivot".
> >
> > SELECT
> > MACHINE,
> > Product,
> > [Value]
> > FROM
> > @T
> > UNPIVOT
> > (
> > [Value]
> > FOR Product IN ([Product1], [Product2], [Product3], [Product4],
> > [Product5])
> > ) AS U
> >
> >
> > que nos dara el resultado:
> >
> > MACHINE Product Value
> > 2 Product1 5001
> > 2 Product2 5002
> > 2 Product3 5003
> > 2 Product4 5004
> > 2 Product5 5005
> > 3 Product1 5011
> > 3 Product2 5022
> > 3 Product3 5033
> > 3 Product4 5044
> > 3 Product5 5055
> > 4 Product1 5021
> > 4 Product2 5032
> > 4 Product3 5043
> > 4 Product4 5054
> > 4 Product5 5065
> > 5 Product1 5031
> > 5 Product2 5042
> > 5 Product3 5053
> > 5 Product4 5064
> > 5 Product5 5075
> >
> >
> > Si usamos este resultado para el proximo paso que seria transponer los
> > valores de la columna [Machine] hacia columnas, y para lo cual uso una tabla
> > derivada formada por el query anterior, entonces obtenemos el resultado
> > deseado.
> >
> > SELECT
> > Product,
> > [2],
> > [3],
> > [4],
> > [5]
> > FROM
> > (
> >
> > ACA VA EL QUERY ANTERIOR QUE USA EL OPERADOR UNPIVOT
> >
> > ) AS T
> > PIVOT
> > (
> > MIN([Value])
> > FOR MACHINE IN ([2], [3], [4], [5])
> > ) AS P;
> > GO
> >
> > Si no conoces de antemano los posibles valores de la columna [machine],
> > entonces tendras que recurrir a hacer pivot dinamico.
> >
> > Yo no voy a exponer como hacer esto dinamicamente, pero te adjunto un link a
> > un problema similar. Chequea la respuesta de "Hunchback".
> >
> >
> > Pivot Example when you don't know the exact values to Pivot on
> > http://social.msdn.microsoft.com/fo...0f10ebaed/
> >
> >
> > AMB
> >
> >
> >
> > "fer" wrote:
> >
> > > Muchas gracias Alejandro, pero todavia tengo dudas.
> > >
> > > Yo parto de este select y obtengo la primera tabla. Los número 5001,
> > > 5002etc. son variables que cambian en la base de datos, no termino de
> > > entender muy bien el codigo que has puesto
> > >
> > > SELECT Maquine, Product1, Product2, Product3, Product4, Product5
> > > FROM tablamachie
> > >
> > >
> > >
> > >
> > > "Alejandro Mesa" wrote:
> > >
> > > > Debes primero transponer las columnas [Product?] a filas usando el operador
> > > > "unpivot", para luego transponer los valores de la columna [MACHINE] a
> > > > columnas.
> > > >
> > > > USE tempdb;
> > > > GO
> > > > DECLARE @T TABLE (
> > > > MACHINE int,
> > > > Product1 int,
> > > > Product2 int,
> > > > Product3 int,
> > > > Product4 int,
> > > > Product5 int
> > > > );
> > > > SET NOCOUNT ON;
> > > > INSERT INTO @T VALUES(2,5001,5002,5003,5004,5005);
> > > > INSERT INTO @T VALUES(3,5011,5022,5033,5044,5055);
> > > > INSERT INTO @T VALUES(4,5021,5032,5043,5054,5065);
> > > > INSERT INTO @T VALUES(5,5031,5042,5053,5064,5075);
> > > > SET NOCOUNT OFF;
> > > >
> > > > SELECT
> > > > Product,
> > > > [2],
> > > > [3],
> > > > [4],
> > > > [5]
> > > > FROM
> > > > (
> > > > SELECT
> > > > MACHINE,
> > > > Product,
> > > > [Value]
> > > > FROM
> > > > @T
> > > > UNPIVOT
> > > > (
> > > > [Value]
> > > > FOR Product IN ([Product1], [Product2], [Product3], [Product4],
> > > > [Product5])
> > > > ) AS U
> > > > ) AS T
> > > > PIVOT
> > > > (
> > > > MIN([Value])
> > > > FOR MACHINE IN ([2], [3], [4], [5])
> > > > ) AS P;
> > > > GO
> > > >
> > > > Resultado:
> > > >
> > > > Product 2 3 4 5
> > > > Product1 5001 5011 5021 5031
> > > > Product2 5002 5022 5032 5042
> > > > Product3 5003 5033 5043 5053
> > > > Product4 5004 5044 5054 5064
> > > > Product5 5005 5055 5065 5075
> > > >
> > > >
> > > > Si deseas las filas ordenadas por [Product], entonces debes adicionar la
> > > > clausula "order by".
> > > >
> > > > AMB
> > > >
> > > >
> > > >
> > > > "fer" wrote:
> > > >
> > > > > Hola,
> > > > > Tengo una tabla definida de la siguiente manera.
> > > > >
> > > > > Machine | Product1 | Product2 | Product3 | Product4 | Product5
> > > > > -
> > > > > 2 | 5001 | 5002 | 5003 | 5004
> > > > > | 5005
> > > > > 3 | 5011 | 5022 | 5033 | 5044
> > > > > | 5055
> > > > > 4 | 5021 | 5032 | 5043 | 5054
> > > > > | 5065
> > > > > 5 | 5031 | 5042 | 5053 | 5064
> > > > > | 5075
> > > > >
> > > > > Me han dicho que lo haga con un pivot pero no tengo ni idea,
> > > > > Necesito dejarla de la siguiente manera.
> > > > >
> > > > > | 2 | 3 | 4 |
> > > > > 5
> > > > > -
> > > > > Product1 | 5001 | 5002 | 5003 | 5004
> > > > > |
> > > > > Product2 | 5011 | 5022 | 5033 | 5044
> > > > > |
> > > > > Product3 | 5021 | 5032 | 5043 | 5054
> > > > > |
> > > > > Product4 | 5031 | 5042 | 5053 | 5064
> > > > > |
> > > > >
> > > > >
> > > > >
> > > > > Muchas Gracias de Antemano.
> > > > >
Respuesta Responder a este mensaje
#7 Alejandro Mesa
12/12/2009 - 15:59 | Informe spam
fer,

Te recomiendo que trates de hacerlo por ti solo(a). Esa es la mojor manera
de aprender. Claro esta, que puedes postear cualquier duda y veremos si te
podemos ayudar.

Trata de entender como funcionan ambos operadores, PIVOT / UNPIVOT. Luego
puedes usar ambas soluciones, la posteada en este hilo mas la solucion en el
link adjuntado en mi previo msg, para encontrar la solucion final..


AMB



"fer" wrote:

Perdona, me equivoqué, los campos product1,product2,etc debende ser las filas

"fer" wrote:

> El valor de machine deben de ser columnas, estos campos siempre van a ser los
> mismos, 2,3,4,5,6, y los campos de product1 product2, etc. serán variables y
> deben de ser las columnas
>
> Un saludo.
>
> "Alejandro Mesa" wrote:
>
> > Corrección,
> >
> > > Ahora, quieres transponer los valores de la columna [Machine] a filas, y los
> > > valores de las columnas [Product?] as filas, correcto?
> >
> > Ahora, quieres transponer los valores de la columna [Machine] a columnas, y
> > los
> > valores de las columnas [Product?] a filas, correcto?
> >
> > AMB
> >
> >
> > "Alejandro Mesa" wrote:
> >
> > > fer,
> > >
> > > > Yo parto de este select
> > >
> > > > SELECT Maquine, Product1, Product2, Product3, Product4, Product5
> > > > FROM tablamachie
> > >
> > > Si usas el script que adjunte a mi post, y ejecutas:
> > >
> > >
> > > SELECT Machine, Product1, Product2, Product3, Product4, Product5
> > > FROM @T;
> > >
> > > Obtendras:
> > >
> > > Machine Product1 Product2 Product3 Product4 Product5
> > > 2 5001 5002 5003 5004 5005
> > > 3 5011 5022 5033 5044 5055
> > > 4 5021 5032 5043 5054 5065
> > > 5 5031 5042 5053 5064 5075
> > >
> > > Ahora, quieres transponer los valores de la columna [Machine] a filas, y los
> > > valores de las columnas [Product?] as filas, correcto?
> > >
> > > Primero debemos transponer las columnas [Product?] hacia filas, para lo cual
> > > usamos el operador "unpivot".
> > >
> > > SELECT
> > > MACHINE,
> > > Product,
> > > [Value]
> > > FROM
> > > @T
> > > UNPIVOT
> > > (
> > > [Value]
> > > FOR Product IN ([Product1], [Product2], [Product3], [Product4],
> > > [Product5])
> > > ) AS U
> > >
> > >
> > > que nos dara el resultado:
> > >
> > > MACHINE Product Value
> > > 2 Product1 5001
> > > 2 Product2 5002
> > > 2 Product3 5003
> > > 2 Product4 5004
> > > 2 Product5 5005
> > > 3 Product1 5011
> > > 3 Product2 5022
> > > 3 Product3 5033
> > > 3 Product4 5044
> > > 3 Product5 5055
> > > 4 Product1 5021
> > > 4 Product2 5032
> > > 4 Product3 5043
> > > 4 Product4 5054
> > > 4 Product5 5065
> > > 5 Product1 5031
> > > 5 Product2 5042
> > > 5 Product3 5053
> > > 5 Product4 5064
> > > 5 Product5 5075
> > >
> > >
> > > Si usamos este resultado para el proximo paso que seria transponer los
> > > valores de la columna [Machine] hacia columnas, y para lo cual uso una tabla
> > > derivada formada por el query anterior, entonces obtenemos el resultado
> > > deseado.
> > >
> > > SELECT
> > > Product,
> > > [2],
> > > [3],
> > > [4],
> > > [5]
> > > FROM
> > > (
> > >
> > > ACA VA EL QUERY ANTERIOR QUE USA EL OPERADOR UNPIVOT
> > >
> > > ) AS T
> > > PIVOT
> > > (
> > > MIN([Value])
> > > FOR MACHINE IN ([2], [3], [4], [5])
> > > ) AS P;
> > > GO
> > >
> > > Si no conoces de antemano los posibles valores de la columna [machine],
> > > entonces tendras que recurrir a hacer pivot dinamico.
> > >
> > > Yo no voy a exponer como hacer esto dinamicamente, pero te adjunto un link a
> > > un problema similar. Chequea la respuesta de "Hunchback".
> > >
> > >
> > > Pivot Example when you don't know the exact values to Pivot on
> > > http://social.msdn.microsoft.com/fo...0f10ebaed/
> > >
> > >
> > > AMB
> > >
> > >
> > >
> > > "fer" wrote:
> > >
> > > > Muchas gracias Alejandro, pero todavia tengo dudas.
> > > >
> > > > Yo parto de este select y obtengo la primera tabla. Los número 5001,
> > > > 5002etc. son variables que cambian en la base de datos, no termino de
> > > > entender muy bien el codigo que has puesto
> > > >
> > > > SELECT Maquine, Product1, Product2, Product3, Product4, Product5
> > > > FROM tablamachie
> > > >
> > > >
> > > >
> > > >
> > > > "Alejandro Mesa" wrote:
> > > >
> > > > > Debes primero transponer las columnas [Product?] a filas usando el operador
> > > > > "unpivot", para luego transponer los valores de la columna [MACHINE] a
> > > > > columnas.
> > > > >
> > > > > USE tempdb;
> > > > > GO
> > > > > DECLARE @T TABLE (
> > > > > MACHINE int,
> > > > > Product1 int,
> > > > > Product2 int,
> > > > > Product3 int,
> > > > > Product4 int,
> > > > > Product5 int
> > > > > );
> > > > > SET NOCOUNT ON;
> > > > > INSERT INTO @T VALUES(2,5001,5002,5003,5004,5005);
> > > > > INSERT INTO @T VALUES(3,5011,5022,5033,5044,5055);
> > > > > INSERT INTO @T VALUES(4,5021,5032,5043,5054,5065);
> > > > > INSERT INTO @T VALUES(5,5031,5042,5053,5064,5075);
> > > > > SET NOCOUNT OFF;
> > > > >
> > > > > SELECT
> > > > > Product,
> > > > > [2],
> > > > > [3],
> > > > > [4],
> > > > > [5]
> > > > > FROM
> > > > > (
> > > > > SELECT
> > > > > MACHINE,
> > > > > Product,
> > > > > [Value]
> > > > > FROM
> > > > > @T
> > > > > UNPIVOT
> > > > > (
> > > > > [Value]
> > > > > FOR Product IN ([Product1], [Product2], [Product3], [Product4],
> > > > > [Product5])
> > > > > ) AS U
> > > > > ) AS T
> > > > > PIVOT
> > > > > (
> > > > > MIN([Value])
> > > > > FOR MACHINE IN ([2], [3], [4], [5])
> > > > > ) AS P;
> > > > > GO
> > > > >
> > > > > Resultado:
> > > > >
> > > > > Product 2 3 4 5
> > > > > Product1 5001 5011 5021 5031
> > > > > Product2 5002 5022 5032 5042
> > > > > Product3 5003 5033 5043 5053
> > > > > Product4 5004 5044 5054 5064
> > > > > Product5 5005 5055 5065 5075
> > > > >
> > > > >
> > > > > Si deseas las filas ordenadas por [Product], entonces debes adicionar la
> > > > > clausula "order by".
> > > > >
> > > > > AMB
> > > > >
> > > > >
> > > > >
> > > > > "fer" wrote:
> > > > >
> > > > > > Hola,
> > > > > > Tengo una tabla definida de la siguiente manera.
> > > > > >
> > > > > > Machine | Product1 | Product2 | Product3 | Product4 | Product5
> > > > > > -
> > > > > > 2 | 5001 | 5002 | 5003 | 5004
> > > > > > | 5005
> > > > > > 3 | 5011 | 5022 | 5033 | 5044
> > > > > > | 5055
> > > > > > 4 | 5021 | 5032 | 5043 | 5054
> > > > > > | 5065
> > > > > > 5 | 5031 | 5042 | 5053 | 5064
> > > > > > | 5075
> > > > > >
> > > > > > Me han dicho que lo haga con un pivot pero no tengo ni idea,
> > > > > > Necesito dejarla de la siguiente manera.
> > > > > >
> > > > > > | 2 | 3 | 4 |
> > > > > > 5
> > > > > > -
> > > > > > Product1 | 5001 | 5002 | 5003 | 5004
> > > > > > |
> > > > > > Product2 | 5011 | 5022 | 5033 | 5044
> > > > > > |
> > > > > > Product3 | 5021 | 5032 | 5043 | 5054
> > > > > > |
> > > > > > Product4 | 5031 | 5042 | 5053 | 5064
> > > > > > |
> > > > > >
> > > > > >
> > > > > >
> > > > > > Muchas Gracias de Antemano.
> > > > > >
email Siga el debate Respuesta Responder a este mensaje
Ads by Google
Help Hacer una pregunta AnteriorRespuesta Tengo una respuesta
Search Busqueda sugerida