title: SQL Beginner : Multiple where clause from same table
body: <p>I'm very new to SQL and am not sure how to accomplish the following. </p>
  
  <p>I have a list of meter IDs that have a years worth of Target and a years worth of Budget data assigned, however these are all held in the same table with an identifier (TargetType) to distinguish between Target(0) &amp; Budget(1) </p>
  
  <pre><code>ID   TARGETTYPE   VALUE01   VALUE02 ...(etc up to VALUE12)
  123  0            1001      1100
  123  1            9000      9100
  456  0            5000      5100
  456  1            8000      8100      
  </code></pre>
  
  <p>Desired result would be also include information from a couple of other tables  want to add that in as well</p>
  
  <p>My query so far can bring through one set of data :</p>
  
  <pre><code>PARAMETERS 
  [Utility] Text;
  
  SELECT 
  Contacts.Group,
  Contacts.Site,
  Points.ID,
  Points.Utility,
  Points.MPAN1,
  Target.Value_01 AS [Target JAN],
  Target.Value_02 AS [Target FEB],
  Target.Value_03 AS [Target MAR],
  Target.Value_04 AS [Target APR],
  Target.Value_05 AS [Target MAY],
  Target.Value_06 AS [Target JUN],
  Target.Value_07 AS [Target JUL],
  Target.Value_08 AS [Target AUG],
  Target.Value_09 AS [Target SEP],
  Target.Value_10 AS [Target OCT],
  Target.Value_11 AS [Target NOV],
  Target.Value_12 AS [Target DEC]
  
  FROM 
  ((Contacts INNER JOIN Points ON Contacts.[Id] = Points.[Contacts_Id]) 
                    INNER JOIN Contracts ON Points.[Id] = Contracts.[Point_Id]) 
                    INNER JOIN Target ON Points.Id = Target.DataSetId
  
  WHERE 
  Points.UtilityType =[Utility]
  
  ORDER BY 
  Contacts.ClientGroup;
  </code></pre>
  
  <p>Desired Output<br>
  (the values would go TargetJan through to TargetDec and then BudgetJan through to BudgetDec but I've not shown for brevity):</p>
  
  <pre><code>Group Site     ID   Utility MPAN1    TargetJan TargetFeb etc... BudgetJan  BudgetFeb etc...      
  ABC   London   123  Gas     123456    1,000     1,100           9,000      9,100
  ABC   NewYork  456  Gas     ABC123    5,000     5,100           8,000      8,100 
  </code></pre>
  
  <p>How can I add in the same fields but depending on the value of Target.TargetType, I'm guessing that it is a Union query, but I have no idea. </p>
  
  <p>Any pointers of where to start would be gratefully received :)</p>
  
  <hr>
  
  <p><strong>UPDATE #1</strong><br>
  Thank you for your help. I think I understand the query, however there is still a bit of odd behaviour I can't figure out.</p>
  
  <p>QUERY USED<br></p>
  
  <pre><code>SELECT 
  Points.ID,
  
    SUM(CASE WHEN Target.TargetType = '0' THEN Target.Value_01 else 0 end) AS [TGT JAN],
    SUM(CASE WHEN Target.TargetType = '0' THEN Target.Value_02 else 0 end) AS [TGT FEB],
  
  FROM 
  ((Contacts INNER JOIN Points ON Contacts.[Id] = Points.[Contacts_Id]) 
                INNER JOIN Contracts ON Points.[Id] = Contracts.[Point_Id] )
                INNER JOIN Target ON Points.Id = Target.DataSetId
  
  GROUP BY
  Points.ID
  
  ORDER BY
  Points.ID;
  </code></pre>
  
  <p><br>
  Where my data row only has one Target.Type then the query returns as desired:<br>
  RAW DATA<br></p>
  
  <pre><code>ID     TARGETTYPE   VALUE_01   VALUE_02 
  10079  0            7642      5735
  </code></pre>
  
  <p>RESULT</p>
  
  <pre><code>ID    TGTJAN   TGTFEB
  10079 7642    5735
  </code></pre>
  
  <p>However ... If I have an ID with 2 or more TargetTypes then the Value output is being  multiplied by 6. <br>
  <br>
  RAW DATA<br></p>
  
  <pre><code>ID     TARGETTYPE   VALUE_01   VALUE_02 
  7423   0            58339     57441
  7423   1            1663      1637
  </code></pre>
  
  <p>RESULT</p>
  
  <pre><code>ID    TGTJAN   TGTFEB
  7423  350034   344646
  </code></pre>
  
  <p>I have experimented and removed the INNER JOIN to the contacts table (although I will need that join) and then it all works as expected ???? So why is the Contacts JOIN causing this issue ?<br>
  <br>
  Please put me out of my misery as I can not work out what is going on !</p>