SSRS: Create chart - sql

I want to create a chart in SSRS where the user can input a time range and maybe selects the wanted TrackingIDs for display.
I have a table which has three main columns:
ID,
TrackingID and
TrackingTime
For each ID are TrackingIDs saved with their corresponding TrackingTime. That means that there are multiple rows with the same ID but different TrackingIDs.
| ID | TrackingID | TrackingTime |....
|--------|----------------|-----------------------|
| 001 | 10 |2017-03-08 10:12:20.240|
| 003 | 50 |2017-03-08 12:30:23.240|
| 001 | 10 |2017-03-03 09:10:23.240|
| 002 | 10 |2017-03-06 10:12:23.240|
| 001 | 15 |2017-03-05 10:12:23.240|
| 001 | 20 |2017-03-08 17:12:23.240|
| 002 | 15 |2017-03-04 00:12:23.240|
| 003 | 10 |2017-03-06 01:18:23.240|
....
The user than receives a chart where the sum of each TrackingID is displayed over time. Out of this chart the user can read the change of TrackingIDs over time.
Important is that only the last given TrackingID for an ID is added to the sum of one kind of TrackingID.

You can add two text box parameters to your report that allow user input. I will assume in this case that we are just going to filter between date ranges and not time.
Then go to your table properties and the Filters tab. Add a new Filter.
Expression: Press the fx button to add an expression.
Try this:
CDate(Fields!TrackingTime.Value)
This should convert your DataSet date/time into a VBnet CDate
Operator: > or < depending on which parameter we are comparing to (add one for each)
Value: Press the fx button to add an expression.
Try this:
CDate(Parameters!(YOUR PARAMETER NAME).Value)
This will convert the user input to a VBnet CDate
Your filter should then be able to successfully compare two CDates and determine if they are greater than or less than. Add one for start and end date and you will get your filtered range.
If CDate doesn't work give this function a try:
FormatDateTime(Fields!TrackingTime.Value, DateFormat.ShortDate)
EDIT: If you are doing this for a chart you can add this as a filter in your data element.

Related

Excel VBA Vlookup second most recent data for a value

