2015-04-30 96 views
0

我要計算etape數量在一個項目的員工的工作錯誤TSQL集合錯誤

我的表貼面有:

臨時工:

noProjet 
noEtape 
noEmploye 
annee 
mois 
jrsHm 

我的查詢是

select 
    t.noEmploye, t.NoProjet, c.count 
from 
    temps t 
inner join 
    (select 
     noProjet, noEtape, count(*) as count 
    from 
     temps 
    group by 
     noProjet) c on t.noProjet = c.noProjet 
order by 
    noEmploye 

我得到的錯誤是

Msg 8120,Level 16,State 1,Line 1
列'temps.noEtape'在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句中。

回答

1

錯誤是不言自明的。從子查詢中刪除noEtape列。通過使用一個查詢

select t.noEmploye, t.NoProjet, c.count 
from temps t 

inner join (select noProjet, max(noEtape) as noEtape, count(*) as count 
from temps 

group by noProjet) c on t.noProjet = c.noProjet 
order by noEmploye 
+0

它不指望它來算「Etape酒店」的員工工作的好事成謨 – Shipl0ad

0

也許你可以這樣做::

select t.noEmploye, t.NoProjet, c.count 
from temps t 

inner join (select noProjet, count(*) as count 
from temps 

group by noProjet) c on t.noProjet = c.noProjet 
order by noEmploye 

或者:如果列是不是在組列表中,除非你申請一些聚集到該列無法選擇它

select 
    t.noEmploye, 
    t.NoProjet, 
    c.count 
from temps t 
    inner join (select noProjet, noEtape, count(*) as count from temps group by noProjet, noEtape) as c 
     on t.noProjet = c.noProjet 
order by noEmploye 
+0

消息207,級別16,狀態1,行7 無效的列名稱noProjet「。在c.noProjet – Shipl0ad

+0

我修正了查詢。 – veljasije

1

這是你想要的嗎?

select distinct 
    noEmploye, 
    noProjet, 
    sum(noEtape) over (
    partition by noProjet 
) as EtapesdeProjet 
from Temps 
order by noEmploye