tags:

views:

45

answers:

3

I am using ms access DB. I need to obtain sale by date. Here is my table specification:

BILL_NO   DATE     SALE
1       8/30/2010   1000
2       8/30/2010   2000
3       8/31/2010   3000
4       8/31/2010   2000

If i want the sale for 8/31/2010 it should return 5000.

I have inserted Date values using java.sql.Date object in DB.

A: 
select
    sum(SALE) as TOTAL_SALE
from
    tbl
where
    DATE='8/31/2010'
group by
    DATE
Björn
A: 
Select sum(SALE) from [your table name] where Format([DATE],"mm/dd/yyyy")='08/30/2010'; 
dave
+2  A: 

Noted should be that DATE is a reserved keyword in MS Access. You need to specify it with braces. Further, you'd like to use SimpleDateFormat to convert a human readable date string to a fullworthy java.util.Date object which you in turn can construct a java.sql.Date with which in turn can be set in the PreparedStatement the usual way.

Here's a kickoff:

String sql = "SELECT SUM(SALE) as TOTAL_SALE FROM tbl WHERE [DATE] = ? GROUP BY [DATE]";
java.util.Date date = new SimpleDateFormat("MM/dd/yyyy").parse("8/31/2010");

Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
int totalSale = 0;

try {
    connection = database.getConnection();
    statement = connection.prepareStatement(sql);
    statement.setDate(new java.sql.Date(date.getTime());
    resultSet = statement.executeQuery();
    if (resultSet.next()) {
        totalSale = resultSet.getInt("TOTAL_SALE");
    }
} finally {
    close(connection, statement, resultSet);
}
BalusC
Its Working Fine...Thanx..Plz can u tell me If I wish to find weekly monthly yearly sale how can i do this?
Pratik
You're welcome. Don't forget to mark the answer accepted. For the new question you should in essence ask a new question, but I'll give you a hint: read up the SQL date/time functions offered by the DB. The MS access specific documentation is available [here](http://www.techonthenet.com/access/functions/). Halfway the page there is a section *Date Functions*. Click your way through it to find information and examples.
BalusC