-6
我必須合併兩個SQL查詢才能在一個位置獲取值。我想表明年初至今和週日期顯示量和單位的一個表與貓一樣合併兩個查詢以獲取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
使用UNION運算符。 – APC
請在發佈問題前查看[問]和[mcve]。其他哪些dbms?你到目前爲止嘗試過什麼? MERGE需要什麼?你想從第一個查詢UNION所有的記錄第二個的所有記錄? – Aleksej
我想單位和金額的第一個查詢和第二個查詢單獨的值。 – vallavan