Getting undesired output in SQL









up vote
0
down vote

favorite












I'm new beginner to SQL. I want to list all the codes and names of all subject. In the case of those subjects, for which any of the students registered for the first time, listing the recommended exam date(s) as well.



Order of columns of the result set:



neptuncode, subjectname, examdate


I ran the following query



SELECT 
subjects.neptuncode AS "neptuncode",
subjects.subjectname AS "subjectname",
enrollments.examdate AS "examdate"
FROM
subjects
LEFT OUTER JOIN
enrollments ON enrollments.subject = subjects.subject_ID
GROUP BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate
HAVING
COUNT(enrollments.enrollmentdate) = 1
ORDER BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate;


The result is only subjects which registered by students for the first time alongside neptuncode and examdate. I would like to list also all subjects with their neptuncode and examdate (it should be NULL because only the subjects which are registered for the first time have exam date value in the column). But I am stuck at doing this. Any suggestions please?










share|improve this question



















  • 2




    Sample data and desired results would help. So would an appropriate database tag.
    – Gordon Linoff
    Nov 8 at 21:31










  • If you remove the HAVING clause, does that result look better?
    – jarlh
    Nov 8 at 21:35










  • When removing HAVING clause it gives all the exam dates with all subjects. I need exam dates with the subjects which is registered for the first time by any students
    – Emil Bədrəddinli
    Nov 8 at 21:37














up vote
0
down vote

favorite












I'm new beginner to SQL. I want to list all the codes and names of all subject. In the case of those subjects, for which any of the students registered for the first time, listing the recommended exam date(s) as well.



Order of columns of the result set:



neptuncode, subjectname, examdate


I ran the following query



SELECT 
subjects.neptuncode AS "neptuncode",
subjects.subjectname AS "subjectname",
enrollments.examdate AS "examdate"
FROM
subjects
LEFT OUTER JOIN
enrollments ON enrollments.subject = subjects.subject_ID
GROUP BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate
HAVING
COUNT(enrollments.enrollmentdate) = 1
ORDER BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate;


The result is only subjects which registered by students for the first time alongside neptuncode and examdate. I would like to list also all subjects with their neptuncode and examdate (it should be NULL because only the subjects which are registered for the first time have exam date value in the column). But I am stuck at doing this. Any suggestions please?










share|improve this question



















  • 2




    Sample data and desired results would help. So would an appropriate database tag.
    – Gordon Linoff
    Nov 8 at 21:31










  • If you remove the HAVING clause, does that result look better?
    – jarlh
    Nov 8 at 21:35










  • When removing HAVING clause it gives all the exam dates with all subjects. I need exam dates with the subjects which is registered for the first time by any students
    – Emil Bədrəddinli
    Nov 8 at 21:37












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm new beginner to SQL. I want to list all the codes and names of all subject. In the case of those subjects, for which any of the students registered for the first time, listing the recommended exam date(s) as well.



Order of columns of the result set:



neptuncode, subjectname, examdate


I ran the following query



SELECT 
subjects.neptuncode AS "neptuncode",
subjects.subjectname AS "subjectname",
enrollments.examdate AS "examdate"
FROM
subjects
LEFT OUTER JOIN
enrollments ON enrollments.subject = subjects.subject_ID
GROUP BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate
HAVING
COUNT(enrollments.enrollmentdate) = 1
ORDER BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate;


The result is only subjects which registered by students for the first time alongside neptuncode and examdate. I would like to list also all subjects with their neptuncode and examdate (it should be NULL because only the subjects which are registered for the first time have exam date value in the column). But I am stuck at doing this. Any suggestions please?










share|improve this question















I'm new beginner to SQL. I want to list all the codes and names of all subject. In the case of those subjects, for which any of the students registered for the first time, listing the recommended exam date(s) as well.



Order of columns of the result set:



neptuncode, subjectname, examdate


I ran the following query



