PIVOTA

0

Please login or register to vote for this query.

(click on this box to dismiss)

Android Enthusiasts

Q&A for enthusiasts and power users of the Android operating system

CREATE TABLE #controles
    ([IdUsuario] int, [IdTipo] int, [Valor] numeric(10, 2))
;

INSERT INTO #controles
    ([IdUsuario], [IdTipo], [Valor])
VALUES
    (102, 1, 5),
    (101, 1, 5),
    (105, 1, 37),
    (109, 1, 59),
    (101, 2, 282),
    (102, 2, 212),
    (105, 2, 78),
    (109, 2, 97),
    (105, 3, 60),
    (102, 3, 123.98),
    (101, 3, 220),
    (109, 3, 87),
    (102, 5, 87.31)
;


CREATE TABLE #TipoControles
([IdTipo] int, [Nombre] nvarchar(30), [Limite] numeric(10, 2))
;

INSERT INTO #TipoControles
    ([IdTipo], [Nombre], [Limite])
VALUES
    (1, 'VecesDias', 5),
    (2, 'VecesMes', 150),
    (3, 'ImporteMes', 5000),
    (4, 'VecesDesdeUltimaFirma', 5),
    (5, 'ImporteDesdeUltimaFirma', 100)
;



select *
from 
(
  select IdUsuario, t.Nombre AS Nombre, Valor
  from #controles c inner join #TipoControles t
  on c.IdTipo = t.IdTipo
  where IdUsuario = 102
) src
pivot
(
MAX(Valor)
  for Nombre in (VecesDias, VecesMes, ImporteMes, VecesDesdeUltimaFirma, ImporteDesdeUltimaFirma)
) piv;

Enter Parameters

Options:
Switch to meta site
loading Hold tight while we fetch your results