Received: from localhost (maia-3.hub.org [200.46.204.184]) by postgresql.org (Postfix) with ESMTP id 10DAF9FBDF2 for ; Tue, 19 Jun 2007 12:24:36 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.184]) (amavisd-maia, port 10024) with ESMTP id 86132-04 for ; Tue, 19 Jun 2007 12:24:31 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.171]) by postgresql.org (Postfix) with ESMTP id 95F039FB660 for ; Tue, 19 Jun 2007 12:24:31 -0300 (ADT) Received: by ug-out-1314.google.com with SMTP id u2so147246uge for ; Tue, 19 Jun 2007 08:24:30 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=AvFHCBaGrB65r3a2LC3wVGlVvtr8+lmTinTAULYjxjwmSBZamIBCe8R7F4lGXDoJ0F5KIzi7J4+Bp6AyFH6u6rDcphRAUoUyOgWpjCuht7o/hU1PnAjE6XaDlQG4MESVqMsRuysPC0ZUkPDiWFkUHR68wDxU0G5U0VAk5iHCRQ8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=Q5dbTEoGrJNCxEj+7n6JqDKWMCKctcV07uQudH57oLubMTH/tP/FvuHj7TONE/3GEPGgkul6EuwfjFOY0BwiKBJ214e6M+I5Hm4y4vKhodTniZILQ4n1hddHxBwKJseYLGFpoaJCDuRdB2QGzzySs86gASEciQzjN5jBefYxhnU= Received: by 10.78.183.8 with SMTP id g8mr2938620huf.1182266669869; Tue, 19 Jun 2007 08:24:29 -0700 (PDT) Received: by 10.78.18.9 with HTTP; Tue, 19 Jun 2007 08:24:29 -0700 (PDT) Message-ID: Date: Tue, 19 Jun 2007 11:24:29 -0400 From: "Merlin Moncure" To: "Ranieri Mazili" Subject: Re: Subquery problems Cc: pgsql-general@postgresql.org In-Reply-To: <4677EF39.6050904@terra.com.br> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <4677C952.9060902@terra.com.br> <4677EF39.6050904@terra.com.br> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200706/1159 X-Sequence-Number: 115982 On 6/19/07, Ranieri Mazili wrote: > > sure!. > > > > SELECT cast(((sum(A.qty_employees_total) > > -(sum(A.qty_absence) > > -sum(A.qty_vacation) > > -sum(A.qty_diseased) > > -sum(A.qty_indirect) > > -sum(A.qty_transferred)) > > +sum(A.qty_received))/DIV_MES01) AS integer), > > C.id_production_area, > > cast(DIV_MES01 as text) AS mes > > FROM head_count A, machine B, machine_type C, > > ( > > select case when ct = 0 then 1 else ct end as DIV_MES01 from > > ( > > select count(distinct production_date) as ctfrom production where > > extract(year from production_date) = EXTRACT(YEAR FROM current_date) > > ) q > > ) D > > WHERE EXTRACT(YEAR FROM head_count_date) = EXTRACT(YEAR FROM > > current_date) > > AND EXTRACT(MONTH FROM head_count_date) = DIV_MES01 > > AND A.id_machine = B.id_machine > > AND B.id_machine_type = C.id_machine_type > > GROUP BY C.id_production_area, B.id_machine_type; > > > > ok, I didn't syntax check this monster, but it should give you a > > start...the trick is to use an 'inline view' to expand your variable > > list into a set. > > > On this way didn't work, I wanna do only one query to return the data of > entire year, not only one month, but thanks for try. > If someone have an idea of how do it, please, help :D > the inlne view (table 'D') breaks down the year month by month. maybe you have to add DIV_MES01 to the group by? merlin