SELECT 
subjects.neptuncode AS "neptuncode",
subjects.subjectname AS "subjectname",
enrollments.examdate AS "examdate"
FROM
subjects
LEFT OUTER JOIN
enrollments ON enrollments.subject = subjects.subject_ID
GROUP BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate
HAVING
COUNT(enrollments.enrollmentdate) = 1
ORDER BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate;


The result is only subjects which registered by students for the first time alongside neptuncode and examdate. I would like to list also all subjects with their neptuncode and examdate (it should be NULL because only the subjects which are registered for the first time have exam date value in the column). But I am stuck at doing this. Any suggestions please?







sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 5:28









marc_s

566k12610921245




566k12610921245










asked Nov 8 at 21:28









Emil Bədrəddinli

295




295







  • 2




    Sample data and desired results would help. So would an appropriate database tag.
    – Gordon Linoff
    Nov 8 at 21:31










  • If you remove the HAVING clause, does that result look better?
    – jarlh
    Nov 8 at 21:35










  • When removing HAVING clause it gives all the exam dates with all subjects. I need exam dates with the subjects which is registered for the first time by any students
    – Emil Bədrəddinli
    Nov 8 at 21:37












  • 2




    Sample data and desired results would help. So would an appropriate database tag.
    – Gordon Linoff
    Nov 8 at 21:31










  • If you remove the HAVING clause, does that result look better?
    – jarlh
    Nov 8 at 21:35










  • When removing HAVING clause it gives all the exam dates with all subjects. I need exam dates with the subjects which is registered for the first time by any students
    – Emil Bədrəddinli
    Nov 8 at 21:37







2




2




Sample data and desired results would help. So would an appropriate database tag.
– Gordon Linoff
Nov 8 at 21:31




Sample data and desired results would help. So would an appropriate database tag.
– Gordon Linoff
Nov 8 at 21:31












If you remove the HAVING clause, does that result look better?
– jarlh
Nov 8 at 21:35




If you remove the HAVING clause, does that result look better?
– jarlh
Nov 8 at 21:35












When removing HAVING clause it gives all the exam dates with all subjects. I need exam dates with the subjects which is registered for the first time by any students
– Emil Bədrəddinli
Nov 8 at 21:37




When removing HAVING clause it gives all the exam dates with all subjects. I need exam dates with the subjects which is registered for the first time by any students
– Emil Bədrəddinli
Nov 8 at 21:37












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Would you try this please



SELECT
subjects.neptuncode AS "neptuncode"
, subjects.subjectname AS "subjectname"
, CASE WHEN COUNT(enrollments.enrollmentdate) = 1 THEN MAX(enrollments.enrollmentdate)
ELSE NULL
END AS "examdate"
FROM subjects
LEFT OUTER JOIN enrollments
ON enrollments.subject = subjects.subject_ID
GROUP BY
subjects.neptuncode
, subjects.subjectname
ORDER BY
subjects.neptuncode
, subjects.subjectname
;


I believe you need to treat the enrollmentdate as an aggregate, as only 1 enrollment date is permitted you can use either max() or min().






share|improve this answer






















  • It did not give the all subjects which is in the database
    – Emil Bədrəddinli
    Nov 8 at 22:15










  • Please re-try, I altered the query more than once (sorry). But it will not give ALL subjects - unless the count of enrollments is <= 1. If this query does not work then I suggest that trying to describe (in words) what you expect isn't working. Provide sample data and the expected result (as tables, not as words). Use fixed format text tables, not images, this url may help: ozh.github.io/ascii-tables
    – Used_By_Already
    Nov 8 at 22:52











  • I want to list all subject name and codes from database. In the case of those subjects for which any of the students registered for the first time, list exam date for this condition. In other words result is all subjects with subjectname, neptuncode and examdate. If above mentioned condition is true write the examdate in the colum otherwise leave as null for the rest which is not registered for the first time
    – Emil Bədrəddinli
    Nov 8 at 23:12










  • In your mind you have an image of that expected result. The design of that result is well formed because you already know the data, you have seen it many times, plus you control the requirements as well. I know only the few words you have used; in 3 places (the question, and a comment under the question, and the comment just above) and in all those places there are different words/variations of that explanation. This is why words are not a great way to DEFINE the expected result - data is the best way.
    – Used_By_Already
    Nov 8 at 23:29










  • In response to your latest description I have removed the having clause and introduced a case expression. I probably will not attempt another query variant based on description of the result wanted.
    – Used_By_Already
    Nov 8 at 23:33










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216409%2fgetting-undesired-output-in-sql%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










Would you try this please



SELECT
subjects.neptuncode AS "neptuncode"
, subjects.subjectname AS "subjectname"
, CASE WHEN COUNT(enrollments.enrollmentdate) = 1 THEN MAX(enrollments.enrollmentdate)
ELSE NULL
END AS "examdate"
FROM subjects
LEFT OUTER JOIN enrollments
ON enrollments.subject = subjects.subject_ID
GROUP BY
subjects.neptuncode
, subjects.subjectname
ORDER BY
subjects.neptuncode
, subjects.subjectname
;


I believe you need to treat the enrollmentdate as an aggregate, as only 1 enrollment date is permitted you can use either max() or min().






share|improve this answer






















  • It did not give the all subjects which is in the database
    – Emil Bədrəddinli
    Nov 8 at 22:15










  • Please re-try, I altered the query more than once (sorry). But it will not give ALL subjects - unless the count of enrollments is <= 1. If this query does not work then I suggest that trying to describe (in words) what you expect isn't working. Provide sample data and the expected result (as tables, not as words). Use fixed format text tables, not images, this url may help: ozh.github.io/ascii-tables
    – Used_By_Already
    Nov 8 at 22:52











  • I want to list all subject name and codes from database. In the case of those subjects for which any of the students registered for the first time, list exam date for this condition. In other words result is all subjects with subjectname, neptuncode and examdate. If above mentioned condition is true write the examdate in the colum otherwise leave as null for the rest which is not registered for the first time
    – Emil Bədrəddinli
    Nov 8 at 23:12










  • In your mind you have an image of that expected result. The design of that result is well formed because you already know the data, you have seen it many times, plus you control the requirements as well. I know only the few words you have used; in 3 places (the question, and a comment under the question, and the comment just above) and in all those places there are different words/variations of that explanation. This is why words are not a great way to DEFINE the expected result - data is the best way.
    – Used_By_Already
    Nov 8 at 23:29










  • In response to your latest description I have removed the having clause and introduced a case expression. I probably will not attempt another query variant based on description of the result wanted.
    – Used_By_Already
    Nov 8 at 23:33














up vote
1
down vote



accepted










Would you try this please



SELECT
subjects.neptuncode AS "neptuncode"
, subjects.subjectname AS "subjectname"
, CASE WHEN COUNT(enrollments.enrollmentdate) = 1 THEN MAX(enrollments.enrollmentdate)
ELSE NULL
END AS "examdate"
FROM subjects
LEFT OUTER JOIN enrollments
ON enrollments.subject = subjects.subject_ID
GROUP BY
subjects.neptuncode
, subjects.subjectname
ORDER BY
subjects.neptuncode
, subjects.subjectname
;


I believe you need to treat the enrollmentdate as an aggregate, as only 1 enrollment date is permitted you can use either max() or min().






