我有三个查询,我已经创建了另一个查询,其中我将三个查询组合在一起。 查询是:
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
我的问题是,我应该使用什么方法将它放入群组中?我已经尝试过分组,但它不起作用。我希望它按角色,coacheid和他们的总教练分组(总栏目)。每当我尝试在最终查询的最终语句中使用GROUP BY角色,coacheid,coacheeeid时,我得到的就是&#34;不明确的列名&#39;角色&#39;&#34;和&#34;不明确的专栏名称&#39; coacheid&#39;&#34;
答案 0 :(得分:1)
&#39;不明确的列名称&#39;意味着,您忘记在列前指定表名。选择一个并将其放在那里(GROUP BY
)。
答案 1 :(得分: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
如果我错过了一些东西,请随意添加,但对我而言似乎工作得很好。