样本表
EmployeeID | AssignmentID | WageCode | CompanyName | BillRate | BillTotal
1 | 1 | Regular | CompanyOne | 10 | 400
1 | 2 | Regular | CompanyTwo | 11 | 440
1 | 1 | Overtime | CompanyOne | 15 | 150
1 | 1 | Mileage | CompanyOne | 0 | 20
2 | 3 | Regular | CompanyThree| 20 | 800
2 | 3 | Regular | CompanyThree| 20 | 800
2 | 3 | Overtime | CompanyThree| 30 | 90
2 | 3 | Mileage | CompanyThree| 0 | 60我只想显示WageCode为“正则”的行,按EmployeeID、WageCode、AssignmentID、CompanyName和BillRate分组,并将其他工资代码转换为列。
最终结果应该如下所示:
EmployeeID | AssignmentID | CompanyName | RegBillRate | RegBill | OTBillRate | OTBill | MileageBill
1 | 1 | CompanyOne | 10 | 400 | 15 | 150 | 20
1 | 2 | CompanyTwo | 11 | 440 | 0 | 0 | 0
2 | 3 | CompanyThree| 20 | 1600 | 30 | 90 | 60有什么更好的方法来做这件事,而不是像这样的语句:
with regular as
(select EmployeeID, AssignmentID, CompanyName, BillRate, sum(BillTotal) Total from SampleTable where wage code = 'Regular' group by EmployeeID, AssignmentID, CompanyName, BillRate
),
overtime as
(select EmployeeID, AssignmentID, CompanyName, BillRate, sum(BillTotal) Total from SampleTable where wage code = 'Overtime' group by EmployeeID, AssignmentID, CompanyName, BillRate
),
mileage as
(select EmployeeID, AssignmentID, CompanyName, BillRate, sum(BillTotal) Total from SampleTable where wage code = 'Mileage' group by EmployeeID, AssignmentID, CompanyName, BillRate
)
select r.*, o.BillRate, o.Total, m.Total
from regular r
left outer join overtime o
on r.EmployeeID = o.EmployeeID and r.AssignmentID= o.AssignmentID and r.CompanyName= o.CompanyName and r.BillRate= o.BillRateand
left outer join mileage m
on r.EmployeeID = m.EmployeeID and r.AssignmentID= m.AssignmentID and r.CompanyName= m.CompanyName and r.BillRate= m.BillRateand 上面的查询是被转述的,可能不起作用。
有什么更好的方法可以通过解码和枢轴的结合来实现呢?单枢轴表可以吗?
发布于 2019-02-12 22:10:11
Oracle:子句允许您编写从Oracle11g开始的交叉表查询。这意味着您可以聚合结果并将行旋转为列。
解码:/PLSQL函数具有IF-然后- The语句的功能。
对于用例,可以通过以下方式使用枢轴和解码:
SELECT
EmployeeID, AssignmentID, CompanyName,
decode(REG_BILLRATE, NULL, 0, REG_BILLRATE) AS REG_BILLRATE,
decode(REG_FILL, NULL, 0, REG_FILL) AS REG_FILL,
decode(OT_BILLRATE, NULL, 0, OT_BILLRATE) AS OT_BILLRATE,
decode(OT_FILL, NULL, 0, OT_FILL) AS OT_FILL,
decode(MILEAGE_FILL, NULL, 0, MILEAGE_FILL) AS MILEAGE_FILL
FROM nbitra.tmp
pivot
(
max(BillRate) AS BillRate, sum(BillTotal) AS Fill
for WageCode IN ('Regular' Reg , 'Overtime' OT , 'Mileage' Mileage )
);注意:代码用0替换空。
发布于 2019-02-12 21:39:09
我想你只是想要有条件的聚合:
select EmployeeID, AssignmentID, CompanyName,
sum(case when WageCode = 'Regular' then billrate end) as regular_billrate,
sum(case when WageCode = 'Regular' then BillTotal end) as regular_billtotal,
sum(case when WageCode = 'Overtime' then billrate end) as ot_billrate,
sum(case when WageCode = 'Overtime' then BillTotal end) as ot_billtotal,
sum(case when WageCode = 'Mileage' then billrate end) as mileage_billrate,
sum(case when WageCode = 'Mileage' then BillTotal end) as mileage_billtotal
from SampleTable st
group by EmployeeID, AssignmentID, CompanyName;https://stackoverflow.com/questions/54658994
复制相似问题