Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Trouble getting result from join query
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows 7
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Desktop
Miscellaneous
Thread ID:
01545667
Message ID:
01545994
Views:
31
>>You can have two variations and you need to decide which one is correct:
>>
>>This one:
>>
>>
>>Select Count( Distinct person.PersonID) As PersonCount , ;
>>Sum(IIF(pinfo.workdate >= {^2012-05-01} and pinfo.workdate <= {^2012-05-31},pinfo.workhour,0000.00)) As TotalHours;
>>		FROM person INNER Join pinfo On person.PersonID = pinfo.PersonID for  ;
>>		WHERE person.startdate >= {^2012-05-01};
>>		AND person.startdate <= {^2012-05-31};
>>		AND Upper(person.court) IN ("R","J") ;
>>		AND Upper(person.sex) = 'M' 
>>
>>
>>Will return all people satisfying your where criteria, but only work hours in the workdate ranges.
>>
>>This query:
>>
>>Select Count( Distinct person.PersonID) As PersonCount , ;
>>Sum(pinfo.workhour) As TotalHours;
>>		FROM person INNER Join pinfo On person.PersonID = pinfo.PersonID for  ;
>>		WHERE person.startdate >= {^2012-05-01};                                
>>		AND person.startdate <= {^2012-05-31};
>>                                        AND pinfo.workdate >= {^2012-05-01} and pinfo.workdate <= {^2012-05-31} ;
>>		AND Upper(person.court) IN ( "R", "J") ;
>>		AND Upper(person.sex) = 'M' ;
>>
>>
>>Will only select people who worked in the work date range.
>>
>>Now, if you need to exclude the non work days from that query and calculation (assuming someone worked in these days), you need to have a Calendar table that clearly tells which days are work days and which are not. Then you'll join with that Calendar table to get your info.
>
>
>Thanks for replying back. So, with the first query, will the pinfo.workhours only pickup values for people found in the Count(Distinct person.PersonID). I have to make sure that:
>
>1: the people in Person table that fit the Person.StartDate criterial are picked up in the Pinfo table only and
>2: of those people found in Person table from above, I further have to filter by Pinfo.Workdates found in Pinfo and pick those to sum the workhours.
>
>Would it be safe to say that is what the first query does. I ran both queries. The count(distinct) result differed, but the sum results stayed the same. Thanks again, much appreciate the help
>
>nick

If you only want to pick people who worked in the workdate ranges, then you need to use the second query where condition is in the filter (WHERE clause).
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform