2012-04-07 8 views
5
select PurchaseOrderID 
    , [244,231] as FirstEmp 
    , [266,274] as secondEmp 
    , [231,241] as ThirdEmp 
    from (select PurchaseOrderID, EmployeeID, TaxAmt 
      from Purchasing.PurchaseOrderHeader 
      Where EmployeeID IN (244, 231, 266, 274, 241) 
       ) SourceTable 
PIVOT (
    SUM(TaxAmt) 
    FOR EmployeeID IN([244,231],[266,274],[231,241]) 
    ) AS PVT 

De la consulta anterior tengo que recuperar los datos para GLcode group-wise usando pivote.necesita una solución para la consulta sql utilizando el pivote

I tienen necesitar un resultado como el siguiente, lo que significa SUM(TaxAmt) para un grupo GLCode IN (244,231) y otra segunda Para GLCode IN (266,274) y tercero es GLCode IN (231,241).

--------------------------------------------------------- 
PurchaseOrderID  [244,231] [266,274] [231,241] 
--------------------------------------------------------- 
1     5678   10456  45643 
2     3456   5643   564 
3     34567   5678   4243 
4     5897   65645   7567 
--------------------------------------------------------------- 
+2

Lo RDBMS? SQL no es suficiente información. Supongo que Oracle o SQL-Server debido al 'pivote'? – Ben

+0

@Ben Según la sintaxis es MSSQL –

+0

@vimal, ¿cuál es su pregunta? –

Respuesta

4
select PurchaseOrderID, 
     sum(case when EmployeeID in (244, 231) then TaxAmt end) as "244,231", 
     sum(case when EmployeeID in (266, 274) then TaxAmt end) as "266,274", 
     sum(case when EmployeeID in (231, 241) then TaxAmt end) as "231,241" 
from PurchaseOrderHeader 
where EmployeeID in(244, 231, 266, 274, 241) 
group by PurchaseOrderID 

versión Pivote:

select PurchaseOrderID, 
     [244]+[231] as "244,231", 
     [266]+[274] as "266,274", 
     [231]+[241] as "231,241" 
from 
    (
    select EmployeeID, TaxAmt, PurchaseOrderID 
    from PurchaseOrderHeader 
    where EmployeeID in(244, 231, 266, 274, 241) 
) as P1 
pivot 
    (
    sum(TaxAmt) for EmployeeID in ([244],[231],[266],[274],[241]) 
) as P2 
+0

Gracias esto es exacto que quiero ... muchas gracias ... –

+0

De nada. Si esto es lo que estaba buscando, debería considerar ["aceptar" esta respuesta] (http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235). –

Cuestiones relacionadas