title: In SQL how to JOIN and then GROUP BY a field that contains Null or int
body: <p>I made a SQL query that joined three tables together but duplicate rows shown in result. i try to resolve this problem by using <strong>GROUP BY</strong> statement but this way raise error because:</p>
  
  <pre><code>OR "allame_secretaries_forms_contributorcommissionsscore"."id" IS NULL
  </code></pre>
  
  <p>line saying that <code>allame_secretaries_forms_contributorcommissionsscore</code> can not exist. my current query is below:</p>
  
  <pre><code>SELECT
      "activities_baseactivity" . "id"
      ,"allame_secretaries_forms_contributorcommissionsscore" . "activity_id"
      ,"activities_baseactivity" . "activity_type_name"
      ,"activities_baseactivity" . "language_type"
      ,"activities_baseactivity" . "other_language"
      ,"activities_baseactivity" . "date_created"
      ,"activities_baseactivity" . "date_modified"
      ,"activities_baseactivity" . "creator_id"
      ,"activities_baseactivity" . "modifier_id"
      ,"activities_baseactivity" . "show_in_homepage"
      ,"activities_baseactivity" . "professor_id"
      ,"activities_baseactivity" . "attachment"
      ,"activities_baseactivity" . "state"
      ,"activities_thesis" . "baseactivity_ptr_id"
      ,"activities_thesis" . "score"
      ,"activities_thesis" . "ratio"
      ,"activities_thesis" . "score1"
      ,"activities_thesis" . "score2"
      ,"activities_thesis" . "judgment_result"
      ,"activities_thesis" . "bpm_process_id"
      ,"activities_thesis" . "judgment_date"
      ,"activities_thesis" . "position"
      ,"activities_thesis" . "edu_id"
      ,"activities_thesis" . "course_title"
      ,"activities_thesis" . "thesis_title_fa"
      ,"activities_thesis" . "thesis_title_en"
      ,"activities_thesis" . "start_date"
      ,"activities_thesis" . "defense_date"
      ,"activities_thesis" . "student_name_fa"
      ,"activities_thesis" . "student_name_en"
      ,"activities_thesis" . "place_of_study_id"
      ,"activities_thesis" . "department_id"
      ,"activities_thesis" . "external_unit"
      ,"activities_thesis" . "educational_group_id"
      ,"activities_thesis" . "country_id"
      ,"activities_thesis" . "number_of_unit"
      ,"activities_thesis" . "project_type"
      ,"activities_thesis" . "project_state"
      ,"activities_thesis" . "approved_date"
      ,"activities_thesis" . "assignment_number"
      ,"allame_secretaries_forms_contributorcommissionsscore" . "college_score"
      ,"allame_secretaries_forms_contributorcommissionsscore" . "university_score"
      FROM
      "activities_thesis" INNER JOIN "activities_baseactivity"
          ON (
          "activities_thesis" . "baseactivity_ptr_id" = "activities_baseactivity" . "id"
          ) INNER JOIN "accounts_contributor"
          ON (
          "accounts_contributor" . "baseactivity_id" = "activities_baseactivity" . "id"
          ) LEFT JOIN "allame_secretaries_forms_contributorcommissionsscore"
          ON (
      ("accounts_contributor" . "id" = "allame_secretaries_forms_contributorcommissionsscore" . "contributor_id"
            OR "allame_secretaries_forms_contributorcommissionsscore"."id" IS NULL
          )
          AND "accounts_contributor" . "profile_id" = 253
          )
      WHERE
      (
          "activities_baseactivity" . "professor_id" = 253
          AND "activities_baseactivity" . "state" = 2
          AND "activities_thesis" . "defense_date" &lt; '2014-01-21'
          AND "activities_thesis" . "defense_date" &gt;= '2013-01-20'
  AND "activities_baseactivity" . "id" NOT IN (32161, 32676, 32677, 32679, 32680, 32681, 32682, 32683, 32150, 32164, 19519))
  GROUP BY "activities_thesis" . "baseactivity_ptr_id", "activities_baseactivity" . "id", "allame_secretaries_forms_contributorcommissionsscore" . "activity_id"
  </code></pre>
  
  <p><strong>How to resolve this problem?</strong></p>