title: How to do PostgreSQL crosstab query there values are missing in the column
body: <p>I'm trying to create a turn around time report using PostgreSQL 9.5 crosstab query where referrals are bucketed into days 1, 2, 3, 4, > 4 (see output below).
  I've got the query to work however, if I ran the query and day 2 values were missing the entire row shifts one cell left. So day two holds day 3's value, day 3 holds day 4's value, etc...
  Can someone help me on how I can keep day 2 in this example be blank or zero but not shift the row left? </p>
  
  <p>Thanks in advance. </p>
  
  <p><strong>SCRIPT:</strong></p>
  
  <pre><code>DROP TABLE IF EXISTS dt_temp;
  CREATE TABLE dt_temp(id SERIAL, day int , referrals bigint);
  
  INSERT INTO dt_temp(day, referrals) VALUES(1, 60);
  INSERT INTO dt_temp(day, referrals) VALUES(2, 15);
  INSERT INTO dt_temp(day, referrals) VALUES (3, 13);
  INSERT INTO dt_temp(day, referrals) VALUES (4, 10);
  INSERT INTO dt_temp(day, referrals) VALUES (5, 1);
  INSERT INTO dt_temp(day, referrals) VALUES (6, 2);
  INSERT INTO dt_temp(day, referrals) VALUES (7, 1);
  INSERT INTO dt_temp(day, referrals) VALUES (8, 1);
  
  Select * from crosstab(
      $$
      Select 'INDICATOR1' "INDICATOR", days::text, sum(referrals)::text     
  from (
        SELECT CASE  
        WHEN day &gt; 4 THEN '&gt;4'
        ELSE day::text
        END "days",
     referrals
     FROM dt_temp
     ) "t"
  group by 1,2
  order by 2
  $$
  ) AS dx2ref(Indicator text, Day1 text,  Day2 text, Day3 text, Day4 text, "Day &gt; 4" text )
  </code></pre>
  
  <p><strong>OUTPUT:</strong></p>
  
  <pre><code>indicator  Day1 Day2 Day3 Day4 Day &gt; 4
  --------------------------------------
  INDICATOR1 60   15   13   10   5
  </code></pre>