Solved

Count() on Joined Table Items

Posted on 2009-07-08
4
248 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
  • 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 29
how to remove non-duplicated row 5 25
MSSQL 2014 Query Synthax 8 38
Access Date Query 28 29
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now