Re: SQL Query Help Please !

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL Query Help Please !
Date: 2010-07-06 15:35:09
Message-ID: 20100706153509.GT7584@samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Jul 06, 2010 at 12:06:06AM -0700, GrGsM wrote:
> SELECT closedate,status,
> SUM (CASE WHEN empcode = 'NT-028' THEN 1 ELSE 0 END) AS NT028,
> SUM (CASE WHEN empcode = 'NT-031' THEN 1 ELSE 0 END) AS NT031,
> SUM (CASE WHEN empcode = 'NT-050' THEN 1 ELSE 0 END) AS NT050,
> SUM (CASE WHEN empcode = 'NT-062' THEN 1 ELSE 0 END) AS NT062
> FROM dbcleaning
> WHERE status = 'Fixed'
> AND empcode IN
> ('NT-028',
> 'NT-031',
> 'NT-050',
> 'NT-062')
> GROUP BY closedate, status
>
> Now i need a column in the same result of the query which shows the
> difference between the two columns .
>
> For Example :
> the result shoud be
> Closedate , status , NT028, NT031, NT050,NT062 , NT028-NT031

Just put your code above in an "inner select", something like:

SELECT closedate, status, NT028-NT031 AS diff
FROM (
SELECT closedate,status, SUM(CASE WHEN ...
...
GROUP BY closedate, status) x;

--
Sam http://samason.me.uk/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vick Khera 2010-07-06 15:36:28 Re: ERROR: canceling statement due to statement timeout
Previous Message Thomas Kellerer 2010-07-06 15:32:54 Re: SQL Query Help Please !