473,503 Members | 1,669 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sum problem

32 New Member
i have two tables
one table contain the basic information about the users.
second table contain amount information of users.
i want to sum of amount of users Country vise. i have country id in users table.
but in second table have the user id and amount information.
how i can get the sum of users amount country vise.
if any one have idea then please help me.
Jun 11 '07 #1
14 2086
Purple
404 Recognized Expert Contributor
Hi,

You refer to tables, is it safe to assume there is a database of some sort ? Can you give a little more info on what environment you are running plse

Purple
Jun 11 '07 #2
ak1dnar
1,584 Recognized Expert Top Contributor
i have two tables
one table contain the basic information about the users.
second table contain amount information of users.
i want to sum of amount of users Country vise. i have country id in users table.
but in second table have the user id and amount information.
how i can get the sum of users amount country vise.
if any one have idea then please help me.
if this user id is repeating in both the tables you can make it.

userinfo table

user_id - userName - user_country_id
1001 - Mike - 5
1002 - Sam - 2
1003 - Jane - 5

amountinfo table

amt_id - user_id - amount
1 - 1001 - 100
2 - 1002 - 150
3 - 1003 -250

Expand|Select|Wrap|Line Numbers
  1. SELECT sum(amount) 
  2. FROM amountinfo
  3. LEFT JOIN userinfo ON amountinfo.user_id = userinfo.user_id
  4. WHERE userinfo.user_country_id = '5'
Jun 11 '07 #3
rahia307
32 New Member
hi
first of all thanks for reply.
now i tell u the structure of data base.
basically i have three tables.
1. country
this table contain the following fields.
id_zone, country_name
2. user_info.
this table contain the following fields.
id, user_name, id_zone
3. amount.
this table contain the following fields.
id, id_user, amount
amount table contain multiple amount of id_user
now i want to get the sum of amount of users country vise .
help me about this problem.
i will be thanks full .
Jun 11 '07 #4
Purple
404 Recognized Expert Contributor
Hi,

this is the SQL based on MSSQL - not sure if it will work on other RDBMS platforms - which is why I asked what the environment was on an previous post..

Expand|Select|Wrap|Line Numbers
  1. SELECT     SUM(amount.amount) AS sum_amount, country.country
  2. FROM         amount INNER JOIN
  3.                       user_info ON amount.id_user = user_info.id INNER JOIN
  4.                       country ON user_info.id_zone = country.id_zone
  5. GROUP BY country.country
Regards Purple
Jun 11 '07 #5
rahia307
32 New Member
Hi,

this is the SQL based on MSSQL - not sure if it will work on other RDBMS platforms - which is why I asked what the environment was on an previous post..

Expand|Select|Wrap|Line Numbers
  1. SELECT     SUM(amount.amount) AS sum_amount, country.country
  2. FROM         amount INNER JOIN
  3.                       user_info ON amount.id_user = user_info.id INNER JOIN
  4.                       country ON user_info.id_zone = country.id_zone
  5. GROUP BY country.country
Regards Purple
hi
thanks but it not work properly .
Jun 12 '07 #6
ak1dnar
1,584 Recognized Expert Top Contributor
Post your SQL queries for table structures with some sample values.

