मैं निम्न क्वेरी निष्पादित करने के लिए कोशिश कर रहा हूँ, लेकिन मुझे लगता है कि यह कहते हुए एक रनटाइम त्रुटि प्राप्त में एकल पंक्ति कुल उप क्वेरी के साथ समस्या:एसक्यूएल उन्नत क्वेरी - चयन खंड
"The column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
लाइन नंबर के नीचे दो में से एक है कि उपरोक्त त्रुटि के साथ विफल रहता है। मुझे समझ में नहीं आ रहा है कि यह क्वेरी निष्पादित नहीं होती है क्योंकि लाइन दो में कोई समूहिंग की आवश्यकता नहीं है क्योंकि आउटपुट सिर्फ गिनती है (*), इस काम को पाने के लिए मुझे क्या बदलने की आवश्यकता है, इस बारे में कोई संकेत है?
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
[संपादित करें]
Zeb के समाधान का एक मामूली बदलाव का उपयोग करना - यहाँ संशोधित कोड है कि मैं का उपयोग कर, जो समुच्चय के साथ एक पंक्ति पैदावार है, जो है जो मैं के बाद समाप्त हो गया था है।
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 फ़ील्ड है, यह वास्तव में गिनती टाइप करने जैसा ही है (lpr.lessionplanid) अगर मुझे गलत नहीं लगता है। – James