Currently working on an Excel worksheet with Vlookup. My reference table in Sheet 1 has a list of data along with its date, its name defined as ProjectEntry. For example:
-----------------------------------
| Project No | ID | Service Date |
|------------|----|---------------|
| 01 | A1 | 10/12/17 |
| 02 | B2 | 13/12/17 |
| 01 | A1 | 14/12/17 |
| 03 | C3 | 14/12/17 |
| 01 | A1 | 16/12/17 |
-----------------------------------
Now my Vlookup in Sheet2 wants to lookup the second most recent date based on the ID to get the last service date. For example when I select ID = 01 , Vlookup = 14/12/17.
For the Vlookup formula I managed to get the first entered Service Date (10/12/17):
=VLOOKUP(I7,ProjectEntry[[#All],[ID]:[Service Date]],2,FALSE)
But I'm not sure how to get the 2nd most recent date for A1. What should I add to the formula to make it work?
Instead of seeking the "second-to-last match" we could instead look for the "second highest match", in which case an array formula using the LARGE function will return what you need.
If your example data is arranged in A2:C6 like this:
then you could use this array formula to return the "2nd highest date" where the ID = A1 :
=LARGE(IF($B$2:$B$6="A1",$C$2:$C$6),2)
Your question said you need to look for ID = 01 but those are two different columns. If you instead need to look for `Project No = 01" then your array formula would be :
=LARGE(IF($A$2:$A$6="01",$C$2:$C$6),2)
...that's assuming that Project No is stored as text. If it's actually a number (formatted with a leading zero) then you would use :
=LARGE(IF($A$2:$A$6=1,$C$2:$C$6),2).
☆ Remember that since these are all ARRAY FORMULAS you need to specify that when entering the formulas; instead of using ENTER, finish entering the formula with:
CTRL + SHIFT + ENTER
EDIT:
To clarify whether this method will work, if the data is arranged like this:
...which value should be returned for ID = A1 (or Project No = 1)?
second-to-last (2016-12-17)
second-most-recent? (2014-12-17)

Sum of selected row in Access?

Just a question:
Is it in anyway possible to get a SUM of selected row/cell on the fly in Access form?
I have a db like this:
| ID | PRICE | TAX
| 1 | 20$ | 5$
> | 2 | 30$ | 6$
> | 3 | 40$ | 7$
Would it be possible to get the sum of row SUM(PRICE + TAX) in selected row (2,3).
I am using access 2007 and prefer something as native as possible, without using VBA since it's a networked DB and it's a bit hard to implement VBA in network DB. If there are no native access way of doing it then what's the VBA solution? Could it be possible if I migrate to MSSQL?
You can add a boolean field to mark if the record is selected.
Then use this expression:
SelectedSum: Sum(IIf([Selected]=True,[Price]*[Tax],0))
or, if you prefer:
SelectedSum: Sum([Price]*[Tax]*Abs([Selected]))

SQLAlchemy getting label names out from columns

I want to use the same labels from a SQLAlchemy table, to re-aggregate some data (e.g. I want to iterate through mytable.c to get the column names exactly).
I have some spending data that looks like the following:
| name | region | date | spending |
| John | A | .... | 123 |
| Jack | A | .... | 20 |
| Jill | B | .... | 240 |
I'm then passing it to an existing function we have, that aggregates spending over 2 periods (using a case statement) and groups by region:
grouped table:
| Region | Total (this period) | Total (last period) |
| A | 3048 | 1034 |
| B | 2058 | 900 |
The function returns a SQLAlchemy query object that I can then use subquery() on to re-query e.g.:
subquery = get_aggregated_data(original_table)
region_A_results = session.query(subquery).filter(subquery.c.region = 'A')
I want to then re-aggregate this subquery (summing every column that can be summed, replacing the region column with a string 'other'.
The problem is, if I iterate through subquery.c, I get labels that look like:
anon_1.region
anon_1.sum_this_period
anon_1.sum_last_period
Is there a way to get the textual label from a set of column objects, without the anon_1. prefix? Especially since I feel that the prefix may change depending on how SQLAlchemy decides to generate the query.
Split the name string and take the second part, and if you want to prepare for the chance that the name is not prefixed by the table name, put the code in a try - except block:
for col in subquery.c:
try:
print(col.name.split('.')[1])
except IndexError:
print(col.name)
Also, the result proxy (region_A_results) has a method keys which returns an a list of column names. Again, if you don't need the table names, you can easily get rid of them.

Date Join Query with Calculated Fields

I'm creating an Access 2010 database to replace an old Paradox one. Just now getting to queries, and there is no hiding that I am a new to SQL.
What I am trying to do is set up a query to be used by a graph. The graph's Y axis is to be a simple percentage passed, and the X axis is a certain day. The graph will be created on form load and subsequent new records entered with a date range of "Between Date() And Date()-30" (30 days, rolling).
The database I'm working with can have multiple inspections per day with multiple passes and multiple fails. Each inspection is a separate record.
For instance, on 11/26/2012 there were 7 inspections done; 5 passed and 2 failed, a 71% ((5/7)*100%) acceptance. The "11/26/2012" and "71%" represent a data point on the graph. On 11/27/2012 there were 8 inspections done; 4 passed and 4 failed, a 50% acceptance. Etc.
Here is an example of a query with fields "Date" and "Disposition" of date range "11/26/2012 - 11/27/2012:"
SELECT Inspection.Date, Inspection.Disposition
FROM Inspection
WHERE (((Inspection.Date) Between #11/26/2012# And #11/27/2012#) AND ((Inspection.Disposition)="PASS" Or (Inspection.Disposition)="FAIL"));
Date | Disposition
11/26/2012 | PASS
11/26/2012 | FAIL
11/26/2012 | FAIL
11/26/2012 | PASS
11/26/2012 | PASS
11/26/2012 | PASS
11/26/2012 | PASS
11/27/2012 | PASS
11/27/2012 | PASS
11/27/2012 | FAIL
11/27/2012 | PASS
11/27/2012 | FAIL
11/27/2012 | PASS
11/27/2012 | FAIL
11/27/2012 | FAIL
*NOTE - The date field is of type "Date," and the Disposition field is of type "Text." There are days where no inspections are done, and these days are not to show up on the graph. The inspection disposition can also be listed as "NA," which refers to another type of inspection not to be graphed.
Here is the layout I want to create in another query (again, for brevity, only 2 days in range):
Date | # Insp | # Passed | # Failed | % Acceptance
11/26/2012 | 7 | 5 | 2 | 71
11/27/2012 | 8 | 4 | 4 | 50
What I think needs to be done is some type of join on the record dates themselves and "calculated fields" in the rest of the query results. The problem is
that I haven't found out how to "flatten" the records by date AND maintain a count of the number of inspections and the number passed/failed all in one query. Do I need multiple layered queries for this? I prefer not to store any of the queries as tables as the only use of these numbers is in graphical form.
I was thinking of making new columns in the database to get around the "Disposition" field being Textual by assigning a PASS "1" and a FAIL "0," but this seems like a cop-out. There has to be a way to make this work in SQL, just I haven't found applicable examples.
Thanks for your help! Any input or suggestions are appreciated! Example databases with forms, queries, and graphs are also helpful!
You could group by Date, and then use aggregates like sum and count to calculate statistics for that group:
select Date
, count(*) as [# Insp]
, sum(iif(Disposition = 'PASS',1,0)) as [# Passed]
, sum(iif(Disposition = 'FAIL',1,0)) as [# Failed]
, 100.0 * sum(iif(Disposition = 'PASS',1,0)) / count(*) as [% Acceptance]
from YourTable
where Disposition in ('PASS', 'FAIL')
group by
Date

Excel: filtering a time series graph

I have data that looks like the following:
ID | Location | Attendees | StartDate | EndDate
---------------------------------------------
Event1 | Bldg 1 | 10 | June 1 | June 5
Event2 | Bldg 2 | 15 | June 3 | June 6
Event3 | Bldg 1 | 5 | June 3 | June 10
I'd like to create a time series graph showing, for every given date, how many events were active on that date (i.e. started but haven't ended yet). For example, on June 1, there was 1 active event, and on June 4, there were 4 active events.
This should be simple enough to do by creating a new range where my first column consists of consecutive dates, and the second column consists of formulas like the following (I hardcoded June 8 in this example):
=COUNTIFS(Events[StartDate],"<=6/8/2009", Events[EndDate],">6/8/2009")
However, the challenge is that I'd like to be able to dynamically filter the time series graph based on various criteria. For example, I'd like to be able to quickly switch between seeing the above time series only for events in Bldg 1; or for Events with more than 10 attendees. I have at least 10 different criteria I'd like to be able to filter on.
What is the best way to do this? Does Excel have a built-in way to do this, or should I write the filtering code in VBA?
Apart from that my answer is not programming related: That's prime example for using a pivot table. Use this to show data consolidated for e.g. each day. Then you can play around with filtering as you like.
Your question is exactly what pivot tables are made for.