Results 1 to 6 of 6

Thread: Reg : Counting Rows from Multiple tables ??[RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Resolved Reg : Counting Rows from Multiple tables ??[RESOLVED]

    hi guys,

    i am creating a report for which i am not able to produce a query. 3 tables are involved in this qry and they are salesperson, first_contact and repeated_contact.

    Relationship
    =========
    primary table --> secondary table
    ~~~~~~~~~~~~~~~~~~~~~~
    1. salesperson -->first_contact
    salesperson --> repeated_contact

    2. first_contact --> repeated_contact

    when a salesperson contacts a client(first time) the details will be added in the first_contact table along with his code(salesperson_code). if the same client is contacted again(may be by the same salesperson or by another which is not important at this point), the details of the contact will be added in the repeated_contact table along with his code(salesperson_code).

    now i have to retrieve datewise reports of salesperson(date of contact is also added in both table). that is the number of clients a salesperson has contacted on a particular day. so the data should be retrieved from both the first_contact and repeated_contact tables. (i tried with group and joins, but i could not figure it out clearly). so guys please help me out.

    actually i figured an alternative ie., retrieving data of both the table and putting in a temporary table and from there make the count. but if there is any other way(sql) then i would prefer that.

    -- Kishore..
    Last edited by kishore.kr; Jun 13th, 2005 at 12:16 AM. Reason: Closing the thread...

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Reg : Counting Rows from Multiple tables ??

    either a subquery in the fields bit (one per each table) or subquery in the tables section (not sure which would be best for you)

    1. salesperson -->first_contact
    salesperson --> repeated_contact

    2. first_contact --> repeated_contact
    Something like this may work...
    Code:
    SELECT 
       tblSalesPersons.SalesPersonID, 
       qrySalesToDates.TheDate, 
       qrySubFC.TotalFC, 
       qrySubRC.TotalRC, 
       nz([TotalFC],0)+nz([TotalRC],0) AS TotalFCRC
    FROM 
    ((tblSalesPersons LEFT JOIN 
       (select 
          [tblFirstContacts].[salespersonid], 
          format([updatedon],'yyyymmdd') as [TheDate] 
        from [tblFirstContacts] 
       union 
        select 
          [tblRepeatedContact].[salespersonid], 
          format([updatedon],'yyyymmdd') as [TheDate] 
        from [tblRepeatedContact]) AS qrySalesToDates 
    ON tblSalesPersons.SalesPersonID = qrySalesToDates.salespersonid) 
    LEFT JOIN 
       (select 
          [tblFirstContacts].[salespersonid], 
          format([updatedon],'yyyymmdd') as FCUDate, 
          count([tblFirstContacts].[salespersonid]) as [TotalFC] 
       from [tblFirstContacts]
       group by 
           [tblFirstContacts].[salespersonid], 
           format([updatedon],'yyyymmdd')) AS qrySubFC 
    ON (qrySalesToDates.TheDate = qrySubFC.FCUDate) AND (qrySalesToDates.salespersonid = qrySubFC.salespersonid)) 
    LEFT JOIN 
       (select 
          [tblRepeatedContact].[salespersonid], 
          format([updatedon],'yyyymmdd') as RCUDate, 
          count([tblRepeatedContact].[salespersonid]) as [TotalRC] 
       from [tblRepeatedContact]
       group by [tblRepeatedContact].[salespersonid], format([updatedon],'yyyymmdd')) AS qrySubRC 
    ON (qrySalesToDates.TheDate = qrySubRC.RCUDate) AND (qrySalesToDates.salespersonid = qrySubRC.salespersonid)
    Nasty huh?
    Seems to work though. Might be over the top and probably could be trimmed down to give better performance.

    Edit:
    What I did:
    1) table of sales people
    2) a sub query to union the formatted date and salesID from both the First contact and repeated contact tables (to give a list of dates... may need to enclose this with another query to ensure no duplicate dates...)
    3) a sub query to count the first contacts by date and salespersonid
    4) as (3) but for repeated contact
    5) Use query builder to put it all together
    Last edited by Ecniv; Jun 8th, 2005 at 08:10 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Re: Reg : Counting Rows from Multiple tables ??

    hi ecniv,

    i tried the query but it is not giving exact results. i will try it for somemore time and let you know. thank you for the query.

    -- Kishore...

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Re: Reg : Counting Rows from Multiple tables ??

    hi ecniv,

    i got the concept of your qry and developed it. i got the required result. thanks a lot.
    I am not willing to close the thread bcos., what i got is just the summary. but i need details also so i'll try for that and then close the thread. if i need help then i'll definetely get back to you people. again thanks a lot ..

    -- Kishore...

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Reg : Counting Rows from Multiple tables ??

    No problem.
    It is only one way of getting the results you want, so you may want to think up other methods as a comparison (speed/coding/time taken etc)

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2004
    Location
    Mumbai
    Posts
    236

    Resolved Re: Reg : Counting Rows from Multiple tables ??

    hi guys,
    i got the required result. spl thanks to ecniv

    -- Kishore...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width