NOTE : As purple asked what is the SQL environment that you have used for this.
(MySQL,MsSQL,etc)
Jun 12 '07 #7
rahia307
32 New Member
hi
i use mysql database
and send a sample of query
$sql =mysql_query("SELECT *
FROM $tbl_users u, $tbl_gd_country c
WHERE u.id_zone=c.id_zone
GROUP BY u.id_zone
ORDER BY c.id_zone ");
$j=0;
while($result = mysql_fetch_array($sql)) {
$sql_quran = "SELECT sum(amount)
FROM $tbl_amount m,$tbl_users u
WHERE m.id_user='$result[id]'
AND u.id=m.id_user
";
one thing i tell here i have multiple entry of a same id_user in amount table
Jun 12 '07 #8
rahia307
32 New Member
hi
i use mysql database
and send a sample of query
$sql =mysql_query("SELECT *
FROM $tbl_users u, $tbl_gd_country c
WHERE u.id_zone=c.id_zone
GROUP BY u.id_zone
ORDER BY c.id_zone ");
$j=0;
while($result = mysql_fetch_array($sql)) {
$sql_quran = "SELECT sum(amount)
FROM $tbl_amount m,$tbl_users u
WHERE m.id_user='$result[id]'
AND u.id=m.id_user
";
one thing i tell here i have multiple entry of a same id_user in amount table
Jun 12 '07 #9
Purple
404 Recognized Expert Contributor
misread previous post
Jun 12 '07 #10
Purple
404 Recognized Expert Contributor
Hi,

Having looked at your code can we just pause for a moment and review what it is your are trying to get as a results set - my 'guess' is amount by user by country ?

are you also using the array returned from the first SQL query - is there are reason you have not done this as one SQL query ?

Apologies for the numerous questions but if we clarify at the start we will all save time :)

Purple
Jun 12 '07 #11
ak1dnar
1,584 Recognized Expert Top Contributor
hi
i use mysql database
and send a sample of query
$sql =mysql_query("SELECT *
FROM $tbl_users u, $tbl_gd_country c
WHERE u.id_zone=c.id_zone
GROUP BY u.id_zone
ORDER BY c.id_zone ");
$j=0;
while($result = mysql_fetch_array($sql)) {
$sql_quran = "SELECT sum(amount)
FROM $tbl_amount m,$tbl_users u
WHERE m.id_user='$result[id]'
AND u.id=m.id_user
";
one thing i tell here i have multiple entry of a same id_user in amount table
I requested the SQL query for table structure i think.

example:

Expand|Select|Wrap|Line Numbers
  1. -- 
  2. -- Table structure for table `users`
  3. -- 
  4.  
  5. CREATE TABLE `users` (
  6.   `id` int(10) NOT NULL auto_increment,
  7.   `region` varchar(250) default NULL,
  8.   `staffid` varchar(250) default NULL,
  9.   `firstname` varchar(250) default NULL,
  10.   `surname` varchar(250) default NULL,
  11.   PRIMARY KEY  (`id`)
  12. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
  13.  
  14. -- 
  15. -- Dumping data for table `users`
  16. -- 
  17.  
  18. INSERT INTO `users` VALUES (1, 'region1', 'staff1', 'Mike', 'Conors');
  19. INSERT INTO `users` VALUES (2, 'region2', 'staff2', 'Akon', 'white');
  20.  
  21.  
If you using phpMyAdmin export them for the current tables.
Jun 12 '07 #12
rahia307
32 New Member
hi
i cannot understand this
plz help me
Jun 12 '07 #13
ak1dnar
1,584 Recognized Expert Top Contributor
hi
i cannot understand this
plz help me
Read these posts carefully !
http://www.thescripts.com/forum/post2625796-7.html
http://www.thescripts.com/forum/post2626374-12.html

Do you have set up the MySQL tables, then can i get the Structured Query language that you used to create those tables. with Insert statement for couple of records.

I clearly mentioned what i need, by giving this example SQL query for a sample table structure.
How may i help you if you are not reading the posts carefully.
Jun 12 '07 #14
Purple
404 Recognized Expert Contributor
Hi,

or you could try replacing both of your queries with :

Expand|Select|Wrap|Line Numbers
  1. SELECT     SUM(amount.amount) AS amount, user_info.user_name, user_info.id_zone, country.id_zone AS country_zone, country.country
  2. FROM         user_info INNER JOIN
  3.                       amount ON user_info.id = amount.id_user INNER JOIN
  4.                       country ON user_info.id_zone = country.id_zone
  5. GROUP BY user_info.id, user_info.user_name, user_info.id_zone, country.id_zone, country.country
  6. ORDER BY user_info.id_zone
If that doesn't do it for you, follow Ajaxrands suggestion and I am sure we can get through this...

Purple
Jun 12 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

11
3733
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
117
7102
by: Peter Olcott | last post by:
www.halting-problem.com
18
6138
by: Ian Stanley | last post by:
Hi, Continuing my strcat segmentation fault posting- I have a problem which occurs when appending two sting literals using strcat. I have tried to fix it by writing my own function that does the...
28
5170
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
3785
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
4870
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
4530
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
0
2929
by: =?Utf-8?B?am8uZWw=?= | last post by:
Hello All, I am developing an Input Methop (IM) for PocketPC / Windows Mobile (PPC/WM). On some devices the IM will not start. The IM appears in the IM-List but when it is selected from the...
1
5100
by: sherifbk | last post by:
Problem description ============== - I have 4 clients and 1 server (SQL server) - 3 clients are Monitoring console 1 client is operation console - Monitoring console collects some data from...
9
3621
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.