我确信这是一个快速的,但尝试了一切,除了答案。我有一个工作正常的数据透视脚本,除了列中的结果,我需要除以100.所以结果/ 100 脚本是
SELECT
USERID AS UserId,APPOINTMENTDATE,
isnull ([1],0) as'other',isnull ([2],0) as 'Medicare'
FROM
(SELECT invoices.USERID, APPOINTMENTDATE,
[total],PAYERCODE,users.LOCATIONID
FROM APPOINTMENTS
where
APPOINTMENTDATE between '2017-01-22' and '2017-01-23'
) AS SourceTable
PIVOT
(
Sum(total)
FOR PAYERCODE IN ([1], [2])
) AS PivotTable;
所以我想做的是: 总和(总)变为总和(总计/ 100)
任何想法.. 干杯
答案 0 :(得分:0)
添加此示例,因为它有效。继承人:http://sqlfiddle.com/#!6/8eef1/1/0
示例代码的问题在于您尝试对表本身而不是数据透视表进行选择。您需要在“源表”中定义列。然后透视PayerCode,然后执行Select * FROM(Sourcetable Pivottable)
drop table TestingPivot
Create table TestingPivot
( Total INT,
Payercode INT,
LocationID INT,
UserID INT,
Name VARCHAR(100) )
Insert into TestingPivot values(100, 1, 1, 1, 'Ryan')
Insert into TestingPivot values(200, 2, 2, 2, 'Mike')
Insert into TestingPivot values(300, 3, 3, 3, 'John')
Select * from (
Select Total/100 as NewTotal, PayerCode, UserID, LocationID,
case when PayerCode = 1 then 'Other'
when PayerCode = 2 then 'Medicare'
else ''
end as Type
from TestingPivot
) as t
Pivot
(
Sum(NewTotal)
FOR PayerCode IN ("1","2")
) as P;