2016-07-29 59 views
2

我有一個查詢返回付款代碼和任務不匹配的交易。但是,有幾個薪酬代碼/任務組合可以被忽略,例如「薪資」/「薪資調整」。如何用一個除非寫一個SQL查詢過濾器?

是否有寫的查詢是這樣一種方式:

select user, trxdate, paycode, task from TABLE 
where paycode <> task 
--unless when paycode = "Salary" the task = "SalaryReg" 

任何建議,將不勝感激!謝謝!

+1

不,提問者希望從查詢中排除這些行。 –

回答

0
select user, trxdate, paycode, task from TABLE 
where paycode <> task 
and not 
(
    (paycode = "Salary" AND task = "SalaryReg") 
OR 
    (paycode = "Janitorial" AND task = "Cleaning") 
OR 
    (paycode = "FrontDesk" AND task = "GreetingCustomers") 
) 
1

查詢下面將選擇從表中的所有記錄,其中paycode <> task,除了當paycodeSalarytaskSalaryReg

SELECT user, 
     trxdate, 
     paycode, 
     task 
FROM yourTable 
WHERE paycode <> task AND 
    (paycode <> "Salary" OR task <> "SalaryReg") -- this condition only kicks in when 
                -- paycode = "Salary" AND 
                -- task = "SalaryReg"