Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

count records when another ecord exists

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
Hi - am struggling making this work

Have a list of users and a count when a record action.typeid = 1 and a response.reasonid LIKE appointment

IF

a record with action.typeid = 2 exists with actiondate between two dates

the two action records are linked by table Event
all dealcount show as "1" however if i run the sub-query
on its own - the totals are shown

so far...

Code:
SELECT 
  users.userid,
  users.userf,
  users.userl,
  COALESCE(dealcount.dealcount,0) AS dealcount 
FROM
  users  
  LEFT JOIN 
    (SELECT 
      COUNT(
        CASE
          WHEN UCASE(response.reasonid) LIKE '%APPOINTMENT%' 
          THEN action.actionid 
        END
      ) AS dealcount,
      response.createuserid 
    FROM
      EVENT 
      LEFT JOIN ACTION 
        ON action.eventid = event.eventid 
      INNER JOIN response 
        ON action.actionid = response.actionid 
    GROUP BY event.eventid 
    HAVING COUNT(
        CASE
          WHEN action.typeid = '2' 
          AND DATE(action.actiondate) BETWEEN '2012-01-01' 
          AND '2012-01-31' 
          THEN 'count' 
        END
      ) > 0) AS dealcount 
    ON dealcount.createuserid = users.userid 
GROUP BY users.userid 
ORDER BY dealcount.dealcount DESC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top