我的表格如下:
var TicketInfo =
[{
for(i = 0; i < data_json.RESULT.length; i++){
y: 'total',
a: data_json.RESULT[i].total_cus,
b: data_json.RESULT[i].new_total_cus
}
}]
Morris.Bar({
element: 'morris-bar-chart',
data: TicketInfo,
xkey: 'y',
ykeys: ['a','b'],
labels: ['abc','xyz'],
hideHover: 'auto',
resize: true
});
我需要写一个查询来计算工资,直到每个月,输出将如下所示。每月工资将增加到上个月的工资。
EmpID SalaryMonth Salary
1 January 20000
1 February 15000
1 March 25000
我尝试使用游标解决方案,但我们是否有任何其他解决方案可以实现此目的。
答案 0 :(得分:1)
试试这个!
select EmpID,SalaryMonth,SUM(Salary) OVER(ORDER BY EmpID desc
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)
AS Salary
FROM t
答案 1 :(得分:0)
请使用“累积总和”,如
选择t1.id,t1.SomeNumt,SUM(t2.SomeNumt)作为总和 来自@t t1 t1.id&gt; = t2.id上的内连接@t t2 group by t1.id,t1.SomeNumt 按t1.id命令
答案 2 :(得分:0)
试试这会帮到你
select * into #emp_sal from
(
select 1 EmpID ,'January'as SalaryMonth, 20000 Salary
union all
select 1 , 'February', 15000
union all
select 1 ,'March', 25000
union all
select 2 ,'January', 30000
union all
select 2 ,'February', 8000
)as a
select EmpID, SalaryMonth ,Salary,
sum(Salary)over(partition by empid order by SalaryMonth rows BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)as Actual_Salary
from #emp_sal
答案 3 :(得分:0)
免责声明:您应该认真考虑使用标准SQL Server日期或时间戳列来表示SalaryMonth
。使用字符串月份名称会产生各种问题,因此我的详细解答如下。
我的方法是使用CTE为每个月的名称(cte1
)分配一个数字位置。然后,我将其加入到原始表格中{cte2
)。最后,我们可以自我加入cte2
来计算表格中的滚动工资。
可能有一种方法可以使用月份名称来创建某种类型的日期对象,但我无法想到它,无论如何这样做可能需要分配一个虚拟年份,可能还有其他东西。
WITH cte1 AS (
SELECT 'January' AS SalaryMonth, 1 AS MonthPos
UNION ALL
SELECT 'Februrary', 2
UNION ALL
SELECT 'March', 3
UNION ALL
SELECT 'April', 4
UNION ALL
SELECT 'May', 5
UNION ALL
SELECT 'June', 6
UNION ALL
SELECT 'July', 7
UNION ALL
SELECT 'August', 8
UNION ALL
SELECT 'September', 9
UNION ALL
SELECT 'October', 10
UNION ALL
SELECT 'November', 11
UNION ALL
SELECT 'December', 12
),
WITH cte2 AS (
SELECT t1.EmpID,
t1.SalaryMonth,
t2.MonthPos,
t1.Salary
FROM yourTable t1
INNER JOIN cte1 t2
ON t1.SalaryMonth = t2.SalaryMonth
)
SELECT t1.EmpID,
t1.SalaryMonth,
SUM(t2.Salary) AS Salary
FROM cte2 t1
INNER JOIN cte2 t2
ON t1.MonthPos >= t2.MonthPos
GROUP BY t1.EmpID,
t1.SalaryMonth