title: SQL Populating with distinct data and a sequence
body: <p>My problem is as follows:</p>
  
  <ul>
  <li>I need to populate a 'cars' table based on information from instances
  of rentals of the cars.</li>
  <li>I need to create a primary key 'car_id' but only for distinct
  registration plates in the rentals table.</li>
  <li>I am creating the car_id with a sequence.</li>
  </ul>
  
  <p>I have tried the following code but receive an error:</p>
  
  <pre><code>--INSERT INTO cars c (c.plate, c.car_id)
  SELECT DISTINCT cr.plate, car_id_seq.nextval
  FROM cars_rentals cr
  ;
  </code></pre>
  
  <p>Although this will work (without distinct registration plates):</p>
  
  <pre><code>--INSERT INTO cars c (c.plate, c.car_id)
  SELECT cr.plate, car_id_seq.nextval
  FROM cars_rentals cr
  ;
  </code></pre>
  
  <p>(The top line is commented so I can see the values I'm trying to output straight away)</p>
  
  <p>So! Does anyone know how I can either; A) Get the above code to work with DISTINCT or B) find a way to get MAXVALUE of the sequence as the DISTINCT COUNT of the registration plates (so I can do two insert statements)</p>
  
  <p>Thanks in advance!
  Jack</p>