I'll separate this post in 3 sections to be as clear as possible. The first one will be informations you need to know. Second one will be the solutions I tried so far to get this working. Third section will be the questions. I also have to precise that's the first time I'm using CrystalReports.
Informations
I'm currently working with Visual Studio 2010 with C# and I'm using SAP Crystal reports. I'm displaying the data using MySQL database.
Now, here are the tables I use to display the report:
Table : orders
Fields : id(primaryKey), date(DATE), quantity(INT), fksizes(INT), fkclients(INT)
Table : sizes
Fields : id(primaryKey), name(VARCHAR(30)), price(FLOAT)
Relationship between these 2 tables : orders.fksizes with sizes.id
Basically, I'm passing a range of dates as parameters to the Crystal Reports to display informations only between those dates. Then, I created 2 parameters : StartDate
and EndDate
both of type Date
.
What I've tried
Here's the original query I'm using to display what I want without the date range condition :
SELECT sizes.name, SUM(orders.quantity) AS totalQty,
(SUM(sizes.price) * orders.quantity) AS totalPrice,
orders.date
FROM orders
INNER JOIN sizes ON orders.fksizes = sizes.id
GROUP BY sizes.name, orders.date
This query works correctly and display the total quantity sold and the total price for each size name. At the report's footer, I'm using a Summary field in which I got the total sum of all totalQty
named sumTotalQty
. I have another field for the same purpose for totalPrice
named sumTotalPrice
.
I have 2 rows of data test which are :
Size name Quantity sold Total Price
------------------------------------------------------------------------------
Big 2 $6.00
XBig 7 $28.00
The field sumTotalQty
displays 9
and sumTotalPrice
displays $34.00
which is exact results. I have to precise that the first row has 2013-10-29
as value for orders.date
and the second one 2013-10-30
.
Now, I want to select a range of dates for which I want to display the results. As an example, I select from 2013-10-30
to today, I should get only the second row with sumTotalQty
displaying 7
and sumTotalPrice
displaying $28.00
. I got the single row correctly displayed, but sumTotalQty
displaying 9
and sumTotalPrice
displaying $34.00
which are incorrect following the date range.
I then tried to add a WHERE clause to my sql query to specify the date range like this (in Database --> Database expert...):
SELECT sizes.name, SUM(orders.quantity) AS totalQty,
(SUM(sizes.price) * orders.quantity) AS totalPrice, orders.date
FROM orders
INNER JOIN sizes ON orders.fksizes = sizes.id
WHERE orders.date BETWEEN '{?StartDate}' AND '{?EndDate}'
GROUP BY sizes.name, orders.date
I got no result displayed with it. So, I think {?StartDate}
and {?EndDate}
are just not set but I'm really not sure. The same goes for WHERE orders.date BETWEEN @StartDate AND @EndDate
.
Questions
- Why aren't
{?StartDate}
and{?EndDate}
set even if I have entered a date range when I'm prompted to give them ? - With the original query (without the WHERE clause), how can I get the right result in my summarize fields with the date range given ? It's like it sums from the database and not from the report fields.
EDIT After many searches, 2 other questions came in my head :
Is it possible to add
DateTimePicker
in the report so the user will be able to enter a starting date and an end date easily ? The report would be refreshed automatically after that.Is there a way to create or use Crystal Reports events (
OnLoad
,AfterLoad
,...) ?
Thank you for all help I'll get.