Question

My problem is actually I have multiple tables and I'm using two case statements to generate one column for ARV1 and one for ICA1, but I need the results are generated in the same row. When I usecase, generate the two columns but the values are displayed in two rows. What am I missing?

the thing is, i have an invoice the table OINV and have the table INV5 that is the table with the Holding Taxes, i need to put on the same row the invoice with all the Holding Taxes in different columns that are applying on it, thanks

this is the example tables

CREATE TABLE Invoice
(
  Id INT, InvoiceNumber VARCHAR(10), Total INT    
)

INSERT INTO Invoice
VALUES
(1,'200000',100),
(2,'200001',200),
(3,'200002',500),
(4,'200003',700),
(5,'200004',200),
(6,'200005',100),
(7,'200006',300)

CREATE TABLE HoldingTaxes
(
 Id INT, HoldingTaxCode VARCHAR(10),HoldedAmount INT) 
)

INSERT INTO HoldingTaxes
VALUES
(1,'ARV1',20),
(1,'ARV2',30),
(1,'ARV3',35),
(2,'ICA1',20),
(2,'ARV1',10),
(1,'ICA3',50)

I want a query that returns something like this:

InvoiceNumber Total  ARV1 ARV2 ARV3  ICA1  ICA2   ICA3
200000          100   20   30   35   null   null   50

This is what i am trying to do with my real tables

SELECT T0.DocNum [No. Factura],
CASE
WHEN t5.WTCode ='ARV1' and (t5.U_Ret_ML <>0 AND t5.U_Ret_ML is not null)
THEN 'Perro1'
else NULL
end AS ARV1
,
CASE
WHEN t5.WTCode ='ICA1' and (t5.U_Ret_ML <>0 AND t5.U_Ret_ML is not null)
THEN 'Perro2'
else NULL
end AS ICA1
FROM OINV T0
INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
INNER JOIN OSLP T4 ON T0.SlpCode = T4.SlpCode 
INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode 
INNER JOIN OITW T3 ON T2.ItemCode = T3.ItemCode
INNER JOIN INV5 T5 ON T5.AbsEntry = T0.DocEntry
WHERE T1.WhsCode = T3.WhsCode`enter code here`
GROUP BY T0.DocNum,T0.DocDate,T0.DocTotal, T0.GrosProfit, T4.SlpName,T5.WTCODE,t5.U_Ret_ML
Était-ce utile?

La solution 2

I would use PIVOT to solve this - as demonstrated in the SQL Fiddle:

SELECT  EmpName
       ,CASE WHEN ARV1 > 0 THEN 'PERRO1' ELSE NULL END
       ,CASE WHEN ARV2 > 0 THEN 'PERRO2' ELSE NULL END
       ,CASE WHEN ICA1 > 0 THEN 'PERRO3' ELSE NULL END
FROM (SELECT t0.EmpName, t1.Name, t1.Value 
      FROM Table0 t0 INNER JOIN Table1 t1 ON t0.Id = t1.Id ) as  src 
PIVOT (
  MAX(src.Value) 
  FOR src.Name IN ([ARV1],[ARV2],[ICA1])) as p

http://sqlfiddle.com/#!3/a6ff0/2

If your having issues and willing to share your structure, I can put this into a a closer match to what you are using on SQL Fiddle.

Edit:

Based on the update you gave, here is the fiddle I created with the pivot. http://sqlfiddle.com/#!3/47511/4

SELECT * FROM
(SELECT    InvoiceNumber  = Invoice.InvoiceNumber
         ,Total          = Invoice.Total
         ,HoldingTaxCode = HoldingTaxes.HoldingTaxCode
         ,HoldedAmount   = HoldingTaxes.HoldedAmount
FROM      Invoice  
LEFT JOIN HoldingTaxes 
ON        Invoice.Id = HoldingTaxes.Id) PivotSource
PIVOT
(
 SUM(HoldedAmount) FOR HoldingTaxCode IN(ARV1, ARV2, ARV3, ICA1, ICA2, ICA3)  
) PivotData
ORDER BY InvoiceNumber

Autres conseils

Alternative way :

SELECT inv.InvoiceNumber,inv.Total,[ARV1],[ARV2],[ARV3],[ICA1],[ICA2],[ICA3]
FROM INVOICE inv
JOIN(
SELECT id,[ARV1],[ARV2],[ARV3],[ICA1],[ICA2],[ICA3]
FROM
(SELECT * FROM HoldingTaxes ) t1
PIVOT(SUM(HoldedAmount) for HoldingTaxCode in
([ARV1],[ARV2],[ARV3],[ICA1],[ICA2],[ICA3])) t2 ) ht
ON inv.id =ht.id

sql fiddle :http://sqlfiddle.com/#!3/ea3a4/10

The two CASE expressions employ the same logic. Only the value in t5.WTCode is different. Since that column is unlikely to have the value ARV1 and ICA1 at the same time you'll always get a NULL in at least one of the computed columns in any row.

You are putting different case statements for 2 different values. Therefore for each row one of them would be valid and other one null, which is what you are getting.

This is how i resolved my issue, thanks to everyone for the help

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(t5.WTCode) 
            FROM INV5 T5
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @query = 'SELECT Factura, Fecha, SN, TotalFacturaSinImpuesto, Total$Descuento, Total$Impuesto, Total$Retenciones, Total$Factura, CostoTotal$Factura, Margen$Factura, Costo$PromedioInventario, Margen$Inventario, NombreVendedor, ' + @cols + ' from
            (
                select T0.DocNum [Factura], T0.DocDate [Fecha], T0.CardName [SN],SUM(T1.LineTotal) [TotalFacturaSinImpuesto], T0.DiscSum [Total$Descuento],  T0.VatSum [Total$Impuesto],(T0.WTSum*-1) [Total$Retenciones],t5.WTCode [CodigoRetencion],t5.U_Ret_ML [CantidadRetenida],T0.DocTotal [Total$Factura],SUM(T1.GrossBuyPr*T1.Quantity) [CostoTotal$Factura], T0.GrosProfit [Margen$Factura],SUM(T1.Quantity*T3.AvgPrice) [Costo$PromedioInventario],(SUM(T1.LineTotal*T1.Quantity))-(SUM(T1.Quantity*T3.AvgPrice)) [Margen$Inventario], T4.SlpName [NombreVendedor]
                FROM OINV T0
                INNER JOIN INV1 T1 ON T0.DocEntry = T1.DocEntry
                INNER JOIN OSLP T4 ON T0.SlpCode = T4.SlpCode 
                INNER JOIN OITM T2 ON T1.ItemCode = T2.ItemCode 
                INNER JOIN OITW T3 ON T2.ItemCode = T3.ItemCode
                INNER JOIN INV5 T5 ON T5.AbsEntry = T0.DocEntry
                WHERE T1.WhsCode = T3.WhsCode 
                GROUP BY T0.DocNum,T0.DocDate, T0.CardName,T0.BaseAmnt, T0.DiscSum,  T0.VatSum,  T0.WTSum,T0.DocTotal, T0.GrosProfit, T4.SlpName,T5.WTCODE,T5.U_RET_ML
           ) x
            pivot 
            (
                sum(CantidadRetenida)
                for CodigoRetencion in (' + @cols + ')
            ) p '


execute(@query)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top