Forum Moderators: open

Message Too Old, No Replies

sum a set of records/numbers in mysql

sum a set of records/numbers in mysql

         

kirang

2:09 pm on May 2, 2008 (gmt 0)

10+ Year Member



Hello,

I had table records as :

+-------+--------+--------+
¦urlid1 ¦ urlid2 ¦jobcount¦
+-------+--------+--------+
¦129147 ¦130676 ¦ 305 ¦
¦130676 ¦129147 ¦ 72 ¦
¦130676 ¦129149 ¦ 756 ¦
¦129149 ¦130676 ¦ 30 ¦
+-------+--------+--------+

Now, i need to club the records, where urlid1 = urlid2 and urlid2 = urlid1 and take the job count for those records.

Means that 1st record of urlid1(129147) should search the column urlid2 and also vice versa and finally it should sum the jobcount column.

Expected output:
+-------+--------+--------+
¦urlid1 ¦ urlid2 ¦jobcount¦
+-------+--------+--------+
¦129147 ¦130676 ¦ 377 ¦
¦130676 ¦129149 ¦ 786 ¦
+-------+--------+--------+

I tried several combinations but all in vain.

SELECT a.urlid1, sum( a.jobcount )
FROM test AS a
JOIN test AS b ON a.urlid1 = b.urlid2
AND a.urlid2 = b.urlid1
GROUP BY a.urlid1

any ideas would be great help.

Thanks

ashish21cool

4:38 am on May 3, 2008 (gmt 0)

10+ Year Member



Hi,

You cannot join within 1 table.

Let me know the exact structure of you table.

Thanks

kirang

5:51 am on May 3, 2008 (gmt 0)

10+ Year Member



Hi,

Table structure of table is :

Table name test
1) urlid1 int(2)
2) urlid2 int(2)
3) jobcount int(2)

ashish21cool

9:13 am on May 3, 2008 (gmt 0)

10+ Year Member



According to me you are looking out for this:
You want to sum all jobcount for say urlid1=x and urlid2=x
If above is the case then do the following:
SELECT ulrid1, urlid2,
(SELECT SUM(jobcount) AS jobcount
FROM tp
WHERE (ulrid1 = 129147) OR
(urlid2 = 129147)) AS jobcount
FROM tp AS tp_1
WHERE (ulrid1 = 129147)

This will solve you problem.

Let me know if you get it or not.

All the best