share|improve this answer






















  • It did not give the all subjects which is in the database
    – Emil Bədrəddinli
    Nov 8 at 22:15










  • Please re-try, I altered the query more than once (sorry). But it will not give ALL subjects - unless the count of enrollments is <= 1. If this query does not work then I suggest that trying to describe (in words) what you expect isn't working. Provide sample data and the expected result (as tables, not as words). Use fixed format text tables, not images, this url may help: ozh.github.io/ascii-tables
    – Used_By_Already
    Nov 8 at 22:52











  • I want to list all subject name and codes from database. In the case of those subjects for which any of the students registered for the first time, list exam date for this condition. In other words result is all subjects with subjectname, neptuncode and examdate. If above mentioned condition is true write the examdate in the colum otherwise leave as null for the rest which is not registered for the first time
    – Emil Bədrəddinli
    Nov 8 at 23:12










  • In your mind you have an image of that expected result. The design of that result is well formed because you already know the data, you have seen it many times, plus you control the requirements as well. I know only the few words you have used; in 3 places (the question, and a comment under the question, and the comment just above) and in all those places there are different words/variations of that explanation. This is why words are not a great way to DEFINE the expected result - data is the best way.
    – Used_By_Already
    Nov 8 at 23:29










  • In response to your latest description I have removed the having clause and introduced a case expression. I probably will not attempt another query variant based on description of the result wanted.
    – Used_By_Already
    Nov 8 at 23:33












up vote
1
down vote



accepted







up vote
1
down vote



accepted






Would you try this please



SELECT
subjects.neptuncode AS "neptuncode"
, subjects.subjectname AS "subjectname"
, CASE WHEN COUNT(enrollments.enrollmentdate) = 1 THEN MAX(enrollments.enrollmentdate)
ELSE NULL
END AS "examdate"
FROM subjects
LEFT OUTER JOIN enrollments
ON enrollments.subject = subjects.subject_ID
GROUP BY
subjects.neptuncode
, subjects.subjectname
ORDER BY
subjects.neptuncode
, subjects.subjectname
;


I believe you need to treat the enrollmentdate as an aggregate, as only 1 enrollment date is permitted you can use either max() or min().






share|improve this answer














Would you try this please



SELECT
subjects.neptuncode AS "neptuncode"
, subjects.subjectname AS "subjectname"
, CASE WHEN COUNT(enrollments.enrollmentdate) = 1 THEN MAX(enrollments.enrollmentdate)
ELSE NULL
END AS "examdate"
FROM subjects
LEFT OUTER JOIN enrollments
ON enrollments.subject = subjects.subject_ID
GROUP BY
subjects.neptuncode
, subjects.subjectname
ORDER BY
subjects.neptuncode
, subjects.subjectname
;


I believe you need to treat the enrollmentdate as an aggregate, as only 1 enrollment date is permitted you can use either max() or min().







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 1:41

























answered Nov 8 at 21:58









Used_By_Already

21.6k21838




21.6k21838











  • It did not give the all subjects which is in the database
    – Emil Bədrəddinli
    Nov 8 at 22:15










  • Please re-try, I altered the query more than once (sorry). But it will not give ALL subjects - unless the count of enrollments is <= 1. If this query does not work then I suggest that trying to describe (in words) what you expect isn't working. Provide sample data and the expected result (as tables, not as words). Use fixed format text tables, not images, this url may help: ozh.github.io/ascii-tables
    – Used_By_Already
    Nov 8 at 22:52











  • I want to list all subject name and codes from database. In the case of those subjects for which any of the students registered for the first time, list exam date for this condition. In other words result is all subjects with subjectname, neptuncode and examdate. If above mentioned condition is true write the examdate in the colum otherwise leave as null for the rest which is not registered for the first time
    – Emil Bədrəddinli
    Nov 8 at 23:12










  • In your mind you have an image of that expected result. The design of that result is well formed because you already know the data, you have seen it many times, plus you control the requirements as well. I know only the few words you have used; in 3 places (the question, and a comment under the question, and the comment just above) and in all those places there are different words/variations of that explanation. This is why words are not a great way to DEFINE the expected result - data is the best way.
    – Used_By_Already
    Nov 8 at 23:29










  • In response to your latest description I have removed the having clause and introduced a case expression. I probably will not attempt another query variant based on description of the result wanted.
    – Used_By_Already
    Nov 8 at 23:33
















  • It did not give the all subjects which is in the database
    – Emil Bədrəddinli
    Nov 8 at 22:15










  • Please re-try, I altered the query more than once (sorry). But it will not give ALL subjects - unless the count of enrollments is <= 1. If this query does not work then I suggest that trying to describe (in words) what you expect isn't working. Provide sample data and the expected result (as tables, not as words). Use fixed format text tables, not images, this url may help: ozh.github.io/ascii-tables
    – Used_By_Already
    Nov 8 at 22:52











  • I want to list all subject name and codes from database. In the case of those subjects for which any of the students registered for the first time, list exam date for this condition. In other words result is all subjects with subjectname, neptuncode and examdate. If above mentioned condition is true write the examdate in the colum otherwise leave as null for the rest which is not registered for the first time
    – Emil Bədrəddinli
    Nov 8 at 23:12










  • In your mind you have an image of that expected result. The design of that result is well formed because you already know the data, you have seen it many times, plus you control the requirements as well. I know only the few words you have used; in 3 places (the question, and a comment under the question, and the comment just above) and in all those places there are different words/variations of that explanation. This is why words are not a great way to DEFINE the expected result - data is the best way.
    – Used_By_Already
    Nov 8 at 23:29










  • In response to your latest description I have removed the having clause and introduced a case expression. I probably will not attempt another query variant based on description of the result wanted.
    – Used_By_Already
    Nov 8 at 23:33















