I'm updating queries that were using the old construction for outer joins (=* and *=). I have 3 tables that I need to include in an outer join.
The original query is:
SELECT s.SkillID ,
NULL AS Signature ,
NULL AS DPL ,
CASE WHEN ISNULL(ds.DPL, dg.DPL) IS NULL
THEN p.ScaleTo - p.ScaleFrom + 1
ELSE ISNULL(ds.DPL, dg.DPL)
END AS DefaultDPL
FROM tbJobs j ,
tbSkills s
INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,
tbPerfScales p ,
tbDPLs ds ,
tbDPLs dg
WHERE j.JobID = 866
AND ( ds.LevelID=*j.LevelID
AND ds.IDType = 1
AND ds.GroupOrSkillID=*s.SkillID
)
AND ( dg.LevelID=*j.LevelID
AND dg.IDType = 0
AND dg.GroupOrSkillID=*sg.SkillGroupID
)
AND ( ( s.PerfScaleID IS NOT NULL
AND p.PerfScaleID = s.PerfScaleID
)
OR ( s.PerfScaleID IS NULL
AND p.PerfScaleID = sg.PerfScaleID
)
)
I'm doing:
SELECT s.SkillID ,
NULL AS Signature ,
NULL AS DPL ,
CASE WHEN ISNULL(ds.DPL, dg.DPL) IS NULL
THEN p.ScaleTo - p.ScaleFrom + 1
ELSE ISNULL(ds.DPL, dg.DPL)
END AS DefaultDPL
FROM tbPerfScales p ,
tbSkills s
INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,
tbJobs j
LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
AND s.SkillID = ds.GroupOrSkillID
LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
AND sg.SkillGroupID = dg.GroupOrSkillID
WHERE j.JobID = 866
AND ds.IDType = 1
AND dg.IDType = 0
AND ( ( s.PerfScaleID IS NOT NULL
AND p.PerfScaleID = s.PerfScaleID
)
OR ( s.PerfScaleID IS NULL
AND p.PerfScaleID = sg.PerfScaleID
)
)
For some reason I'm getting the error:
The multi-part identifier "s.SkillID" could not be bound.
And I know is in this part:
tbJobs j
LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
AND s.SkillID = ds.GroupOrSkillID
I'm not sure what I'm doing wrong.
Thanks any help.
Jose
There is a comma after the statement
INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,
Which is because you have tbJobs after it, when it should be in the other tables. I would recommend using CROSS JOIN instead of just having multiple tables in the FROM clause as it will be more clear.
Here is the base rewrite of your query that should work, though you should be able to easily get rid of most of the cross joins.
SELECT s.SkillID ,
NULL AS Signature ,
NULL AS DPL ,
CASE WHEN ISNULL(ds.DPL, dg.DPL) IS NULL
THEN p.ScaleTo - p.ScaleFrom + 1
ELSE ISNULL(ds.DPL, dg.DPL)
END AS DefaultDPL
FROM tbPerfScales p
CROSS JOIN tbSkills s
CROSS JOIN tbJobs j
INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID
LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
AND s.SkillID = ds.GroupOrSkillID
LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
AND sg.SkillGroupID = dg.GroupOrSkillID
WHERE j.JobID = 866
AND ds.IDType = 1
AND dg.IDType = 0
AND ( ( s.PerfScaleID IS NOT NULL
AND p.PerfScaleID = s.PerfScaleID
)
OR ( s.PerfScaleID IS NULL
AND p.PerfScaleID = sg.PerfScaleID
)
)
You cannot mix the different SQL syntax. If you are going with ANSI-92 you need to correct all the joins, not just the outer joins. Consistency is key.
The comma is the incorrect syntax now. You need to add a join for each table, or pull them out into cross applies per Darren's answer.
This is the issue:
FROM tbPerfScales p ,
tbSkills s
INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID ,
tbJobs j
LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
AND s.SkillID = ds.GroupOrSkillID
LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
AND sg.SkillGroupID = dg.GroupOrSkillID
it needs to be along the lines of:
FROM tbPerfScales p
INNER JOIN tbSkills s ON p.<field> = s.<field>
INNER JOIN tbSkillGroups sg ON s.SkillGroupID = sg.SkillGroupID
INNER JOIN tbJobs j on j.<field> = <table p? sg? not sure>.<field>
LEFT OUTER JOIN tbDPLs ds ON j.LevelID = ds.LevelID
AND s.SkillID = ds.GroupOrSkillID
LEFT OUTER JOIN tbDPLs dg ON j.LevelID = dg.LevelID
AND sg.SkillGroupID = dg.GroupOrSkillID
Related
error: ORA-00920: invalid relational operator
SELECT DISTINCT B.MILESTONE_NAME, D.TASK_NAME, F.FIELD_NAME, F.FIELD_ALIAS, G.VALUE
FROM TBL_APPLICATIONS tblapp
INNER JOIN TBL_WORKFLOWS tblwflow ON tblwflow.APPLICATION_FK = tblapp.APPLICATION_PK
INNER JOIN TBL_WORKFLOW_DEFINITION A ON A.WORKFLOW_FK = tblwflow.WORKFLOW_PK
INNER JOIN TBL_MILESTONE B ON B.MILESTONE_PK = A.START_MILESTONE_FK OR B.MILESTONE_PK = A.END_MILESTONE_FK
INNER JOIN TBL_TASK_FOR_MILESTONE C ON C.MILESTONE_FK = B.MILESTONE_PK
INNER JOIN TBL_TASK D ON D.TASK_PK = C.TASK_FK
INNER JOIN TBL_REQ_FOR_TASK E ON E.TASK_FK = D.TASK_PK
INNER JOIN TBL_TASK_REQUIREMENTS F ON F.TASK_REQUIREMENT_PK = E.TASK_REQUIREMENT_FK
INNER JOIN TBL_PROJECT_REQ_VALUES G ON G.TASK_REQUIREMENT_FK = F.TASK_REQUIREMENT_PK
INNER JOIN TBL_PROJECTS H ON H.PROJECT_PK = G.PROJECTS_FK
WHERE PROJECT_PK = :PROJECT_ID
OR B.MILESTONE_NAME
OR D.TASK_NAME
OR F.FIELD_NAME
OR F.FIELD_ALIAS
OR G.VALUE IS NULL
GROUP BY B.MILESTONE_NAME, D.TASK_NAME, F.FIELD_NAME, F.FIELD_ALIAS, G.VALUE
This is not valid Oracle SQL:
WHERE
PROJECT_PK = :PROJECT_ID
OR B.MILESTONE_NAME
OR D.TASK_NAME
OR F.FIELD_NAME
OR F.FIELD_ALIAS
OR G.VALUE IS NULL
You would need to repeat the IS NULL condition:
WHERE
PROJECT_PK = :PROJECT_ID
OR B.MILESTONE_NAME IS NULL
OR D.TASK_NAME IS NULL
OR F.FIELD_NAME IS NULL
OR F.FIELD_ALIAS IS NULL
OR G.VALUE IS NULL
Side note: the following part of your query:
INNER JOIN TBL_MILESTONE B
ON B.MILESTONE_PK = A.START_MILESTONE_FK OR B.MILESTONE_PK = A.END_MILESTONE_FK
Can be shorten with the IN operator:
INNER JOIN TBL_MILESTONE B
ON B.MILESTONE_PK IN (A.START_MILESTONE_FK, A.END_MILESTONE_FK)
I have View called VW_STOCKOPNAME
CREATE OR REPLACE VIEW VW_STOCKOPNAME AS SELECT
A.M_STOCKCODE_KL_ID,
A.M_STOCKCODE_JB_ID,
A.M_STOCKCODE_NB_ID,
A.M_STOCKCODE_SB_ID,
B."NAME" AS S1_KL,
C."NAME" AS S2_JB,
D."NAME" AS S3_NB,
E."NAME" AS S4_SB,
A.M_ASSETCODE_GOLONGAN_ID,
A.M_ASSETCODE_BIDANG_ID,
A.M_ASSETCODE_KELOMPOK_ID,
A.M_ASSETCODE_SUBKELOMPOK_ID,
A.M_ASSETCODE_SUBSUBKEL_ID,
F."NAME" AS A1_GOLONGAN,
G."NAME" AS A2_BIDANG,
H."NAME" AS A3_KELOMPOK,
I."NAME" AS A4_SUBKELOMPOK,
J."NAME" AS A5_SUBSUBKEL,
A.M_PRODUCT_ID,
K."NAME" AS PRODUCT,
L."NAME" AS UOM,
A.QTYBOOK,
A.QTYCOUNT,
A.DIFFERENCE,
N.M_WAREHOUSE_ID,
O."NAME" AS WAREHOUSE
FROM M_INVENTORYLINE A
LEFT JOIN M_STOCKCODE_KL B ON B.M_STOCKCODE_KL_ID = A.M_STOCKCODE_KL_ID
LEFT JOIN M_STOCKCODE_JB C ON C.M_STOCKCODE_JB_ID = A.M_STOCKCODE_JB_ID
LEFT JOIN M_STOCKCODE_NB D ON D.M_STOCKCODE_NB_ID = A.M_STOCKCODE_NB_ID
LEFT JOIN M_STOCKCODE_SB E ON E.M_STOCKCODE_SB_ID = A.M_STOCKCODE_SB_ID
LEFT JOIN M_ASSETCODE_GOLONGAN F ON F.M_ASSETCODE_GOLONGAN_ID = A.M_ASSETCODE_GOLONGAN_ID
LEFT JOIN M_ASSETCODE_BIDANG G ON G.M_ASSETCODE_BIDANG_ID = A.M_ASSETCODE_BIDANG_ID
LEFT JOIN M_ASSETCODE_KELOMPOK H ON H.M_ASSETCODE_KELOMPOK_ID = A.M_ASSETCODE_KELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBKELOMPOK I ON I.M_ASSETCODE_SUBKELOMPOK_ID = A.M_ASSETCODE_SUBKELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBSUBKEL J ON J.M_ASSETCODE_SUBSUBKEL_ID = A.M_ASSETCODE_SUBSUBKEL_ID
LEFT JOIN M_PRODUCT K ON K.M_PRODUCT_ID = A.M_PRODUCT_ID
LEFT JOIN C_UOM L ON L.C_UOM_ID = A.C_UOM_ID
LEFT JOIN M_INVENTORY N ON N.M_INVENTORY_ID = A.M_INVENTORY_ID
LEFT JOIN M_WAREHOUSE O ON O.M_WAREHOUSE_ID = N.M_WAREHOUSE_ID
;
I want to add another column which has command as follows :
SELECT S1_KL ||'-'|| S2_JB ||'-'|| S3_NB ||'-'|| S4_SB AS ASSET_CODE
FROM VW_STOCKOPNAME
It shows group of codes from the columns in the view to make it easier to see.
How can I add it?
Try this
CREATE OR REPLACE VIEW VW_STOCKOPNAME AS SELECT
A.M_STOCKCODE_KL_ID,
A.M_STOCKCODE_JB_ID,
A.M_STOCKCODE_NB_ID,
A.M_STOCKCODE_SB_ID,
B."NAME" AS S1_KL,
C."NAME" AS S2_JB,
D."NAME" AS S3_NB,
E."NAME" AS S4_SB,
A.M_ASSETCODE_GOLONGAN_ID,
A.M_ASSETCODE_BIDANG_ID,
A.M_ASSETCODE_KELOMPOK_ID,
A.M_ASSETCODE_SUBKELOMPOK_ID,
A.M_ASSETCODE_SUBSUBKEL_ID,
F."NAME" AS A1_GOLONGAN,
G."NAME" AS A2_BIDANG,
H."NAME" AS A3_KELOMPOK,
I."NAME" AS A4_SUBKELOMPOK,
J."NAME" AS A5_SUBSUBKEL,
A.M_PRODUCT_ID,
K."NAME" AS PRODUCT,
L."NAME" AS UOM,
A.QTYBOOK,
A.QTYCOUNT,
A.DIFFERENCE,
N.M_WAREHOUSE_ID,
O."NAME" AS WAREHOUSE,
B."NAME" ||'-'|| C."NAME" ||'-'|| D."NAME" ||'-'|| E."NAME" AS ASSET_CODE
FROM M_INVENTORYLINE A
LEFT JOIN M_STOCKCODE_KL B ON B.M_STOCKCODE_KL_ID = A.M_STOCKCODE_KL_ID
LEFT JOIN M_STOCKCODE_JB C ON C.M_STOCKCODE_JB_ID = A.M_STOCKCODE_JB_ID
LEFT JOIN M_STOCKCODE_NB D ON D.M_STOCKCODE_NB_ID = A.M_STOCKCODE_NB_ID
LEFT JOIN M_STOCKCODE_SB E ON E.M_STOCKCODE_SB_ID = A.M_STOCKCODE_SB_ID
LEFT JOIN M_ASSETCODE_GOLONGAN F ON F.M_ASSETCODE_GOLONGAN_ID = A.M_ASSETCODE_GOLONGAN_ID
LEFT JOIN M_ASSETCODE_BIDANG G ON G.M_ASSETCODE_BIDANG_ID = A.M_ASSETCODE_BIDANG_ID
LEFT JOIN M_ASSETCODE_KELOMPOK H ON H.M_ASSETCODE_KELOMPOK_ID = A.M_ASSETCODE_KELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBKELOMPOK I ON I.M_ASSETCODE_SUBKELOMPOK_ID = A.M_ASSETCODE_SUBKELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBSUBKEL J ON J.M_ASSETCODE_SUBSUBKEL_ID = A.M_ASSETCODE_SUBSUBKEL_ID
LEFT JOIN M_PRODUCT K ON K.M_PRODUCT_ID = A.M_PRODUCT_ID
LEFT JOIN C_UOM L ON L.C_UOM_ID = A.C_UOM_ID
LEFT JOIN M_INVENTORY N ON N.M_INVENTORY_ID = A.M_INVENTORY_ID
LEFT JOIN M_WAREHOUSE O ON O.M_WAREHOUSE_ID = N.M_WAREHOUSE_ID
;
SELECT A.SspStudent,
B.StuLastName,
B.StuFirstName,
C.TerDescription,
A.SspCourseSubject,
A.SspCourseID,
A.SspCreditHours,
A.SspStatus,
B.StuEmailUNCW,
B.StuAdvisorEmail,
B.StuApplicationStatus,
D.UseName
FROM ((tblStudentSchedulePerm A
INNER JOIN tblStudent B
ON (A.SspStudent = B.StuID AND
B.StuAdmitTerm <> '201520A' AND
A.SspStatus = 'Schedul' AND
(A.SspTerm = '201520A' OR A.SspTerm = '201520B') AND
A.SspCreditHours <> 0))
LEFT OUTER JOIN tblTerms C
ON A.SspTerm = C.TerTermPart)
LEFT OUTER JOIN D tblUser
ON B.StuAdvisorEmail = D.UseEmail;
I am trying to make this query work but everytime I try to adjust the parenthesis I get an error.
Could I get some help??
you are better off, with out those parenthesis, also you need to a sub query in the joins
Retained the parenthesis except in the OR AND condition , try this out.
SELECT A.SspStudent,
B.StuLastName,
B.StuFirstName,
C.TerDescription,
A.SspCourseSubject,
A.SspCourseID,
A.SspCreditHours,
A.SspStatus,
B.StuEmailUNCW,
B.StuAdvisorEmail,
B.StuApplicationStatus,
D.UseName
FROM tblStudentSchedulePerm A
INNER JOIN tblStudent B
ON A.SspStudent = B.StuID
AND B.StuAdmitTerm <> '201520A'
AND A.SspStatus = 'Schedul'
AND ( A.SspTerm = '201520A'
OR A.SspTerm = '201520B' )
AND A.SspCreditHours <> 0
LEFT OUTER JOIN tblTerms C
ON A.SspTerm = C.TerTermPart
LEFT OUTER JOIN tblUser D
ON B.StuAdvisorEmail = D.UseEmail;
I indented your query. This is how I usually start to pick apart something with which I'm unfamilar.
SELECT
A.SspStudent
, B.StuLastName
, B.StuFirstName
, C.TerDescription
, A.SspCourseSubject
, A.SspCourseID
, A.SspCreditHours
, A.SspStatus
, B.StuEmailUNCW
, B.StuAdvisorEmail
, B.StuApplicationStatus
, D.UseName
FROM
(
(
tblStudentSchedulePerm A
INNER JOIN tblStudent B ON
(
A.SspStudent = B.StuID
AND B.StuAdmitTerm <> '201520A'
AND A.SspStatus = 'Schedul'
AND (A.SspTerm = '201520A' OR A.SspTerm = '201520B')
AND A.SspCreditHours <> 0
)
)
LEFT OUTER JOIN tblTerms C ON A.SspTerm = C.TerTermPart
)
LEFT OUTER JOIN D tblUser ON B.StuAdvisorEmail = D.UseEmail
;
D tblUser
should be :
tblUser D
Other than that, remove all useless parentheses:
SELECT A.SspStudent,
B.StuLastName,
B.StuFirstName,
C.TerDescription,
A.SspCourseSubject,
A.SspCourseID,
A.SspCreditHours,
A.SspStatus,
B.StuEmailUNCW,
B.StuAdvisorEmail,
B.StuApplicationStatus,
D.UseName
FROM tblStudentSchedulePerm A
INNER JOIN tblStudent B
ON A.SspStudent = B.StuID
AND B.StuAdmitTerm <> '201520A'
AND A.SspStatus = 'Schedul'
AND ( A.SspTerm = '201520A'
OR A.SspTerm = '201520B' )
AND A.SspCreditHours <> 0
LEFT OUTER JOIN tblTerms C
ON A.SspTerm = C.TerTermPart
LEFT OUTER JOIN tblUser D
ON B.StuAdvisorEmail = D.UseEmail;
I'm getting this error: Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause.
I searched on internet, but i don't know how to apply the correction in my case. I found different types of query only.
I'm trying to get a Count result in a field (line 5), but when I add the Count query i'm getting this error.
I'm using SQL SERVER 2008 R2.
When I remove the Count from SELECT and GROUP BY the query run correctly.
SELECT TF.COD_FORNECEDOR AS 'Cód. Fornec.',
TF.NOM_FANTASIA AS 'Fornecedor',
DM.COD_CONTRATO_RELACIONADO AS 'CONTRATO',
UA2.NOM_USUARIO AS 'NOM_USUARIO_COMPRADOR',
(SELECT COUNT(COD_CONTRATO_RELACIONADO) FROM TB_DEMANDA D INNER JOIN TB_PROCESSO P ON D.COD_CONTRATO_RELACIONADO = P.NUM_CONTRATO WHERE D.COD_CONTRATO_RELACIONADO = DM.COD_CONTRATO_RELACIONADO) AS 'NUM_ADITIVO',
0 AS 'Qtd. Aditivo',
SUM(DM.VAL_TOTAL_ORCADO) AS 'Valor Inicial',
SUM(TP.VAL_CONTRATADO) AS 'Valor Final',
((SUM(TP.VAL_CONTRATADO) / SUM(DM.VAL_TOTAL_ORCADO)) -1) * 100 AS 'Var. %'
FROM TB_FORNECEDOR TF
LEFT JOIN TB_DEMANDA DM ON DM.COD_FORNECEDOR = TF.COD_FORNECEDOR
LEFT JOIN TB_PROCESSO TP ON DM.COD_DEMANDA = TP.COD_DEMANDA
LEFT JOIN TB_PROCESSO_FORNECEDOR PF ON PF.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN USUARIO UA ON UA.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN USUARIO UA2 ON UA2.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN TB_STATUS_DEMANDA_DATA SD ON SD.COD_DEMANDA = DM.COD_DEMANDA
LEFT JOIN TB_BASELINE BA ON BA.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN TB_BASELINE_TAREFAS BT ON BT.COD_BASELINE = BA.COD_BASELINE AND BT.COD_PROCESSO = BA.COD_PROCESSO
LEFT JOIN TB_STATUS_PROCESSO SP ON SP.COD_STATUS = SD.COD_STATUS
LEFT JOIN TB_MEDIO_STATUS MS ON MS.COD_MEDIO_STATUS = SP.COD_MEDIO_STATUS
left JOIN #TB_PROCESSO TT ON TT.COD_PROCESSO = TP.COD_PROCESSO
GROUP BY
TF.COD_FORNECEDOR,
TF.NOM_FANTASIA,
DM.COD_CONTRATO_RELACIONADO,
UA2.NOM_USUARIO,
(SELECT COUNT(COD_CONTRATO_RELACIONADO) FROM TB_DEMANDA D INNER JOIN TB_PROCESSO P ON D.COD_CONTRATO_RELACIONADO = P.NUM_CONTRATO WHERE D.COD_CONTRATO_RELACIONADO = DM.COD_CONTRATO_RELACIONADO)
ORDER BY TF.NOM_FANTASIA
Try this:
SELECT TF.COD_FORNECEDOR AS 'Cód. Fornec.',
TF.NOM_FANTASIA AS 'Fornecedor',
DM.COD_CONTRATO_RELACIONADO AS 'CONTRATO',
UA2.NOM_USUARIO AS 'NOM_USUARIO_COMPRADOR',
sq.cnt AS 'NUM_ADITIVO',
0 AS 'Qtd. Aditivo',
SUM(DM.VAL_TOTAL_ORCADO) AS 'Valor Inicial',
SUM(TP.VAL_CONTRATADO) AS 'Valor Final',
((SUM(TP.VAL_CONTRATADO) / SUM(DM.VAL_TOTAL_ORCADO)) -1) * 100 AS 'Var. %'
FROM TB_FORNECEDOR TF
LEFT JOIN TB_DEMANDA DM ON DM.COD_FORNECEDOR = TF.COD_FORNECEDOR
LEFT JOIN TB_PROCESSO TP ON DM.COD_DEMANDA = TP.COD_DEMANDA
LEFT JOIN TB_PROCESSO_FORNECEDOR PF ON PF.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN USUARIO UA ON UA.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN USUARIO UA2 ON UA2.NUM_USUARIO = DM.NUM_USUARIO_COMPRADOR
LEFT JOIN TB_STATUS_DEMANDA_DATA SD ON SD.COD_DEMANDA = DM.COD_DEMANDA
LEFT JOIN TB_BASELINE BA ON BA.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN TB_BASELINE_TAREFAS BT ON BT.COD_BASELINE = BA.COD_BASELINE AND BT.COD_PROCESSO = BA.COD_PROCESSO
LEFT JOIN TB_STATUS_PROCESSO SP ON SP.COD_STATUS = SD.COD_STATUS
LEFT JOIN TB_MEDIO_STATUS MS ON MS.COD_MEDIO_STATUS = SP.COD_MEDIO_STATUS
LEFT JOIN #TB_PROCESSO TT ON TT.COD_PROCESSO = TP.COD_PROCESSO
LEFT JOIN (
SELECT D.COD_CONTRATO_RELACIONADO, COUNT(COD_CONTRATO_RELACIONADO) AS cnt
FROM TB_DEMANDA D
INNER JOIN TB_PROCESSO P ON D.COD_CONTRATO_RELACIONADO = P.NUM_CONTRATO
GROUP BY D.COD_CONTRATO_RELACIONADO
) sq ON sq.COD_CONTRATO_RELACIONADO = DM.COD_CONTRATO_RELACIONADO
GROUP BY
TF.COD_FORNECEDOR,
TF.NOM_FANTASIA,
DM.COD_CONTRATO_RELACIONADO,
UA2.NOM_USUARIO,
sq.cnt
ORDER BY TF.NOM_FANTASIA
I integrated the num_aditivo subquery in a LEFT JOIN.
I have this query and I am trying to group by surveyname but im getting this error:
Msg 8120, Level 16, State 1, Line 1
Column 'pvt.Follow Up' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
This is the query:
SELECT
surveyname, [Follow Up] AS Follow_Up, [Ambiance] AS Ambiance,
[Consultation] AS Consultation, [Procedure/Service] AS Procedure_Service
FROM
(SELECT
s.name surveyname, q.question, subq.answer subquestion,aw.answerweight,
aw.score, rc.categoryname, sc.cweight
FROM survey.dbo.results r
JOIN survey.dbo.questions q ON r.questionidfk = q.id
LEFT JOIN survey.dbo.answers subq ON r.itemidfk = subq.id
LEFT JOIN survey.dbo.answers a ON r.answeridfk = a.id
JOIN survey.dbo.surveys s ON q.surveyidfk = s.id
join sigweb.dbo.survey_types_main stm on s.id = stm.surveyidfk
join survey.dbo.survey_results sr on r.owneridfk = sr.ownerid
join sigweb.dbo.BosleySurvey bs on bs.contactid = sr.contactid and stm.clientsurveytypeid = bs.surveytype
join sigweb.dbo.contact c on sr.contactid = c.contactid
join sigweb.dbo.patient p on p.contactid = c.contactid
join sigweb.dbo.doctor d on p.doctorid = d.doctorid
join sigweb.dbo.survey_tracking st on st.contactid = c.contactID and st.surveytypeid = stm.surveytypeid
left join survey.dbo.answerweighting aw on isnull(r.itemidfk, r.questionidfk) = aw.questionitemidfk and r.answeridfk = aw.answeridfk
left join survey.dbo.rating_categories rc on aw.categoryidfk = rc.id
left join survey.dbo.survey_categories sc on aw.categoryidfk = sc.categoryidfk and s.id = sc.surveyidfk
where
aw.answerWeight is not null) ps
PIVOT
(
AVG(score)
FOR categoryname IN
( [Follow Up], [Ambiance], [Consultation], [Procedure/Service])
) AS pvt
group by surveyname
This is an example of the results im getting
SURVEYNAME FOLLOW_UP Ambiance Consultation Procedure_Service
Review NULL NULL NULL 9.81
Review 9.54 NULL NULL NULL
Consultation 5 NULL NULL NULL
Consultation NULL 5 NULL NULL
Consultation NULL 5 NULL NULL
Consultation NULL 5 NULL NULL
Consultation NULL 5 NULL NULL
Consultation NULL 5 NULL NULL
Consultation NULL NULL 5 NULL
Consultation 5 NULL NULL NULL
Consultation NULL NULL 5 NULL
This is an example of the data before the pivot:
Review 6 Follow Up
Review 9 Procedure/Service
Consultation 5 Ambiance
Consultation 5 Ambiance
Consultation 5 Ambiance
Consultation 5 Ambiance
Consultation 5 Ambiance
Consultation 5 Ambiance
Consultation 5 Consultation
Consultation 5 Consultation
The idea is to group by the surveyname and have only two results in the end.
It appears that you are including too many columns in the inner SELECT, try to remove the columns:
q.question, subq.answer subquestion, aw.answerweight, sc.cweight
They are most likely making the rows DISTINCT so the GROUP BY does not work properly. So your query will be:
SELECT surveyname,
[Follow Up] AS Follow_Up,
[Ambiance] AS Ambiance,
[Consultation] AS Consultation,
[Procedure/Service] AS Procedure_Service
FROM
(
SELECT s.name surveyname,
aw.score,
rc.categoryname,
FROM survey.dbo.results r
JOIN survey.dbo.questions q
ON r.questionidfk = q.id
LEFT JOIN survey.dbo.answers subq
ON r.itemidfk = subq.id
LEFT JOIN survey.dbo.answers a
ON r.answeridfk = a.id
JOIN survey.dbo.surveys s
ON q.surveyidfk = s.id
join sigweb.dbo.survey_types_main stm
on s.id = stm.surveyidfk
join survey.dbo.survey_results sr
on r.owneridfk = sr.ownerid
join sigweb.dbo.BosleySurvey bs
on bs.contactid = sr.contactid
and stm.clientsurveytypeid = bs.surveytype
join sigweb.dbo.contact c
on sr.contactid = c.contactid
join sigweb.dbo.patient p
on p.contactid = c.contactid
join sigweb.dbo.doctor d on p.doctorid = d.doctorid
join sigweb.dbo.survey_tracking st
on st.contactid = c.contactID
and st.surveytypeid = stm.surveytypeid
left join survey.dbo.answerweighting aw
on isnull(r.itemidfk, r.questionidfk) = aw.questionitemidfk
and r.answeridfk = aw.answeridfk
left join survey.dbo.rating_categories rc
on aw.categoryidfk = rc.id
left join survey.dbo.survey_categories sc
on aw.categoryidfk = sc.categoryidfk and s.id = sc.surveyidfk
where aw.answerWeight is not null
) ps
PIVOT
(
AVG(score)
FOR categoryname IN
( [Follow Up], [Ambiance], [Consultation], [Procedure/Service])
) AS pvt
I am not sure where the error is coming from in what you have posted (I assume it is when you try and add GROUP BY SurveyName to the end of the query you have posted), but you need to remove the redundant columns from your subquery, so you only select the 3 columns you need, surveyname, score, and categoryname:
SELECT
surveyname, [Follow Up] AS Follow_Up, [Ambiance] AS Ambiance,
[Consultation] AS Consultation, [Procedure/Service] AS Procedure_Service
FROM
(SELECT
s.name surveyname, aw.score, rc.categoryname
FROM survey.dbo.results r
JOIN survey.dbo.questions q ON r.questionidfk = q.id
LEFT JOIN survey.dbo.answers subq ON r.itemidfk = subq.id
LEFT JOIN survey.dbo.answers a ON r.answeridfk = a.id
JOIN survey.dbo.surveys s ON q.surveyidfk = s.id
join sigweb.dbo.survey_types_main stm on s.id = stm.surveyidfk
join survey.dbo.survey_results sr on r.owneridfk = sr.ownerid
join sigweb.dbo.BosleySurvey bs on bs.contactid = sr.contactid and stm.clientsurveytypeid = bs.surveytype
join sigweb.dbo.contact c on sr.contactid = c.contactid
join sigweb.dbo.patient p on p.contactid = c.contactid
join sigweb.dbo.doctor d on p.doctorid = d.doctorid
join sigweb.dbo.survey_tracking st on st.contactid = c.contactID and st.surveytypeid = stm.surveytypeid
left join survey.dbo.answerweighting aw on isnull(r.itemidfk, r.questionidfk) = aw.questionitemidfk and r.answeridfk = aw.answeridfk
left join survey.dbo.rating_categories rc on aw.categoryidfk = rc.id
left join survey.dbo.survey_categories sc on aw.categoryidfk = sc.categoryidfk and s.id = sc.surveyidfk
where
aw.answerWeight is not null) ps
PIVOT
(
AVG(score)
FOR categoryname IN
( [Follow Up], [Ambiance], [Consultation], [Procedure/Service])
) AS pvt
In the background you are also grouping your end results by q.question, subq.answer subquestion,aw.answerweight, sc.cweight because they are included in the subquery, but because the are not in the select list you are not seeing immediately the effect this is having.