This topic is locked

tricky count operation in the middle of a join

10/20/2009 11:59:40 AM
PHPRunner General questions
phray author

I have two tables, WorkshopMaster and WorkshopDetail. On the Master List Page I'd like to count the number of COUPLES for each workshop.
These workshops are mostly for married couples, but sometimes only one of the couple can show up. Each record in the WorkshopDetail has a field for Male's Name and Female's Name. When only one of the couple shows up, we just enter that info in the approriate field, leaving the other name field blank.
I figured out how to add a field in PHPRunner on the Query tab for the Count() for both these fields. Like this:

SELECT

WorkshopMaster.ID,

WorkshopMaster.`Workshop Fac ID`,

WorkshopMaster.`Workshop Date`,

Count(WorkshopDetail.`Male's Name`) + Count(WorkshopDetail.`Female's Name`) AS Enrolled

FROM WorkshopMaster

INNER JOIN WorkshopDetail ON WorkshopMaster.ID = WorkshopDetail.`Workshop ID`

GROUP BY WorkshopMaster.ID


And this is working beautifully thanks to Jane's help on making the empty fields be submitted as NULL. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=12955&image=1&table=forumtopics' class='bbc_emoticon' alt=':)' />
However, I've been asked to make a column on the Master page counting the number of COUPLES. (ie, the records that have both a male and female name entered) I can pull this up using a SQL query like this:

SELECT Count(*) AS Couples FROM `WorkshopDetail` WHERE `Male's Name` IS NOT NULL AND `Female's Name` IS NOT NULL


but I don't see how to work this into the query above, the join stuff messes me up... <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=12955&image=2&table=forumtopics' class='bbc_emoticon' alt=':unsure:' />

K
KevinMillican 10/20/2009



I have two tables, WorkshopMaster and WorkshopDetail. On the Master List Page I'd like to count the number of COUPLES for each workshop.
These workshops are mostly for married couples, but sometimes only one of the couple can show up. Each record in the WorkshopDetail has a field for Male's Name and Female's Name. When only one of the couple shows up, we just enter that info in the approriate field, leaving the other name field blank.
I figured out how to add a field in PHPRunner on the Query tab for the Count() for both these fields. Like this:

SELECT

WorkshopMaster.ID,

WorkshopMaster.`Workshop Fac ID`,

WorkshopMaster.`Workshop Date`,

Count(WorkshopDetail.`Male's Name`) + Count(WorkshopDetail.`Female's Name`) AS Enrolled

FROM WorkshopMaster

INNER JOIN WorkshopDetail ON WorkshopMaster.ID = WorkshopDetail.`Workshop ID`

GROUP BY WorkshopMaster.ID


And this is working beautifully thanks to Jane's help on making the empty fields be submitted as NULL. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=44818&image=1&table=forumreplies' class='bbc_emoticon' alt=':)' />
However, I've been asked to make a column on the Master page counting the number of COUPLES. (ie, the records that have both a male and female name entered) I can pull this up using a SQL query like this:

SELECT Count(*) AS Couples FROM `WorkshopDetail` WHERE `Male's Name` IS NOT NULL AND `Female's Name` IS NOT NULL


but I don't see how to work this into the query above, the join stuff messes me up... <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=44818&image=2&table=forumreplies' class='bbc_emoticon' alt=':unsure:' />


Try
(WorkshopDetail.Male's Name > "") * (WorkshopDetail.Female's Name > "") AS Couple,
in your query, and get the total of the Couple virtual field instead.

phray author 10/21/2009



Try
(WorkshopDetail.Male's Name > "") * (WorkshopDetail.Female's Name > "") AS Couple,
in your query, and get the total of the Couple virtual field instead.


Interesting, but I'm not sure how to interpret the results I'm getting. These are text fields that are submitted as NULL when they're blank so the COUNT() comes out right.
When I do

(WorkshopDetail.`Male's Name` > "") AS Males



I get either a NULL or a 1. Which can't be right, since I have more than one Male in each Workshop. And Multiplying by NULL always returns null...

K
KevinMillican 10/21/2009



Interesting, but I'm not sure how to interpret the results I'm getting. These are text fields that are submitted as NULL when they're blank so the COUNT() comes out right.
When I do

(WorkshopDetail.`Male's Name` > "") AS Males



I get either a NULL or a 1. Which can't be right, since I have more than one Male in each Workshop. And Multiplying by NULL always returns null...


The value will be a 1 if both fields, are filled, or zero otherwise. You can change its properties to display a tick to indicate a couple, and if you tell PHPrunner to summarise that column as a Total, it will show you the number of couples.

phray author 10/21/2009



The value will be a 1 if both fields, are filled, or zero otherwise. You can change its properties to display a tick to indicate a couple, and if you tell PHPrunner to summarise that column as a Total, it will show you the number of couples.



Ah, that would work on the Details page, and I could get a total there.
However, my bosses have changed their minds and want to do something completely different now, so this whole thing is no longer needed. Lovely. <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=44846&image=1&table=forumreplies' class='bbc_emoticon' alt=':wacko:' />