Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Self-join to return either/or results?

Self-join to return either/or results?

Scheduled Pinned Locked Moved Database
databasequestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AHSTech
    wrote on last edited by
    #1

    Hi, given the following tables (a student assessment system): Student: StudentID StuAssess: StudentID, SubjectCode, AssessID, CritID, CritVal Each student has one row in StuAssess for each subject for each assessment or target. The round/target is identified by the AssessID and CritID. Is it possible to write a query that will return each row in StuAssess for a student where there is either a 'Result' (i.e. AssessID = 10, CritID = 35) AND/OR a 'Target' (i.e. AssessID = 11, CritID = 42), something like this as a resultset: SubjectCode, ResultCritVal, TargetCritVal so a null 'ResultCritVal' but populated 'TargetCritVal' would indicate a target without a result, null 'Target' but populated 'Result' means result without a target, and null for both columns means no result or target. I've resorted to using a 2-pass query and handling the two resultsets in my code, but if it's possible, I'd like to just have the one resultset. Hope I've managed to explain the requirement.

    D S C 3 Replies Last reply
    0
    • A AHSTech

      Hi, given the following tables (a student assessment system): Student: StudentID StuAssess: StudentID, SubjectCode, AssessID, CritID, CritVal Each student has one row in StuAssess for each subject for each assessment or target. The round/target is identified by the AssessID and CritID. Is it possible to write a query that will return each row in StuAssess for a student where there is either a 'Result' (i.e. AssessID = 10, CritID = 35) AND/OR a 'Target' (i.e. AssessID = 11, CritID = 42), something like this as a resultset: SubjectCode, ResultCritVal, TargetCritVal so a null 'ResultCritVal' but populated 'TargetCritVal' would indicate a target without a result, null 'Target' but populated 'Result' means result without a target, and null for both columns means no result or target. I've resorted to using a 2-pass query and handling the two resultsets in my code, but if it's possible, I'd like to just have the one resultset. Hope I've managed to explain the requirement.

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      Maybe the "Case" statement will work for you. http://msdn.microsoft.com/en-us/library/ms181765.aspx[^]

      A 1 Reply Last reply
      0
      • D David Mujica

        Maybe the "Case" statement will work for you. http://msdn.microsoft.com/en-us/library/ms181765.aspx[^]

        A Offline
        A Offline
        AHSTech
        wrote on last edited by
        #3

        Thanks David. Maybe it's getting too late here, but I can't figure out how CASE would help me, given that for any given subjectcode, each student would have EITHER a result (AssessID = 10, CritID = 35) OR a target (AssessID = 11, CritID = 42) OR both OR neither. I've got it in my head that I can only accomplish this through one or more self-joins, but the only common columns I can work with are StudentID and SubjectCode - there's no concept of a left or right table here. Or am I really suffering brain-fade (it's been a long day) :-) Derek

        1 Reply Last reply
        0
        • A AHSTech

          Hi, given the following tables (a student assessment system): Student: StudentID StuAssess: StudentID, SubjectCode, AssessID, CritID, CritVal Each student has one row in StuAssess for each subject for each assessment or target. The round/target is identified by the AssessID and CritID. Is it possible to write a query that will return each row in StuAssess for a student where there is either a 'Result' (i.e. AssessID = 10, CritID = 35) AND/OR a 'Target' (i.e. AssessID = 11, CritID = 42), something like this as a resultset: SubjectCode, ResultCritVal, TargetCritVal so a null 'ResultCritVal' but populated 'TargetCritVal' would indicate a target without a result, null 'Target' but populated 'Result' means result without a target, and null for both columns means no result or target. I've resorted to using a 2-pass query and handling the two resultsets in my code, but if it's possible, I'd like to just have the one resultset. Hope I've managed to explain the requirement.

          S Offline
          S Offline
          SilimSayo
          wrote on last edited by
          #4

          SELECT DISTINCT
          A.StudentID,
          A.SubjectCode,
          B.CritVal As ResultCritVal,
          C.CritVal As TargetCritVal
          FROM StuAssess A LEFT JOIN StuAssess B
          ON A.StudentID=B.StudentID, A.SubjectCode=B.SubjectCode
          LEFT JOIN StuAssess C
          ON A.StudentID=C.StudentID, A.SubjectCode=C.SubjectCode
          WHERE (B.AssessID=10 AND B.CritID=35) AND (C.AssessID=11 AND C.CritID=42)

          I haven't test the code but I did something similar to that sometime back. BTW... StuAssess ... I'd change that name.

          1 Reply Last reply
          0
          • A AHSTech

            Hi, given the following tables (a student assessment system): Student: StudentID StuAssess: StudentID, SubjectCode, AssessID, CritID, CritVal Each student has one row in StuAssess for each subject for each assessment or target. The round/target is identified by the AssessID and CritID. Is it possible to write a query that will return each row in StuAssess for a student where there is either a 'Result' (i.e. AssessID = 10, CritID = 35) AND/OR a 'Target' (i.e. AssessID = 11, CritID = 42), something like this as a resultset: SubjectCode, ResultCritVal, TargetCritVal so a null 'ResultCritVal' but populated 'TargetCritVal' would indicate a target without a result, null 'Target' but populated 'Result' means result without a target, and null for both columns means no result or target. I've resorted to using a 2-pass query and handling the two resultsets in my code, but if it's possible, I'd like to just have the one resultset. Hope I've managed to explain the requirement.

            C Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #5

            How about a view that implements one criteria, called Result_view

            create view result_view as
            select StudentID
            ,SubjectCode
            ,AssessID
            ,CritID
            ,CritVal
            from StuAssess
            where assessId = 10 and CritID = 35
            ;

            Then another view that implements the other criteria, called Target_view

            create view target_view as
            select StudentID
            ,SubjectCode
            ,AssessID
            ,CritID
            ,CritVal
            from StuAssess
            where assessId = 11 and CritID = 42
            ;

            Then you query the views

            select StudentID
            ,SubjectCode
            ,AssessID
            ,CritID
            ,CritVal
            from result_view
            union
            select StudentID
            ,SubjectCode
            ,AssessID
            ,CritID
            ,CritVal
            from target_view
            ;

            :)

            Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

            S 1 Reply Last reply
            0
            • C Chris Meech

              How about a view that implements one criteria, called Result_view

              create view result_view as
              select StudentID
              ,SubjectCode
              ,AssessID
              ,CritID
              ,CritVal
              from StuAssess
              where assessId = 10 and CritID = 35
              ;

              Then another view that implements the other criteria, called Target_view

              create view target_view as
              select StudentID
              ,SubjectCode
              ,AssessID
              ,CritID
              ,CritVal
              from StuAssess
              where assessId = 11 and CritID = 42
              ;

              Then you query the views

              select StudentID
              ,SubjectCode
              ,AssessID
              ,CritID
              ,CritVal
              from result_view
              union
              select StudentID
              ,SubjectCode
              ,AssessID
              ,CritID
              ,CritVal
              from target_view
              ;

              :)

              Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

              S Offline
              S Offline
              SilimSayo
              wrote on last edited by
              #6

              I think he is looking for something in the form: StudentID, SubjectCode, ResultCritVal, TargetCritVal

              C 1 Reply Last reply
              0
              • S SilimSayo

                I think he is looking for something in the form: StudentID, SubjectCode, ResultCritVal, TargetCritVal

                C Offline
                C Offline
                Chris Meech
                wrote on last edited by
                #7

                Very true, but he's failed to indicate how a ResultCritVal or TargetCritVal are calculated or populated. He's only shown what values for the original columns are to be. By using the views I suggested, the conditions for each criteria are provided. How those column values are 'translated' to a ResultCritVal or a TargetCritVal would still be necessary. :)

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups