2015-09-17 56 views
-4

注意(DEPTID = 2,3)或(DEPTID = 1,總和(總擁有成本)= 0)集團通過並具有OR條件在SQL Server

select * 
into employee1 
from employee2 
where empid in (select distinct empid 
       from employee3 e3 
       join employee2 e2 on e3.empid = e2.empid 
       where e3.deptid in (2,3) 
        or (e3.deptid = 1 
       group by empid, cost 
       having sum(e2.cost) = 0)) 
+0

什麼是你的問題? –

+0

查詢有幾個錯誤..'(e3.deptid = 1'沒有緊密的圓括號,並且由xxxx'組沒有字段。 –

+0

實際上我想實現像下面這樣的條件..(deptid = 2,3)或者(deptid = 1且總和(總成本)= 0).....................成本欄屬於employee3並且deptid屬於employee2 –

回答

0

我會給你一個很大的猜測。這些都是我的問題。

  1. 該查詢可能返回多個結果所以不能使用INTO employee1
  2. employee3看起來應該是depatment

我創建了一個CTE先計算每個員工的成本。然後驗證部門。

WITH ecost as (
    select e.empid, SUM(e.cost) as cost 
    from employee2 e 
    group by e.empid 
) 
select e.empid 
from ecost e 
inner join employee3 d 
     on e.empid = d.empid   
where 
    d.deptid in (2,3) 
or (d.deptid = 1 and e.cost = 0)