SSRS table split based on Value keeping headers Constant - c#

I want to break the table as soon the data encounters June. I want only one table which should split the table keeping the table header intact. I searched the whole internet but I am not getting any reliable solution I can go with.
Right now I am getting all the month in a single column, as you can see from the image left side is what I want and right side is what I am getting
I am trying to achieve this in SSRS 2012

If I understand you correctly what you want is the same table to appear displaying information for a time period from July to the following June for each year (so July2015-Jun2016, July2016-June2017…)
If you were to use a dataset in the following format (which looks similar to that posted in your image above
CashFlowYear theDate Revenue Expense
2015 12/01/2015 1000 500
2016 01/01/2016 1100 1500
(Note that the date column where you list a bunch of month names are represented by an actual date here)
You want to create a repeating table, so create a rectangle in your report. Then create your tablix within this rectangle. It should look something like this.
Then to get the grouping behaviour that you desire, right-click the List row header, then Row Group -> Group properties and Group it on
=year(dateAdd("M", -6, Fields!theDate.Value))
This will remove 6 months from each of the dates, and group when they are in the same year (so June 2016 will become December 2015, and be grouped with the other 2015 dates)
When run the report will look like this.
You can then edit the list to insert a page break between each of the tables if you wish, or whatever additional formatting you require.
Hopefully this will assist you. Let me know if I can be of further help.

Related

How to search a date field using wild cards?

I have a date search field in my ASP.Net application where the user enters the date he's looking for and the SQL query looks for the exact date provided.
I'm asked to allow the user to enter the date search using wildcards as follows:
- */12/2015 would mean get all the month of December, 2015
- */*/2015 would mean get all the year of 2015
- */*/* would mean get all the data you got
The only way I could think about to do this is to look in my C# code for '*' in the date, if it exists I will replace the query by a date range based on the position of the *.
This solution doesn't look practical to me, is there an easier way to do it in C# or SQL ?
the solution you found is the only one that makes sense while properly handling the information.
what you've been asked for is to give the users 'something' that allows them to search the dates as text and imho there are 2 possible solution:
the one you found (imho the 'right' one)
store the date as formatted string (BAD, AVOID, NIGHTMARE)
the wildcard approach would be simple to implement should you (wrongly, badly, poorly) store the date information and the format in a text field but this would prevent you from manipulating any date as a date and will put you in huge troubles at the first request that involves date handling (consolidation by month, calculate any rate in a given time span, etc...) or if you happen to have users in different countries (that expect a format different from the one stored into the text field).
implementing the search logic in the frontend the database will store the information using the correct data type and all the manipulation needed to accomplish the 'search the date as text' task are performed by the frontend that will feed to the RDBMS a set of parameters that allows it to handle the date properly.
the result would be as follows:
*/12/2015 --> between '20151201' and '20151231'
*/*/2015 --> between '20150101' and '20151231'
*/*/* --> no filter at all
if stored procedures are in use instead the result would be a couple of date parameters filled with the date values.
I would do as you already suggested and parse the search-string. Then depending on the search-string you can select your data. However you do not have to create dateranges, you can use YEAR and MONTH isntead.
Your SQL-Query could look something like this
SELECT * FROM YourTable WHERE YEAR(YourDate) = 2015 and MONTH(YourDate) = 12
In SQL you can replace * with % and use LIKE. Here is one example:
SELECT *
FROM (
SELECT cast('20150611' as date) as dateColumn
UNION
SELECT cast('20150610' as date)) as X
WHERE convert(varchar, dateColumn, 103) LIKE N'%/06/2015'
Keep in mind that this is practical and not the best solution from performance point of view.

Crystal Reports (C#) : Informations not displaying correctly

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.
Question 1: You either use BETWEEN (? AND ?) or BETWEEN (#StartDate AND #EndDate)
Question 2: You would have to group your data and then use totals in your report.
Edit: After discovering that the issue is with the parameter declaration, I suggest taking a look at some of the following posts:
http://www.dotnetperls.com/sqlparameter
Why do we always prefer using parameters in SQL statements?
Do some more research and you will find lots of info on the net. What I would suggest is to add some input boxes (TextBox, DateTimePicker, etc.) to your page where the user can input the parameter values and then pass them programmatically to the report. That way you can pass the parameters to the SQL statement and get the correct data to your report. You also won't need parameters in your report.
As far as CR events go, there is the Init event, which is public
http://msdn.microsoft.com/en-us/library/aa691447(v=vs.71).aspx
and then there are some Viewer events that could be useful
http://msdn.microsoft.com/en-us/library/ms227097(v=vs.80).aspx

How to arrange this data in columns?

I have a mysql database. In which there are 50 columns of detail.
Detail 1, Detail2, Detail3...... Detail50.
I have the website locally so i am scrapping from myself. The site is not in order no tags and names data is just in form of text line by line, so this was the only option to take what i get line by line and save to DB. So every line gets a column from 1-50....
Some pages have 10 columns other have 50 and the data is in no order now i have the DB,how can i sort them any suggestion ,idea is welcome.
This image will make it more clear:
So You can see Sometimes its Inner Diameter in Detail4 and sometime in 1, these are just examples i would have hard coded but there are too many possibilities, but the repeating words all have the same staring name just values different .Any chance to atleast make 50 % of the data in order the ones with same 4-5 starting words like
part,inner,diameter,oil filter etc.
Any suggestion or ideas can it be done in mysql or C# code.....
Thank you
your approach is totally wrong, but if you want to go this way, just make a table with two columns 'id' and 'details' ... make an insert for each column for the specific product ID.
After that you can use a SELECT like that:
select SUBSTRING(details, 14) from products where details like 'Inner Diameter%' and id = 'my_product_id';

asp.net mvc datetime save to different db table depending on date

I want to save entries to a database table depending on the DateTime entered. I have a different model and partial view for each month in the year. Users can create events, I want the events to be saved to the corresponding month table in the db so I can return it to the correct view.
So I need some sort of if statement that says 'if the month value of the entered DateTime is x save to tabel x, if y save to y' and so on.
The user will navigate from month to month and pick dates from a html table styled like a calendar, so the entries need to be inserted to the section of the calendar that corresponds to the datetime. This is just to explain why I need this functionality.
If someone can reccomend a more elegant and functional method of achieving this, go right ahead!
I'm sorry I don't have code to post, I have tried a number of ways and failed. I will post code tomorrow morning when I'm at my computer, but this seems quite simple and I'm very new to this lark so if someone could shed light now, that would be great.
Thanks in advance!
Don't break it out into 12 separate tables. Store them in a single table. You could create a computed column that tells you the month number by calculating it from the DateTime value you're already storing. If your REALLY need to, you can create 12 views off of this table rather easily but I'd take the approach of adding time parameters to your query's WHERE clause. Make sure you index on the DateTime column.

How to insert data into the right calendar cells?

I have a calendar that I generate. Currently it makes the entire month and fills each cell with a number(representing the date).
Now I want to grab values from a database and fill in the cells. How could I do this efficiently?
Like right now I can only think of grabbing the data from the database. Once that is down going through that data and essentially have like 30 if statements to determine what cell it should go into.
So that just seems like a very bad way and I am thinking of better ways. So I am wondering anyone else has any ideas.
I am using asp.net mvc I generate the body of the calendar(what is just a table) through my controller and pass it as just a string of html cells and rows.
So basically I generate in the controller all 6 rows of 7 cells(42 cells with 2 cells for previous month and remaining cells for the next month - basically looks like the windows 7 calendar) with the TagBuilder and return that as one big string.
So while building the cells that's what I would have to put the if statements to do the checking.
I am using linq to sql by the way so not sure if that will help or not.
Edit
Another way what I was thinking but not sure how to do it. Would be some how getting all the dates in range. Then take those results and do some grouping on those results. Not sure how to that kind of grouping though. It probably would not be to bad if I do the grouping on the first results and not do a request for each date and then group that. Otherwise I am looking at like 42 requests to the database to group everything.
You're having to loop anyway, to build the rows and columns I assume, so why not pull the data down first, for that month, put the data into an array (old fashioned I know), and check the offset in that array as you increment through the cell rendering?

Categories