我必须合并两个SQL查询才能在一个地方获取值。我想显示年初至今和周日显示一张表中具有相同猫的数量和单位
SQL查询
select
`EmpsID`, `CAT`, `CHK_DATE`,
SUM(AMOUNT) as CurrentAmount,
SUM(UNITS) as CurrentUnits
from
`pays`
where
`EmpsID` = 'SEMLAD01' and
`CAT` in ('Salary Pay', 'TRUCK ALLOWANCE', 'Expense Reimbursement', 'BONUS (Accrued)', 'Phone Reimbursement') and
date(`CHK_DATE`) = '2016-11-12'
group by
`CAT`
第二次查询:
select
`EmpsID`, `CAT`, `CHK_DATE`,
SUM(AMOUNT) as ytdAmount,
SUM(UNITS) as ytdUnits
from
`pays`
where
`EmpsID` = 'SEMLAD01' and
`CAT` in ('Salary Pay', 'TRUCK ALLOWANCE', 'Expense Reimbursement', 'BONUS (Accrued)', 'Phone Reimbursement') and
date(`CHK_DATE`) <= '2016-11-12'
group by
`CAT`
表格结构
EmpsID CAT YTDAmount YTDUnits currentAmount currentunit
SEMLAD01 BONUS 3000.00 0.00 0.00 0.00
SEMLAD01 Expense 1307.90 0.00 50.00
SEMLAD01 Phone Reimbursement 272.00 50.00 1840.00 0.00
SEMLAD01 Salary Pay 60153.74 1840.00 0.00 0.00
SEMLAD01 TRUCK ALLOWANCE 4246.26 0.00
答案 0 :(得分:0)
select `EmpsID`, `CAT`, `CHK_DATE`, SUM(AMOUNT) as CurrentAmount,SUM(UNITS) as CurrentUnits
,sum(case when date(`CHK_DATE`) = '2016-11-12' then AMOUNT else 0 end) as '2016-11-12Amount' ,
,sum(case when date(`CHK_DATE`) = '2016-11-12' then UNITS else 0 end) as '2016-11-12Units' ,
from `pays` where `EmpsID` = 'SEMLAD01' and
`CAT` in ('Salary Pay', 'TRUCK ALLOWANCE', 'Expense Reimbursement',
'BONUS (Accrued)', 'Phone Reimbursement')and date(`CHK_DATE`) <= '2016-11-12'
group by `CAT`