我有一個表SQL樞不處理數據缺失
Create table dbo.temp(ID int, ExpenseID int, ExpenseName Varchar(100) null, ExpenseDate date null, Value decimal(19,4) null)
Insert into dbo.temp values(1,100,'CostClothes','2015-01-01',100.0000)
Insert into dbo.temp values(1,200,'Discount','2015-01-01',1.0000)
Insert into dbo.temp values(2,100,'CostClothes','2016-01-01',250.0000)
Insert into dbo.temp values(2,200,'Discount','2016-01-01',1.0000)
Insert into dbo.temp values(1,100,'CostClothes','2014-01-01',500.0000)
Insert into dbo.temp values(2,200,'Discount','2014-01-01',5.0000)
現在我想轉動的ExpenseID和預期輸出此表的是
Create table dbo.output(ID int, CostClothes decimal(19,4) null, Discount decimal(19,4) null, ExpenseDate date null)
insert into dbo.output values (1,100.0000,1.0000,'2015-01-01')
insert into dbo.output values (1,500.0000,NULL,'2014-01-01')
insert into dbo.output values (2,NULL,5.0000,'2014-01-01')
insert into dbo.output values (2,100.0000,1.0000,'2016-01-01')
這是我有什麼,我沒有得到正確的輸出
SELECT ID,ISNULL([100],0) as CostClothes,ISNULL([200],0) as Discount,expenseDate
FROM
(
SELECT * FROM dbo.temp
) AS p
PIVOT
(
MAX(Value) for ExpenseID in ([100],[200])
) AS PV1
如何更改查詢
謝謝 MR