請問我有一資料表欄位有 , [公司ID] , [年], [月], [當月營收], [當月累計營收]
[公司ID] , [年], [月]為組合鍵
我做一個查詢表來計算 營收月成長率
語法用以下SQL
SELECT a.[公司ID], a.[年], a.[月], a.[當月營收], a.[當月累計營收],
(a.[當月營收]-b.[當月營收])*100/b.[當月營收] AS 月成長率
FROM 月營收表 AS a
LEFT JOIN 月營收表 AS b
ON (a.[公司ID] = b.[公司ID] and a.[年] = b.[年] and a.[月]-1 = b.[月] )
or (a.[公司ID] = b.[公司ID] and a.[年]-1 = b.[年] and a.[月]=1 and b.[月]=12 );