Mujahid
Mujahid

Reputation: 1237

Get the sum of the 2 row values

My table structure is as follows

**ID**     **datefrom**         **dateto**    **amount**
  1         2011-03-01          2011-03-04      3500
  2         2011-03-05          2011-03-10      2500

I need to search in both rows and get the sum of the amount and number of rows

I need to retrieve all the rows which are between the user input 2 date values, from date and to date. I write a query as follows:

SELECT SUM(amount) as amount, COUNT(amount) as numcount 
FROM rate 
WHERE "2011-03-03" BETWEEN datefrom AND dateto 
AND "2011-03-06" BETWEEN datefrom AND dateto

But its not working as I expected, I need to search all the datefrom and dateto rows to find the given dates and get the number of the rows and the sum of the amount, pls somebody help me with the correct query

Upvotes: 0

Views: 304

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270617

We aren't sure yet what results you are expecting, but should you be using OR instead of AND for the two date ranges?

WHERE "2011-03-03" BETWEEN datefrom AND dateto 
OR "2011-03-06" BETWEEN datefrom AND dateto

Upvotes: 1

Related Questions