나는 다음과 같은 쿼리를 실행하기 위해 노력하고있어하지만 난 그것을 알리는 런타임 오류가 발생에서 단일 행 집계 하위 쿼리 문제 :SQL 고급 쿼리 - select 절
는"The column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
행 번호 아래에 두 사람은 하나입니다 위의 오류로 실패합니다. 왜이 쿼리를 실행하지 않는 이유는 이해할 수 없다. 출력이 단지 카운트 (*) 일 뿐이므로, 내가이 작업을하기 위해 바꿀 필요가있는 어떤 단서가 있는가? Hogan 박사의 솔루션에 약간의 변화를 사용
SELECT @lessonPlans = COUNT(*)
, @lessonPlanResources = (SELECT COUNT(*) FROM dbo.LessonPlanResource lpr where lpr.LessonPlanId = a.LessonPlanId)
FROM
(
SELECT DISTINCT lpt.LessonPlanId
FROM dbo.LearningTargetBreakout ltb
JOIN dbo.LessonPlanLearningTarget lpt
on lpt.LearningTargetId = ltb.LearningTargetId
WHERE (CASE
WHEN ltb.LearningTargetId = @learningTargetId and @learningTargetId is not null THEN 1
WHEN ltb.CategoryId = @categoryId and @categoryId is not null THEN 1
WHEN ltb.Grade = @grade and @grade is not null THEN 1
WHEN ltb.SubjectId = @subjectId and @subjectId is not null THEN 1
ELSE 0 END) = 1
) a
[편집]
- 여기 내가 후 무엇 인 집계로 하나 개의 행을 얻을 수있는, 사용 결국 수정 된 코드입니다.
SELECT @lessonPlans = ISNULL(COUNT(*), 0)
, @lessonPlanResources = ISNULL(SUM(a.ResCount), 0)
FROM
(
SELECT DISTINCT lpt.LessonPlanId, lpr.ResCount
FROM dbo.LearningTargetBreakout ltb
JOIN dbo.LessonPlanLearningTarget lpt
on lpt.LearningTargetId = ltb.LearningTargetId
JOIN (SELECT LessonPlanId, COUNT(*) ResCount FROM dbo.LessonPlanResource lpr GROUP BY LessonPlanId) lpr
ON lpr.LessonPlanId = lpt.LessonPlanId
WHERE (CASE
WHEN ltb.LearningTargetId = @learningTargetId and @learningTargetId is not null THEN 1
WHEN ltb.CategoryId = @categoryId and @categoryId is not null THEN 1
WHEN ltb.GradeId = @grade and @grade is not null THEN 1
WHEN ltb.SubjectId = @subjectId and @subjectId is not null THEN 1
ELSE 0 END) = 1
) a
뭐죠에서 내부 쿼리의 출력에서부터 카운트 (*)가 아닌 카운트 (lpr.lessionplanid) – Perpetualcoder
을하고 뒤에 이유 절은 단지 LessonPlanId 필드입니다. 잘못 입력하지 않은 경우 count (lpr.lessionplanid)를 입력하는 것과 실제로 동일합니다. – James