2017-02-07 10 views
1

私は3つのクエリを持っています。私は3つのクエリを組み合わせた別のクエリを作成しました。 クエリは、次のとおりです。SQLグループが機能しない(3つのクエリから)

with tbl1 as(
    SELECT coachEID,role,coacheeEID, 
    IIF(count(status)>IIF(a.role='SME',1,2),IIF(a.role='SME',1,2),count(status)) 
    AS total from coaching z 
    JOIN tbl_calendar v 
    ON z.starttime BETWEEN v.datestart and v.dateend 
    JOIN roster a on z.coachEID=a.EID 
    WHERE z.status='completed' 
    AND v.month='1' and v.year='2017' 
    AND a.location='manila' 
GROUP BY coacheid, coacheeeid, status, role), 

tbl2 as(
    SELECT y.role, y.eid, x.eid as coachee, IIF(y.eid=x.sme,1,2) AS target 
    FROM roster y 
    JOIN roster x ON (x.supervisor = y.eid OR x.sme = y.eid OR x.teamlead = y.eid) AND x.eid <> y.eid 
    AND x.employstatus = 'Active' 
    WHERE y.employstatus = 'active' 
    AND y.Role IN ('SME', 'Junior Team Lead','Team Lead', 'Shift Lead', 'Operations Lead', 'Quality Lead', 'Policy Lead') 
    AND y.location = 'manila'), 

tbl3 as(
    SELECT x.coachEID,x.CoacheeEID, sum (x.NoOfApproved) as exempt, x.month, x.year from exemptitems x 
    LEFT JOIN roster y on y.eid=x.Coacheid 
    WHERE x.month='1' and x.year='2017' 
    AND y.eid=x.coacheid and x.NoOfApproved is not NULL 
    GROUP BY x.CoachEID, x.CoacheeEID, x.Month, x.year) 

最終クエリは次のとおりです。

select IIF(tbl2.role is NULL, tbl1.role,tbl2.role) as role, 
     IIF(tbl1.coachEID is NULL,tbl2.eid,tbl1.coachEID) as coacheid, 
     IIF(tbl1.coacheeEID is NULL, tbl2.coachee,tbl1.coacheeEID) as coacheeeid, 
     IIF(total is NULL,0,total) as total, 
     IIF(tbl2.target is NUll,total,tbl2.target) as ttarget, 
     IIF(tbl3.exempt is NULL,0,tbl3.exempt) as texemption, 
     IIF(tbl2.target is NULL,total,tbl2.target)-IIF(tbl3.exempt is NULL,0,tbl3.exempt) as ttotal 
from tbl1 
FULL join tbl2 
    on tbl1.coachEID=tbl2.eid and tbl1.coacheeEID=tbl2.coachee 
left JOIN tbl3 
    on tbl1.coachEID=tbl3.CoachEID 

query result

私の質問、どのような方法私はグループでそれを持っているために使用すべきですか?私は既にグループを試しましたが、うまくいきません。私はそれをロール、コーチェイド、そして彼らの総コーチング(合計コラム)でグループ化したかったのです。最終的なクエリの最後のステートメントでGROUP BYロール、coacheid、coacheeeidを使用しようとするたびに、「曖昧な列名の役割」と「あいまいな列名 'coacheid」が返される "

+4

どうすればいいですか?構文エラー、他のエラー、または予期しない結果がありますか?あなたのGROUP BYの試みを見せてください。 – jarlh

+2

まずは、「合体」の何が問題なのですか?第二に、何でグループ化するのですか?各行はすでに区別されて表示されます... – JohnHC

+0

可能であればサンプルのdbを共有できますか?また、必要な出力について簡単に説明できますか? –

答えて

1

' 'は、列の前に表名を指定するのを忘れたことを意味します。 1つを選択してそこに置きます(GROUP BY)。

0

ありがとうございました。私はすでにそれがどのように行われているかを考え出しました。ここに私の完全なコードです:

with tbl1 as(
    SELECT coachEID,role,coacheeEID, 
    IIF(count(status)>IIF(a.role='SME',1,2), 
    IIF(a.role='SME',1,2),count(status)) 
AS total from coaching z 
JOIN tbl_calendar v 
ON z.starttime BETWEEN v.datestart and v.dateend 
JOIN roster a on z.coachEID=a.EID 
WHERE z.status='completed' 
AND v.month='1' and v.year='2017' 
AND a.location='manila' 
AND a.Role IN ('SME', 'Junior Team Lead','Team Lead', 'Shift Lead', 'Operations Lead', 'Quality Lead', 'Policy Lead') 
GROUP BY coacheid, coacheeeid, status, role), 

tbl2 as(
    SELECT y.role, y.eid, x.eid as coachee, IIF(y.eid=x.sme,1,2) AS target 
    FROM roster y 
JOIN roster x ON (x.supervisor = y.eid OR x.sme = y.eid OR x.teamlead = y.eid) AND x.eid <> y.eid 
AND x.employstatus = 'Active' 
WHERE y.employstatus = 'active' 
AND y.Role IN ('SME', 'Junior Team Lead','Team Lead', 'Shift Lead', 'Operations Lead', 'Quality Lead', 'Policy Lead') 
AND y.location = 'manila'), 

tbl3 as(
    SELECT x.coachEID,x.CoacheeEID, sum (x.NoOfApproved) as exempt, x.month, x.year from exemptitems x 
LEFT JOIN roster y on y.eid=x.Coacheid 
WHERE x.month='1' and x.year='2017' 
AND y.eid=x.coacheid and x.NoOfApproved is not NULL 
GROUP BY x.CoachEID, x.CoacheeEID, x.Month, x.year), 


tbl4 as (
SELECT 
IIF(tbl2.role is NULL, tbl1.role,tbl2.role) as role, 
IIF(tbl1.coachEID is NULL,tbl2.eid,tbl1.coachEID) as coacheid, 
IIF(tbl1.coacheeEID is NULL, tbl2.coachee,tbl1.coacheeEID) as coacheeeid, 
IIF(total is NULL,0,total) as total, 
IIF(tbl2.target is NUll,total,tbl2.target) as ttarget, 
IIF(tbl3.exempt is NULL,0,tbl3.exempt) as texemption, 
IIF(tbl2.target is NULL,total,tbl2.target)-IIF(tbl3.exempt is NULL,0,tbl3.exempt) as ttotal 
from tbl1 
FULL JOIN tbl2 on tbl1.coachEID=tbl2.eid and tbl1.coacheeEID=tbl2.coachee 
LEFT JOIN tbl3 on tbl1.coachEID=tbl3.CoachEID 
) 

SELECT role, coacheid, SUM (total) as totalcoach, SUM (ttarget) as target, SUM (texemption) exemption, SUM (ttotal) total_target from tbl4 
GROUP BY role, coacheid 
ORDER BY coacheid asc 

here's the result

は、私が何かを逃した場合は追加すること自由に感じなさい、私にとって、それはかなり正常に動作しているようです。

関連する問題