Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3486072
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:59:04+00:00 2026-05-18T10:59:04+00:00

I am having a problem trying to JOIN across a total of three tables:

  • 0

I am having a problem trying to JOIN across a total of three tables:

  • Table users: userid, cap (ADSL bandwidth)
  • Table accounting: userid, sessiondate, used bandwidth
  • Table adhoc: userid, date, amount purchased

I want to have 1 query that returns a set of all users, their cap, their used bandwidth for this month and their adhoc purchases for this month:

< TABLE 1 ><TABLE2><TABLE3>
User   | Cap | Adhoc | Used
marius | 3   | 1     | 3.34
bob    | 1   | 2     | 1.15
(simplified)

Here is the query I am working on:

SELECT
        `msi_adsl`.`id`,
        `msi_adsl`.`username`,
        `msi_adsl`.`realm`,
        `msi_adsl`.`cap_size` AS cap,
        SUM(`adsl_adhoc`.`value`) AS adhoc,
        SUM(`radacct`.`AcctInputOctets` + `radacct`.`AcctOutputOctets`) AS used
FROM
        `msi_adsl`
INNER JOIN
        (`radacct`, `adsl_adhoc`)
ON
        (CONCAT(`msi_adsl`.`username`,'@',`msi_adsl`.`realm`) 
           = `radacct`.`UserName` AND `msi_adsl`.`id`=`adsl_adhoc`.`id`)

WHERE
        `canceled` = '0000-00-00'
AND
        `radacct`.`AcctStartTime`
BETWEEN
        '2010-11-01'
AND
        '2010-11-31'
AND
        `adsl_adhoc`.`time`
BETWEEN
        '2010-11-01 00:00:00'
AND
        '2010-11-31 00:00:00'
GROUP BY
        `radacct`.`UserName`, `adsl_adhoc`.`id` LIMIT 10

The query works, but it returns wrong values for both adhoc and used; my guess would be a logical error in my joins, but I can’t see it. Any help is very much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:59:05+00:00Added an answer on May 18, 2026 at 10:59 am

    Your query layout is too spread out for my taste. In particular, the BETWEEN/AND conditions should be on 1 line each, not 5 lines each. I’ve also removed the backticks, though you might need them for the ‘time’ column.

    Since your table layouts don’t match your sample query, it makes life very difficult. However, the table layouts all include a UserID (which is sensible), so I’ve written the query to do the relevant joins using the UserID. As I noted in a comment, if your design makes it necessary to use a CONCAT operation to join two tables, then you have a recipe for a performance disaster. Update your actual schema so that the tables can be joined by UserID, as your table layouts suggest should be possible. Obviously, you can use functions results in joins, but (unless your DBMS supports ‘functional indexes’ and you create appropriate indexes) the DBMS won’t be able to use indexes on the table where the function is evaluated to speed the queries. For a one-off query, that may not matter; for production queries, it often does matter a lot.

    There’s a chance this will do the job you want. Since you are aggregating over two tables, you need the two sub-queries in the FROM clause.

    SELECT u.UserID,
           u.username,
           u.realm,
           u.cap_size AS cap,
           h.AdHoc,
           a.OctetsUsed
      FROM msi_adsl AS u
      JOIN (SELECT UserID, SUM(AcctInputOctets + AcctOutputOctets) AS OctetsUsed
              FROM radact
             WHERE AcctStartTime BETWEEN '2010-11-01' AND '2010-11-31'
             GROUP BY UserID
           )    AS a ON a.UserID = u.UserID
      JOIN (SELECT UserID, SUM(Value) AS AdHoc
              FROM adsl_adhoc
             WHERE time BETWEEN '2010-11-01 00:00:00' AND '2010-11-31 00:00:00'
             GROUP BY UserId
           )    AS h ON h.UserID = u.UserID
     WHERE u.canceled = '0000-00-00'
     LIMIT 10
    

    Each sub-query computes the value of the aggregate for each user over the specified period, generating the UserID and the aggregate value as output columns; the main query then simply pulls the correct user data from the main user table and joins with the aggregate sub-queries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to join three tables together but having a problem getting the
Hi I am having a problem when trying to update a table using an
I am trying to join two tables. One being the sys.databases table and the
Having a problem trying to create a function, as part of a BizTalk helper
I am having a problem trying to use the prependTo() function in jQuery... for
I'm having a problem trying to format the output on the jQuery UI datepicker.
I'm having a problem when trying to pass an array back to a COM
I am having problem that when i am trying to submit the form by
I'm trying to implement a CSS menu and am having a problem with the
I am trying to convert this test code to C# and having a problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.