It did not give the all subjects which is in the database
– Emil Bədrəddinli
Nov 8 at 22:15




It did not give the all subjects which is in the database
– Emil Bədrəddinli
Nov 8 at 22:15












Please re-try, I altered the query more than once (sorry). But it will not give ALL subjects - unless the count of enrollments is <= 1. If this query does not work then I suggest that trying to describe (in words) what you expect isn't working. Provide sample data and the expected result (as tables, not as words). Use fixed format text tables, not images, this url may help: ozh.github.io/ascii-tables
– Used_By_Already
Nov 8 at 22:52





Please re-try, I altered the query more than once (sorry). But it will not give ALL subjects - unless the count of enrollments is <= 1. If this query does not work then I suggest that trying to describe (in words) what you expect isn't working. Provide sample data and the expected result (as tables, not as words). Use fixed format text tables, not images, this url may help: ozh.github.io/ascii-tables
– Used_By_Already
Nov 8 at 22:52













I want to list all subject name and codes from database. In the case of those subjects for which any of the students registered for the first time, list exam date for this condition. In other words result is all subjects with subjectname, neptuncode and examdate. If above mentioned condition is true write the examdate in the colum otherwise leave as null for the rest which is not registered for the first time
– Emil Bədrəddinli
Nov 8 at 23:12




I want to list all subject name and codes from database. In the case of those subjects for which any of the students registered for the first time, list exam date for this condition. In other words result is all subjects with subjectname, neptuncode and examdate. If above mentioned condition is true write the examdate in the colum otherwise leave as null for the rest which is not registered for the first time
– Emil Bədrəddinli
Nov 8 at 23:12












In your mind you have an image of that expected result. The design of that result is well formed because you already know the data, you have seen it many times, plus you control the requirements as well. I know only the few words you have used; in 3 places (the question, and a comment under the question, and the comment just above) and in all those places there are different words/variations of that explanation. This is why words are not a great way to DEFINE the expected result - data is the best way.
– Used_By_Already
Nov 8 at 23:29




In your mind you have an image of that expected result. The design of that result is well formed because you already know the data, you have seen it many times, plus you control the requirements as well. I know only the few words you have used; in 3 places (the question, and a comment under the question, and the comment just above) and in all those places there are different words/variations of that explanation. This is why words are not a great way to DEFINE the expected result - data is the best way.
– Used_By_Already
Nov 8 at 23:29












In response to your latest description I have removed the having clause and introduced a case expression. I probably will not attempt another query variant based on description of the result wanted.
– Used_By_Already
Nov 8 at 23:33




In response to your latest description I have removed the having clause and introduced a case expression. I probably will not attempt another query variant based on description of the result wanted.
– Used_By_Already
Nov 8 at 23:33

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216409%2fgetting-undesired-output-in-sql%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế