Solved

Count() on Joined Table Items

Posted on 2009-07-08
4
252 Views
Last Modified: 2012-05-07
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?
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

Open in new window

0
Comment
Question by:customertrax
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:pssandhu
ID: 24808612
A quick observation, your following code may not work properly:
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, ','))

CREATE FUNCTION [dbo].[ParseVal]
   (@RepParam nvarchar(4000), @Delim char(1)= ',')
RETURNS @Values TABLE (RecordID int, Param nvarchar(4000))AS
  BEGIN
  DECLARE @chrind INT
  DECLARE @id INT	
  DECLARE @Piece nvarchar(50)
  SELECT @chrind = 1
  SELECT @id = 0 
  WHILE @chrind > 0
    BEGIN
      SET @id = @id + 1	
      SELECT @chrind = CHARINDEX(@Delim,@RepParam)
      IF @chrind  > 0
        SELECT @Piece = LEFT(@RepParam,@chrind - 1)
      ELSE
        SELECT @Piece = @RepParam
      INSERT  @Values(RecordID, Param) VALUES(@id, @Piece)
      SELECT @RepParam = RIGHT(@RepParam,LEN(@RepParam) - @chrind)
      IF LEN(@RepParam) = 0 BREAK
    END
  RETURN
END

Open in new window

0
 
LVL 1

Author Comment

by:customertrax
ID: 24809322
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?
0
 
LVL 17

Assisted Solution

by:pssandhu
pssandhu earned 100 total points
ID: 24813905
Try runnig the subquery by itself to eliminate possibilities subquery being wrong.
P.
0
 
LVL 1

Accepted Solution

by:
customertrax earned 0 total points
ID: 24817266
Thanks for the advice...I found out that my subquery actually wasn't producing the correct results.  I finally got my query to work by doing the following:
SELECT c.contact,
(select count(wce_history.uniqueid) as callcount from wce_history inner join wce_linkto l2 on (wce_history.uniqueid = l2.luniqueid)
inner join wce_contact c2 on (l2.lentityid = c2.uniqueid) 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 and c2.uniqueid = c.uniqueid) as 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 wce_history on (l.luniqueid = wce_history.uniqueid)
left outer join equipsold e on (c.accountnumber = convert(varchar(10),e.companyno))
 
WHERE c.recordmanager IN (@parm_user)
 
GROUP BY c.contact,
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

Open in new window

0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent question popped up and the discussion heated up regarding updating a COMMENTS (TXT) field in a table using SSRS. http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Q_27475269.html?cid=1572#a37227028 (htt…
It is helpful to note: This is a cosmetic update and is not required, but should help your reports look better for your boss.  This issue has manifested itself in SSRS version 3.0 is where I have seen this behavior in.  And this behavior is only see…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question