DIVISION with Group By

From: "hdhgdh mjhff" <jp(dot)mcy(dot)10(at)gmail(dot)com>
To: pgsql-sql(at)postgresql(dot)org
Subject: DIVISION with Group By
Date: 2008-12-02 02:57:04
Message-ID: 2c532b630812011857r554fadfcpc1be8be297910c57@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hai all,

I am new to SQL coding.
I need some help in writing SQL
In my task i have to calculate division of two sub queries.
each sub query has its own group by clause.

Here with i have Paste my table information and my SQL.

/* tdos table */
CREATE TABLE tdos
(
dosid integer NOT NULL DEFAULT nextval('dos_sequence'::regclass),
dcode character varying(20) NOT NULL,
count integer NOT NULL,
dosresult integer NOT NULL,
standardcode character varying(12) NOT NULL,
dosnakbn integer NOT NULL,
dosintrsv1 integer,
dosintrsv2 integer,
dosdatersv1 date,
dosdatersv2 date,
dostxtrsv1 text,
dostxtrsv2 text,
CONSTRAINT tdos_pkey PRIMARY KEY (dosid),
CONSTRAINT tdos_mstandard_fkey FOREIGN KEY (standardcode)
REFERENCES mstandard (standardcode) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT tdos_tdlr_fkey FOREIGN KEY (dlrcode)
REFERENCES tdlr (dlrcode) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (OIDS=FALSE);
ALTER TABLE tdos OWNER TO postgres;

/*mrea table*/
CREATE TABLE mrea
(
arcode character varying(20) NOT NULL,
aeregistdate date NOT NULL,
arupdatedate date NOT NULL,
arstartdate date,
arenddate date,
areaintrsv1 integer,
areaintrsv2 integer,
areadatersv1 date,
areadatersv2 date,
areatxtrsv1 text,
areatxtrsv2 text,
CONSTRAINT mrea_pkey PRIMARY KEY (arcode)
)
WITH (OIDS=FALSE);
ALTER TABLE mrea OWNER TO postgres;

/*mscn table*/
CREATE TABLE mscn
(
scncode character varying(20) NOT NULL,
arcode character varying(20) NOT NULL,
oldscncode character varying(20) NOT NULL,
dtkbn integer NOT NULL,
inputfilepath text NOT NULL,
outputfilepath text NOT NULL,
backupfilepath text NOT NULL,
nscstartdate date,
nscenddate date,
nscregistdate date NOT NULL,
nscupdatedate date NOT NULL,
nscintrsv1 integer,
nscintrsv2 integer,
nscdatersv1 date,
nscdatersv2 date,
nsctxtrsv1 text,
nsctxtrsv2 text,
CONSTRAINT mscn_pkey PRIMARY KEY (scncode),
CONSTRAINT mkey_fkey FOREIGN KEY (arcode)
REFERENCES mrea (arcode) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (OIDS=FALSE);
ALTER TABLE mscn OWNER TO postgres;

/*mstandard table*/

CREATE TABLE mstandard
(
standardcode character varying(12) NOT NULL,
oldstandardtdcode character varying(12) NOT NULL,
"level" character varying(3) NOT NULL,
title text NOT NULL,
ststartdate date,
stenddate date,
registdate date NOT NULL,
updatedate date NOT NULL,
stdintrsv1 integer,
stdintrsv2 integer,
stddatersv1 date,
stddatersv2 date,
stdtxtrsv1 text,
stdtxtrsv2 text,
CONSTRAINT mstandard_pkey PRIMARY KEY (standardcode)
)
WITH (OIDS=FALSE);
ALTER TABLE mstandard OWNER TO postgres;

SQL i have used:

select ((select count(*) from tdos,tdlr,mscn,mstandard
where tdos.standardcode = mstandard.standardcode and
tdos.dlrcode=tdlr.dlrcode and
tdlr.scncode = mscn.scncode and
tdos.dosresult = 1 and
mscn.scncode = 'AAAA' and
tdos.dosnakbn = 0 and
tdos.dosdatersv1 between '2006/11/1' and '2007/4/1')) as Periodone,
(select count(*) from tdos,tdlr,mscn,mstandard
where tdos.standardcode = mstandard.standardcode and
tdos.dlrcode=tdlr.dlrcode and
tdlr.scncode = mscn.scncode and
mscn.scncode = 'AAAA' and
tdos.dosresult = 1 and
tdos.dosnakbn = 0 and
tdos.dosdatersv1 between '2007/4/1' and '2008/11/1' ) as Periodtwo,
Round(((select count(*) from tdos,tdlr,mscn,mstandard
where tdos.standardcode = mstandard.standardcode and
tdos.dlrcode=tdlr.dlrcode and
tdlr.scncode = mscn.scncode and
mscn.scncode = 'AAAA' and
tdos.dosresult = 1 and
tdos.dosnakbn = 0 and
tdos.dosdatersv1 between '2007/4/1' and '2008/11/1') -
(select count(*) from tdos,tdlr,mscn,mstandard
where tdos.standardcode = mstandard.standardcode and
tdos.dlrcode=tdlr.dlrcode and
tdlr.scncode = mscn.scncode and
tdos.dosresult = 1 and
tdos.dosnakbn = 0 and
mscn.scncode = 'AAAA' and
tdos.dosdatersv1 between '2006/11/1' and '2007/4/1'))/(1.0*((select count(*)
from tdos,tdlr,mscn,mstandard
where tdos.standardcode = mstandard.standardcode and
tdos.dlrcode=tdlr.dlrcode and
tdlr.scncode = mscn.scncode and
tdos.dosresult = 1 and
tdos.dosnakbn = 0 and
mscn.scncode = 'AAAA' and
tdos.dosdatersv1 between '2007/4/1' and '2008/11/1')))*100,0)
As Growthrate.

in this SQL i calculated the result only for one scncode.
i need this result for all scncode under mscn table.

If you have other logic to calculate this,please tell me the logic

Thanks in advance.
John.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Pavel Stehule 2008-12-02 05:58:58 Re: JOIN results of refcursor functions
Previous Message Milan Oparnica 2008-12-01 23:52:13 Re: JOIN results of refcursor functions