customertrax
asked on
Count() on Joined Table Items
I need help with the following issue:
In the query below, I have table C outer joined with tables H and E. The results of my query need to contain a separate row for each E item that is returned. for example, if a particular contact (Bob) from table C is linked to 6 items in table E, then 6 rows would be returned for Bob. I also need to have a "CallCount" column that contains a count of the number of items in table H that are linked to Bob (ie. 2). For example, the results of my query should contain the following rows for Bob:
Bob 2 6/1/09 Ford Focus
Bob 2 6/7/09 Chev Malibu
Bob 2 6/9/09 Honda Civic
Bob 2 7/2/09 Toyota Camry
Bob 2 7/6/09 Honda Accord
Bob 2 7/8/09 Honda CRV
The problem I am having is that the H.CallCount value is not showing up correctly. I am either seeing a 1 or a blank in that column. I have tried using "h.CallCount" and "count(h.uniqueid) as CallCount" in my select statement, but neither of these methods produce the correct number.
Also, I am having problems with additional rows showing up for the E table items that don't fall within my date parameters. All of these extra items are being returned with a blank DateEntered, blank Make, and blank Model, and are getting grouped together as one extra row per c.contact. I would like to eliminate that extra row if possible.
My main concern is getting the CallCount to work. Eliminating the extra blank rows would be an added bonus. Any ideas?
In the query below, I have table C outer joined with tables H and E. The results of my query need to contain a separate row for each E item that is returned. for example, if a particular contact (Bob) from table C is linked to 6 items in table E, then 6 rows would be returned for Bob. I also need to have a "CallCount" column that contains a count of the number of items in table H that are linked to Bob (ie. 2). For example, the results of my query should contain the following rows for Bob:
Bob 2 6/1/09 Ford Focus
Bob 2 6/7/09 Chev Malibu
Bob 2 6/9/09 Honda Civic
Bob 2 7/2/09 Toyota Camry
Bob 2 7/6/09 Honda Accord
Bob 2 7/8/09 Honda CRV
The problem I am having is that the H.CallCount value is not showing up correctly. I am either seeing a 1 or a blank in that column. I have tried using "h.CallCount" and "count(h.uniqueid) as CallCount" in my select statement, but neither of these methods produce the correct number.
Also, I am having problems with additional rows showing up for the E table items that don't fall within my date parameters. All of these extra items are being returned with a blank DateEntered, blank Make, and blank Model, and are getting grouped together as one extra row per c.contact. I would like to eliminate that extra row if possible.
My main concern is getting the CallCount to work. Eliminating the extra blank rows would be an added bonus. Any ideas?
SELECT c.contact, h.CallCount,
case
when (e.dateentered >= @parm_begindate AND e.dateentered <= @parm_enddate) then e.dateentered end as DateEntered,
case
when(e.dateentered >= @parm_begindate AND e.dateentered <= @parm_enddate) then e.make end as Make,
case
when (e.dateentered >= @parm_begindate AND e.dateentered <= @parm_enddate) then e.model end as Model
FROM wce_contact c
left outer join wce_linkto l on (c.uniqueid = l.lentityid)
left outer join (select uniqueid, count(uniqueid) as callcount from wce_history where wce_history.recordedfor IN (@parm_user) and wce_history.htype = 'Call Completed' and wce_history.recordedtime >= @parm_begindate AND wce_history.recordedtime <= @parm_enddate group by uniqueid) as h
on (l.luniqueid = h.uniqueid)
left outer join equipsold e on (c.accountnumber = convert(varchar(10),e.companyno))
WHERE c.recordmanager IN (@parm_user)
GROUP BY c.contact, h.CallCount,
case
when (e.dateentered >= @parm_begindate AND e.dateentered <= @parm_enddate) then e.dateentered end,
case
when(e.dateentered >= @parm_begindate AND e.dateentered <= @parm_enddate) then e.make end,
case
when (e.dateentered >= @parm_begindate AND e.dateentered <= @parm_enddate) then e.model end
ORDER BY c.contact
ASKER
During my testing I have only been selecting 1 single value from the multiselect dropdown for @parm_user, and the problem still occurs. So yes, the problem is still occurring when only 1 value is passed. Any idea why?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
wce_history.recordedfor IN (@parm_user)
that is because if you stored multiple names in the @parm_user seperated by comma (or any other delimiter) then you wont' be able to run your query correctly. It will only work if you pass just one value. Thatis because when the values are passed into a variable it is being treated as one big value even thougn you have them seperated by commas. What you need here here is a finction that will parse the string into values and return them in for for a list.
Copy the function from the code snippet below and create the function. Then use the function yuo just created to parse values. Like so:
wce_history.recordedfor IN (Select Param From dbo.ParseVal(@parm_user, ','))
Open in new window