I don't understand why I'm getting the error "Each GROUP BY expression must contain at least one column that is not an outer reference" because I really do not understand what it means. Can someone help me understand its true meaning?
I have a temp which is driving the selection. It is correctly populated. But when I run the report to pull data from the table and other related tables I get the above error on the item that I am doing a SUM. When I comment out this item the report runs correctly. I thought I had done other reports that are nearly identical, but this one is giving me problems.
What am I doing wrong that is causing this error? THANK YOU.
DECLARE @AWDP AS VARCHAR(10);
SET @AWDP = '2016/SP';
DECLARE @TATABLE TABLE -- which has been populated correctly
(
TA_KEY VARCHAR(25),
TA_AMT DECIMAL (10),
TA_TX DECIMAL (10),
TA_AMT_DIFF DECIMAL (10),
TA_ACTION VARCHAR(2),
TA_AWD VARCHAR(10),
TA_STU_ID VARCHAR (7)
)
SELECT DISTINCT
P.ID,
P.LAST_NAME,
P.FIRST_NAME,
@AWDP as 'Term',
AWD.AW_TYPE,
AWD.AW_CATEGORY,
AWD.AW_ID,
AWD.AW_DESCRIPTION,
TTAB.TA_AMT,
TTAB.TA_TX,
TTAB.TA_AMT_DIFF,
TTAB.TA_ACTION,
-- getting the error on the following item
(SELECT SUM(STC.STC_CRED)
FROM STUDENT_ACAD_CRED STC1
JOIN STUDENT_TERMS_LS STL1 ON STL1.STUDENT_TERMS_ID = TTAB.TA_STU_ID + '*' + @AWDP + '*UG'
WHERE STC1.STUDENT_ACAD_CRED_ID = STL1.STTR_SCHEDULE
GROUP BY TTAB.TA_STU_ID) REG_CRED,
SR.SAPR_TRM_REG_CRED,
SR.SAPR_CALC_SAP_STATUS,
SR.SAPR_OVR_SAP_STATUS
FROM @TATABLE TTAB
LEFT JOIN PERSON P ON TTAB.TA_STU_ID = P.ID
LEFT JOIN AWARDS AWD ON TTAB.TA_AWD = AWD.AW_ID
LEFT JOIN FIN_AID_LS FINL ON P.ID = FINL.FA_STUDENT_ID
LEFT JOIN SAP_RESULTS SR ON FINL.FA_SAP_RESULTS_ID = SR.SAP_RESULTS_ID
AND SR.SAPR_CALC_THRU_TERM = @AWDP
LEFT JOIN STUDENT_TERMS_LS STTRL2 ON STTRL2.STUDENT_TERMS_ID = P.ID + '*' + @AWDP + '*UG'
AND STTRL2.STTR_SCHEDULE IS NOT NULL
LEFT JOIN STUDENT_ACAD_CRED STC ON STTRL2.STTR_SCHEDULE = STC.STUDENT_ACAD_CRED_ID
WHERE TTAB.TA_AMT > 0
AND AWD.AW_TYPE IN ('F','O','S','O')
I have a temp which is driving the selection. It is correctly populated. But when I run the report to pull data from the table and other related tables I get the above error on the item that I am doing a SUM. When I comment out this item the report runs correctly. I thought I had done other reports that are nearly identical, but this one is giving me problems.
What am I doing wrong that is causing this error? THANK YOU.
DECLARE @AWDP AS VARCHAR(10);
SET @AWDP = '2016/SP';
DECLARE @TATABLE TABLE -- which has been populated correctly
(
TA_KEY VARCHAR(25),
TA_AMT DECIMAL (10),
TA_TX DECIMAL (10),
TA_AMT_DIFF DECIMAL (10),
TA_ACTION VARCHAR(2),
TA_AWD VARCHAR(10),
TA_STU_ID VARCHAR (7)
)
SELECT DISTINCT
P.ID,
P.LAST_NAME,
P.FIRST_NAME,
@AWDP as 'Term',
AWD.AW_TYPE,
AWD.AW_CATEGORY,
AWD.AW_ID,
AWD.AW_DESCRIPTION,
TTAB.TA_AMT,
TTAB.TA_TX,
TTAB.TA_AMT_DIFF,
TTAB.TA_ACTION,
-- getting the error on the following item
(SELECT SUM(STC.STC_CRED)
FROM STUDENT_ACAD_CRED STC1
JOIN STUDENT_TERMS_LS STL1 ON STL1.STUDENT_TERMS_ID = TTAB.TA_STU_ID + '*' + @AWDP + '*UG'
WHERE STC1.STUDENT_ACAD_CRED_ID = STL1.STTR_SCHEDULE
GROUP BY TTAB.TA_STU_ID) REG_CRED,
SR.SAPR_TRM_REG_CRED,
SR.SAPR_CALC_SAP_STATUS,
SR.SAPR_OVR_SAP_STATUS
FROM @TATABLE TTAB
LEFT JOIN PERSON P ON TTAB.TA_STU_ID = P.ID
LEFT JOIN AWARDS AWD ON TTAB.TA_AWD = AWD.AW_ID
LEFT JOIN FIN_AID_LS FINL ON P.ID = FINL.FA_STUDENT_ID
LEFT JOIN SAP_RESULTS SR ON FINL.FA_SAP_RESULTS_ID = SR.SAP_RESULTS_ID
AND SR.SAPR_CALC_THRU_TERM = @AWDP
LEFT JOIN STUDENT_TERMS_LS STTRL2 ON STTRL2.STUDENT_TERMS_ID = P.ID + '*' + @AWDP + '*UG'
AND STTRL2.STTR_SCHEDULE IS NOT NULL
LEFT JOIN STUDENT_ACAD_CRED STC ON STTRL2.STTR_SCHEDULE = STC.STUDENT_ACAD_CRED_ID
WHERE TTAB.TA_AMT > 0
AND AWD.AW_TYPE IN ('F','O','S','O')