Jump to content

Combining 2 tables and outputting top 5 results


beanymanuk

Recommended Posts

Hi

 

I am trying to combine 2 tables and show the 5 highest results

 

SELECT email, total_amount FROM cashsumtotals WHERE cause = '$causename'

AND

SELECT email, total_amount FROM cardsumtotals WHERE cause = '$cause_id_get'

 

I need to get the total SUM of total_amount where email address is the same and ORDER results by total_amount DESC and display the top 5 results

 

Any help would be muchly appreciated.

Link to comment
Share on other sites

Hi

 

Also a touch confused, but if you did mean 2 items from the same table something like this:-

 

SELECT email, SUM(total_amount) 
FROM cashsumtotals 
WHERE cause = '$causename' 
OR cause = '$cause_id_get'
GROUP BY email
ORDER BY SUM(total_amount) DESC
LIMIT 5

 

All the best

 

Keith

Link to comment
Share on other sites

I have 2 tables though?

 

cashsumtotals

AND

cardsumtotals

 

Eg if have

 

in cardsumtotals

email                              total_amount

Dan@???.com                          16.66

 

in cashsumtotals

email                              total_amount

Dan@???.com                         10

lisa.evans@?????.com              10

 

result to be

 

email                                  total_amount

Dan@???.com                              26.66

lisa.evans@???.com                    10

Link to comment
Share on other sites

Found a solution in the end

Is this the most efficient way of doing it though?

 

SELECT email, SUM(sumAmount1) as total
FROM (
  SELECT email, SUM(total_amount) as sumAmount1
      FROM cashsumtotals
      WHERE cause= '$causename'
      GROUP BY email
  UNION ALL SELECT email, SUM(total_amount)
      FROM cardsumtotals
      WHERE cause = '$cause_id_get'
      GROUP BY email
  ) tmp
GROUP BY email

Link to comment
Share on other sites

Hi

 

Ah, missed the slightly different names.

 

Doesn't seem to be any point to summing things separately, so try the following.

 

SELECT email, SUM(total_amount) as total
FROM (
  SELECT email, total_amount
      FROM cashsumtotals
      WHERE cause= '$causename'
  UNION ALL SELECT email, total_amount
      FROM cardsumtotals
      WHERE cause = '$cause_id_get'
  ) tmp
GROUP BY email 

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Or if you have a table of people then maybe this (ie a table that the email address links to), although not tested and not sure that adding the 2 SUMs together will work OK.

 

SELECT a.email, SUM(b.total_amount) + SUM(c.total_amount)
FROM People a
LEFT OUTER JOIN cashsumtotals b ON a.email = b.email AND b.cause= '$causename'
LEFT OUTER JOIN cardsumtotals c ON a.email = c.email AND c.cause = '$cause_id_get'
GROUP BY a.email
ORDER BY SUM(b.total_amount) + SUM(c.total_amount) DESC
LIMIT 5

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

How can I show the total_amount for each of the Select queries as well as the name, email, SUM(total_amount) that it already displays.

 

SELECT name, email, SUM(total_amount) as total
FROM (
  SELECT name, email, total_amount
      FROM cashsumtotals
      WHERE cause= '$causename'
  UNION ALL SELECT name, email, total_amount
      FROM cardsumtotals
      WHERE cause = '$cause_id_get'
  ) tmp
GROUP BY email ORDER BY total DESC LIMIT 6

Link to comment
Share on other sites

Hi

 

In which case use a subselect to get one:-

 

SELECT a.email, SUM(b.total_amount) + SUM(c.total_amount)
FROM (SELECT DISTINCT email FROM  cashsumtotals UNION SELECT DISTINCT email FROM  cardsumtotals ) AS a
LEFT OUTER JOIN cashsumtotals b ON a.email = b.email AND b.cause= '$causename'
LEFT OUTER JOIN cardsumtotals c ON a.email = c.email AND c.cause = '$cause_id_get'
GROUP BY a.email
ORDER BY SUM(b.total_amount) + SUM(c.total_amount) DESC
LIMIT 5 

 

All the best

 

Keith

Link to comment
Share on other sites

SELECT name, email, SUM(total_amount) as total
FROM (
  SELECT name, email, total_amount
      FROM cashsumtotals
      WHERE cause= '1st Lawford Scout Group'
  UNION ALL SELECT name, email, total_amount
      FROM cardsumtotals
      WHERE cause = '53'
  ) tmp
GROUP BY email ORDER BY total DESC LIMIT 6

 

pull out these results

 

name email total

Daniel A Dan@griffin.co.uk 16.66

Peter G hero1@awebsite.com 5

 

The 16.66 in this example is made up from just cardsumtotals

The 5 in this example is made up from just cashsumtotals

But I want to be able to to show how much some from cardsumtotals and how much come from cashsumtotals for each email.

Link to comment
Share on other sites

SELECT a.email, SUM(b.total_amount), SUM(c.total_amount)
FROM (SELECT DISTINCT email FROM  cashsumtotals UNION SELECT DISTINCT email FROM  cardsumtotals ) AS a
LEFT OUTER JOIN cashsumtotals b ON a.email = b.email AND b.cause= '1st Lawford Scout Group'
LEFT OUTER JOIN cardsumtotals c ON a.email = c.email AND c.cause = '53'
GROUP BY a.email
ORDER BY SUM(b.total_amount) + SUM(c.total_amount) DESC
LIMIT 5 

 

Result

testemail2@ifaw.com NULL NULL

conniebushby@aol.com NULL NULL

jackielovesbrian@hotmail.co.uk NULL NULL

ronniebartlett@mail.com NULL NULL

simone@coolcatpr.com NULL NULL

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.