Jump to content

artacus

Members
  • Posts

    737
  • Joined

  • Last visited

    Never

Posts posted by artacus

  1. You'll need to do a left join:

     

    SELECT chn.name AS chain, dlr.name AS dealer, ....
    FROM dealers dlr
    LEFT JOIN dealer_chain_lookup cl ON dlr.dealer_id = cl.dealer_id
    LEFT JOIN dealer chn ON cl.chain_dealer_id = chn.dealer_id
    ORDER BY 1, 2
    

     

  2. Easiest way would be to use the keyword DEFAULT for the invoice number.

     

    INSERT INTO invoices (invoicenumber, invoice_time, customer_id)

    VALUES (DEFAULT, current_timestamp, 222);

     

    Otherwise you can use nextval('invoice_seq') where invoice_seq is the name of your sequence.

     

  3. Well just because you've made it harder doesn't mean you've normalized it :)

     

    So I'm going to guess these are job interests a person may have. So what do you do when someone comes along with 4 job interests? or 5 or 6? Do you keep adding tables?

     

    Instead do something like this:

    CREATE TABLE person (
      person_id  INT NOT NULL PRIMARY KEY,
      first_name VARCHAR(30),
      last_name VARCHAR(30),
    );
    
    CREATE TABLE job_interests(
      interest_id INT NOT NULL PRIMARY KEY,
      job_description VARCHAR(80) NOT NULL
    );
    
    
    CREATE TABLE person_job_interests (
      person_id  INT NOT NULL,
      interest_id INT NOT NULL,
      preference_no INT2,
      CONSTRAINT person_job_interests_pk PRIMARY KEY (person_id, interest_id)
    );
    

     

    Also "english" appears to be a poor choice for a column name here. Use something like 'job_category', 'label' or 'description'.

  4. This isn't the greatest design. Always try to keep the start and end times for an event in a single row.

    This would be ideal for a full outer join but I'm pretty sure MySQL still doesn't support them.

     

    SELECT CASE WHEN yesterday.id IS NULL AND tomorrow.id IS NULL THEN 
      concat(today.id, ' was added ', today.date, ' and removed ', tomorrow.date)
      WHEN yesterday.id IS NULL THEN concat(today.id, ' was added ', today.date)
      WHEN tomorrow.id IS NULL THEN concat(today.id, ' was removed ', tomorrow.date)
      ELSE 'Artacus freaking rocks'
    END AS status_update
    FROM example today 
    LEFT JOIN example yesterday ON today.id = yesterday.id
      AND today.date = yesterday.date + INTERVAL 1 DAY
    LEFT JOIN example tomorrow ON today.id = tomorrow.id
      AND today.date = tomorrow.date - INTERVAL 1 DAY
    WHERE yesterday.id IS NULL OR tomorrow.id IS NULL
    

  5. No, you won't be able to do that with copy.

     

    Is this a one time thing or is it something you need to process on a continual basis?

     

    If it's a one time thing you can copy/reorder the columns in Excel. You have several options for continual process.

    One would be to copy to a staging table and then transform into your regular table. There is pgloader. It will allow you to do transforms before outputting to copy.

    http://www.postgresql.org/docs/8.3/static/pgcrypto.html

     

    Another option would be to use an ETL tool. There are loads of them out there.

     

     

  6. Am I close?

     

    You got it. Oh and just comment out the first line to test when you are making views.

     

    --CREATE OR REPLACE VIEW im_project_types AS
    SELECT project_status_id, CASE WHEN id = 96 THEN 'ThisIsMyLabel'
    ELSE originalLabel END AS originalLabel
    FROM im_project_types;
    

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.