这是在SQL Server中。我正在寻找一种方法来添加一列来从201602中减去201601列。
IE如果在程序运行后,201601的tranamt为10,000,而201602的tranamt为11,000,我想要一个将产生1,000的列出现在这两列的旁边。
这适用于数千行,因此无法使用括号。
我在where子句之后尝试了一个subselect,它甚至没有出现,甚至远远接近正确,并使我的枢轴没有返回任何东西,好像它被否定了。我有大约一个月的SQL经验。
SELECT *
FROM (
SELECT c.BLDGID AS 'BLDGID',
c.LEASID AS 'LEASID',
l.OCCPNAME AS 'OCCPNAME',
l.SUITID AS 'SUITID',
c.INCCAT AS 'INCCAT',
c.SRCCODE AS 'SRCCODE',
c.TRANAMT AS 'TRANAMT',
c.PERIOD AS 'PERIOD'
FROM SQLDATA.dbo.LEAS l
INNER JOIN SQLDATA.dbo.CMLEDG c
ON l.BLDGID = c.BLDGID AND l.LEASID = c.LEASID
WHERE c.BLDGID 87000
AND c.INCCAT JYL
AND c.SRCCODE NOT LIKE 'CR'
AND c.SRCCODE NOT LIKE 'PR'
AND DESCRPTN NOT LIKE 'SECURITY APPLIED'
AND c.PERIOD BETWEEN 201601 and 201602
) as t
PIVOT (
SUM(TRANAMT)
FOR PERIOD IN ([201601],[201602])
) revenueperspace
ORDER BY BLDGID, SUITID
答案 0 :(得分:1)
在SELECT *中,您可以将其更改为
SELECT *, revenueperspace.[201602]-revenueperspace.[201601] as Diff