tags:

views:

62

answers:

2

i have one table with two columns as shown in picture

alt text

at the execution of page user enter the start and end date.

now what i need to calculate is total amount.

for example, if user put

      start date: 18-jan-2010
      end date: 23-jan-2010

then he has 2 dates in first options (20$) and 3 dates in second options (26$) so total will be (56$).

first i want to calculate in sql

1. how many days in first range (if any this depends on supplied dates from user)
2. how many days in 2nd range  (if any this depends on supplied dates from user)
3. how many days in 3rd range  (if any this depends on supplied dates from user)

then

we will multiply days with Amount in php for each range.

at the end i want to get grand total also in php.

Thanks

+2  A: 

Maybe:

SELECT SUM(amount) FROM your_table WHERE date BETWEEN start_date AND end_date GROUP BY user

fabrik
Doesn't sum requires GROUP BY clause?
Ilian Iliev
@Ilian Iliev: sorry, it's edited.
fabrik
but how i get total amount for each section and then grand total ?
air
@Ilian Iliev - You only need a GROUP BY if you're returning more columns than simply the aggregate values... then you need to GROUP BY the non-aggregated columns
Mark Baker
A: 
SELECT 'In Range' AS SumType
       SUM(amount) 
  FROM <table> 
 WHERE date BETWEEN start_date AND end_date
UNION
SELECT 'Outside Range' AS SumType
       SUM(amount) 
  FROM <table> 
 WHERE date < start_date OR date > end_date
UNION
SELECT 'Grand Total' AS SumType
       SUM(amount) 
  FROM <table> 
Mark Baker
dear i did't understand In Range and Outside Range,you saw the table structure, can you please write SQL for me ?Thank you vary much
air
@air - Do you really have a column called "amount us$" in your database table?
Mark Baker
@Mark Baker that called Amount
air
Can you then perhaps show the actual structure of your table, perhaps with some sample data, and the output that you need from that sample data
Mark Baker
dear @Mark Bakerthis is actual structure of table, two column MAXdate (date type), Amount (int)Thanks
air