Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Count() on Joined Table Items

Posted on 2009-07-08
4
Medium Priority
?
257 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 400 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

730 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