Re: SQL Query Help Please !

From: Guy Rouillier <guyr-ml1(at)burntmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: SQL Query Help Please !
Date: 2010-07-06 16:59:39
Message-ID: 4C3360FB.8020306@burntmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 7/6/2010 3:06 AM, GrGsM wrote:
>
> Hi all
>
> I am using the following query for data to be displayed in crosstab :
>
> 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 .

SUM (CASE WHEN empcode = 'NT-028' THEN 1 ELSE 0 END) -
SUM (CASE WHEN empcode = 'NT-031' THEN 1 ELSE 0 END) as NT028_NT031

Or if you prefer, define an outer select with your select in the FROM
clause; in the outer select, you can select each of the 4 columns plus
any difference you like.

--
Guy Rouillier

--
Guy Rouillier

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Andy Colson 2010-07-06 19:43:12 make view with union return one record
Previous Message Thom Brown 2010-07-06 15:57:20 Re: moderninzing/upgrading mail list format