我的问题实际上是我有多个表,并且我使用两条case
语句生成一列 forARV1
和一列 for ICA1
,但我需要在同一行中生成结果。当我使用case
时,生成两列,但值显示在两行中。我错过了什么?
问题是,我有一张 OINV 表的发票,并且有一张 INV5 表,即持有税的表,我需要将所有持有税的发票放在同一行,并在不同的列中应用,谢谢
这是示例表
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)
我想要一个返回如下内容的查询:
InvoiceNumber Total ARV1 ARV2 ARV3 ICA1 ICA2 ICA3
200000 100 20 30 35 null null 50
这就是我想要用我的真实桌子做的事情
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