我使用相同的斯科特模式。问题是这样的。 使用描述性标签显示empno,sal,TA,DA,HRA,总限额,保险,TDS,Netdeductions,Netsal。
这里Ta是sal的30%,DA是sal的40%,hra是sal的50%,保险是5%sal和tds是5%sal。 总配额为ta + da + hra。扣除总额为ins + tds 现在netsal是(sal + allowances-total deductions)
我试图使用声明
来实现这一点WITH T1 AS
(SELECT (30 / 100 * SAL) AS TA FROM EMP),
T2 AS
(SELECT (40 / 100 * SAL) AS DA FROM EMP),
T3 AS
(SELECT (50 / 100 * SAL) AS HRA FROM EMP)
SELECT Empno, TA, DA, HRA, (ta + da + hra)
FROM EMP, T1, T2, T3
group by empno;
像这样的事情。逻辑错了我能理解。
答案 0 :(得分:2)
只需在一个查询中执行此操作:
select e.*, (ta + da + hra) as total_allowances,
(sal + (ta + da + hra) - (ins + tds)) as net_salary
from (select empno, (sal * 30/100) as ta, (sal * 40/100) as da,
(sal * 50/100) as hra,
(sal * 0.05) as ins,
(sal * 0.05) as tds
from emp
) e;
我不明白group by
的含义。您没有聚合函数,并且表格名称为emp
,我希望每一行都有empno
的唯一值。
答案 1 :(得分:1)
select empno,
sal,
.3 * sal as ta,
.4 * sal as da,
.5 * sal as hra,
.05 * sal as insurance,
.05 * sal as tds,
(.3 * sal) + (.4 * sal) + (.5 * sal) as total_allowances,
(.05 * sal) + (.05 * sal) as total_deductions,
sal + ((.3 * sal) + (.4 * sal) + (.5 * sal)) - ((.05 * sal) + (.05 * sal)) as netsal
from emp
我不知道您正在尝试实现WITH子句。您的t1 / t2查询分别在整个表中执行每个计算。您可以在同一查询中执行每个计算。并且似乎不涉及任何多个表,因此您不需要任何连接。