Note that there are some explanatory texts on larger screens.

plurals
  1. POSelf join to compare quarterly data from same table?
    primarykey
    data
    text
    <p>I'm helping a small rural health system add some databanking to their operations. They currently keep track of some operational statistics that they report each quarter.</p> <p>I'd like to put the meat of these responses in one table and do historical comparisons from it. I'll have some additional tables for normalization, but here's the main one.</p> <pre><code>+-------+----------+-------------+----------+ | year | quarter | question_id | response | +-------+----------+-------------+----------+ | 2000 | 1 | 100 | 50 | +-------+----------+-------------+----------+ | 2000 | 2 | 100 | 100 | +-------+----------+-------------+----------+ | 2000 | 1 | 200 | 75 | +-------+----------+-------------+----------+ | 2000 | 2 | 200 | 25 | +-------+----------+-------------+----------+ </code></pre> <p>The output I'm looking for is to put those quarterly responses for each question side-by-side so I can calculate changes better and easy up my PHP processing. Below is the desired output:</p> <pre><code>+-------------+----------------------+----------------------+ | question_id | 1st quarter response | 2nd quarter response | +-------------+----------------------+----------------------+ | 100 | 50 | 100 | +-------------+----------------------+----------------------+ | 200 | 75 | 25 | +-------------+----------------------+----------------------+ </code></pre> <p>I'm new to SQL (using MySQL 5) and the only thing I can think of is I need a self join. I've tried but can't seem to get it. Am I approaching the table structure the correct way for this kind of historical analysis?</p> <p>Any pointers would be a big help and a help to our project!</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload