Domanda

Ho una tabella che contiene intervalli:

CREATE TABLE tbl (
    user_id: INTEGER,
    start: TIMESTAMP,
    end: TIMESTAMP,
    billable: BOOLEAN
);

Voglio creare una vista simile alla seguente:

user_id | billable_time | unbillable_time

Posso ottenere uno di questi due usando una query come questa:

SELECT user_id, sum(end - start) AS billable_time WHERE billable = TRUE GROUP BY user_id;

Tuttavia, ho bisogno di entrambe le colonne, con un tempo non fatturabile equivalente:

SELECT user_id, sum(end - start) AS unbillable_time WHERE billable = FALSE GROUP BY user_id;

Come posso ottenere quei due valori in una query?

È stato utile?

Soluzione

select user_id, sum(case billable when 1 then (end - start) end) as can_bill,
  sum(case billable when 0 then (end - start) end) as unbillable

Altri suggerimenti

SELECT user_id, billable, sum(end - start) AS billable_time 
GROUP BY user_id,billable;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top