Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. sponsors. rss.
 From: Steven Rebello
  Where is Steven Rebello?
 Bangalore
 India
 Steven Rebello
 To: tushar
  Where is tushar?
 Panaji
 India
 tushar
 Tags
Subject: RE: Conditional Sum in sub-select
Thread ID: 151388 Message ID: 151714 # Views: 26 # Ratings: 0
Version: Visual FoxPro 9 Category: Databases, Tables and SQL Server
Date: Sunday, November 18, 2007 5:45:21 PM         
   


> >
> >
> > No Luck, Tushar, Sorry.
> >
> > I've remodelled the query again into two distinct selects, one with Plants and another with sum(plants). The second select works fine when the 'between' clause is not incorporated, but is obviously giving me a sum of plants of the entire table, and not of the period specified. The moment I incorporate the 'between', I get irregulated results of the sum. Basically, the between function should fire by linking on to the dailywork table immediate record and doing a sum of plants there.
> >
> > My Code:
> >
> >
SELECT Temp1.Ddate,Temp1.cblock,Temp1.cName,temp1.plants,Temp1.Men,Temp2.Plants as totPlants;
> > from;
> > (SELECT A.ddate,A.cblock,C.cname,;
> > ROUND(iif(not isnull(c.nspx) and not isnull(c.nspy) and C.nspx<>0 and C.nspy<>0,C.narea*(43560/(C.nspx*C.nspy)),C.nrdmPlants),2)  as Plants,; 
> > C.nspx as spx,C.nspy as spy,A.nmen as Men;
> > FROM Dailywork2 A;
> > Left Outer JOIN BlockProfile C;
> > ON A.cblock = C.cparent;
> > and BETWEEN(A.ddate,C.dstart,C.dend)) temp1;
> > LEFT outer Join;
> > (select cparent, sum(iif(not isnull(c.nspx) and not isnull(c.nspy) and C.nspx<>0 and C.nspy<>0,C.narea*(43560/(C.nspx*C.nspy)),0))+(SUM(C.nrdmPlants))as Plants ;
> > FROM BlockProfile C group by cparent ) Temp2;
> > ON temp1.cBlock=temp2.cparent
> > 
> > 
> > * where BETWEEN(A.ddate,C.dstart,C.dend)
> > 

> >
> > Thanks a lot, Tushar,
> >
> > Regards,
> >
> > Steve.
>
> Steve
>
> I feel your second select has to be joined to the Dailywork2 to select only the relevant date ranges in the SUM().
>
>
> SELECT A.ddate,A.cblock,C.cname,NumberPlants(c.nspx,c.nspy,C.narea,C.nrdmPlants)  as Plants,; 
> C.nspx as spx,C.nspy as spy,A.nmen as Men ;
> FROM Dailywork2 A ;
> Left Outer JOIN BlockProfile C ;
> ON A.cblock = C.cparent ;
> and BETWEEN(A.ddate,C.dstart,C.dend);
> LEFT outer JOIN ;
> (select cparent, sum(NumberPlants(d.nspx,d.nspy,d.narea,d.nrdmPlants)) as Plants ;
> FROM BlockProfile d group by cparent ;
> <B>left outer JOIN Dailywork2 e ;
> on e.cblock = d.cparent ;
> WHERE BETWEEN(e.ddate,d.dstart,d.dend)</B> ;
> ) Temp2;
> ON temp1.cBlock=temp2.cparent
> 
> 
> 
> FUNCTION NumberPlants
> LPARAMETERS lnX,lnY,lnArea,lnNrdm
> IF ISNULL(lnX) OR ISNULL(lnY) OR lnX=0 OR lnY=0
> 	RETURN lnNrdm
> ELSE
> 	RETURN ROUND(lnArea*43560/(lnX*lnY),2)
> ENDIF
> 

>
> The function NumberPlants is just because I was getting confused with the long statement. You can use the old thing there.
>
> Regards
> Tushar



Hi Tushar,

Was trying out your code but I got a syntax error at the point of sum(numberPlants.

Since this is the first time I am using a sub function in an SQl select, I wasn't able to sort it out.

Could you please check from your end. Thanks for all the help, tushar,

With best regards,

Steve

Tushar,

While I was tinkering with this SQL, I derived another simplistic sequence of code, which might make easier reading, and requires the same result.(The sum of plants that fulfill the criteria of being within dstart and dend).I've also cut off some of the general fields to enhance readability. Just thought I'd send it to you, to see whether it could be used with some modifications. Somehow, the sum is not giving me desired results here too.

The code:

SELECT A.ddate,A.cblock,E.cname,;
ROUND(iif(not isnull(c.nspx) and not isnull(c.nspy) and C.nspx<>0 and C.nspy<>0,C.narea*(43560/(C.nspx*C.nspy)),C.nrdmPlants),2) as Plants,; 
sum(iif(not isnull(E.nspx) and not isnull(E.nspy) and E.nspx<>0 and E.nspy<>0,E.narea*(43560/(E.nspx*E.nspy)),0))+(SUM(E.nrdmPlants))as TotPlants ;
FROM Dailywork2 A;
LEFT outer JOIN BlockProfile C;
LEFT Outer JOIN Blockprofile E;
on A.cblock = E.cParent;
on A.cBlock = C.cParent;
and BETWEEN(A.ddate,C.dstart,C.dend);
AND BETWEEN(A.ddate,E.dstart,E.dend);
group by 1,2,3,4



One Blockprofile Table is to give individual values of Plants (C). The other Blockprofile Table (E) is to give the consolidated summed value of plants for the between Period. Both tables are joined to the same field of Dailywork (A)

Steve



COMPLETE THREAD
Conditional Sum in sub-select Posted by Steven Rebello @ 11/14/2007 2:16:34 PM
RE: Conditional Sum in sub-select Posted by tushar @ 11/14/2007 2:55:18 PM
RE: Conditional Sum in sub-select Posted by Steven Rebello @ 11/14/2007 3:02:53 PM
RE: Conditional Sum in sub-select Posted by Steven Rebello @ 11/14/2007 4:13:55 PM
RE: Conditional Sum in sub-select Posted by Ken Murphy @ 11/14/2007 4:17:51 PM
RE: Conditional Sum in sub-select Posted by Steven Rebello @ 11/14/2007 4:43:15 PM
RE: Conditional Sum in sub-select Posted by tushar @ 11/14/2007 6:21:17 PM
RE: Conditional Sum in sub-select Posted by Steven Rebello @ 11/16/2007 4:31:05 AM
RE: Conditional Sum in sub-select Posted by tushar @ 11/16/2007 4:45:32 PM
RE: Conditional Sum in sub-select Posted by Steven Rebello @ 11/18/2007 5:45:21 PM
RE: Conditional Sum in sub-select Posted by tushar @ 11/19/2007 7:35:11 AM
RE: Conditional Sum in sub-select Posted by Steven Rebello @ 11/21/2007 4:52:34 PM
RE: Conditional Sum in sub-select Posted by tushar @ 11/21/2007 6:27:59 PM
RE: Conditional Sum in sub-select Posted by Steven Rebello @ 11/22/2007 12:12:49 PM
RE: Conditional Sum in sub-select Posted by tushar @ 11/22/2007 2:43:37 PM
RE: Conditional Sum in sub-select Posted by Steven Rebello @ 11/22/2007 4:11:52 PM
RE: Conditional Sum in sub-select Posted by Steven Rebello @ 11/22/2007 5:08:04 PM
RE: Conditional Sum in sub-select Posted by tushar @ 11/22/2007 7:28:08 PM