SSRS - Get the max time for the max date from a list of dates - reporting-services

I have an SSRS report with a dataset that queries AS400. I am trying to print a "Version number" on the top of my report. Due to several complications, I cannot really print a version number better than last update time.
This is my what my data looks like:
The query takes a parameter that is a PackingDate. As in, the ShopOrders were written to pack on this date.
The query returns anywhere from 10-25 shop orders for each PackingDate.
Each of these shop orders have 4 columns: DateCreated, TimeCreated, DateModified, TimeModified.
Shop orders go through changes and revisions frequently. Every time a shop order is changed, the DateModified, TimeModified field changes.
I want to look at each of these shop orders, look at the DateModified, get the maximum date, then look at the TimeModified, get the maximum time, and add a concatenated form of that as the version number on top of my report. For instance:
Date fields are in yyyyMMdd format and time fields are in mmhhss format.
ShopOrder: 65642
DateModified: 20180118
TimeModified: 124500
ShopOrder: 65643
DateModified: 20180117
TimeModified: 142000
Since the MAX(DateModified) in these two shop orders is 20170118, I want the TimeModified for that corresponding date: 124500.
So the version number would look like this: v0118.1245.
I would like to, if possible, have this done in SSRS and not have to do much in my dataset, but that is not written in stone. I just want the MAX(Time) for THE MAX(Date).
EDIT 1:
This is what I've already tried:
LOOKUP(MAX(Fields!DateModified.Value), Fields!DateModified.Value, MAX(Fields!TimeModified.Value), "ShopOrders")
I was pretty proud of myself for thinking of this, but that burned down quickly when I got an error that said I cannot use Aggregate functions in Lookup.

I'm not sure if you can use lookups in this case although I could be very wrong as I don't use lookups enough to know their limitations.
The way I would approach it would be to simply add a new column in your query results that combines your date and time columns. Then you could simply get the Max of that new column.

Related

Can I replicate this equation in excel through subqueries? I started differentiating between purchases and sales but now I'm stuck

just started learning sql last week and am trying to use a table imported using .csv to then carry out functions and add columns in the final table that perform these equations.
I am tasked with the first equation which is Taking the ((Purchases - Shares)/ (Purchases + sales), for a given owner, for a given year.
I have started a query that asks to look at transcode column and identify if it is P for Purchase, S for Sale and then from there I want to look at the shares column to then take that amount and plug it into the equation.
I am unsure if what I am trying to do is feasible?
Should I break it down further into seperate tables for purchases and shares?
Thank you.
[Picture of My data table labeled IHD in MySQL workebnch]
[MySQL Workbench to give you idea of code I wrote to distinguish Sales from purchases]
You can try something like :
select sum(shares) as num_of_shares , transcode, owner, YEAR(year) from table group by owner, transcode, YEAR(year)
This should give you aggregate of number of shares for sales and purchase per owner per year. After you can plug in this information into other queries to get the desired result.

office access substrac values from each row in one field

Please help, how to substract values in one field from one row to another, for example:
I want it become like this:
This Question is not very clear but I've given what you have asked for.
This will place the first total in the row with NO = 1 and then will reduce that total as NO increases.
(Select Total from table1 where Date1 = (Select Min(NO) from Table1))-IIf([No]=1,0,(DSum("[Total]","[Table1]","[NO]<=" & [NO] & " AND [NO]<>1")))
Add this as the last column in a query on your table. You will have to replace "Table1" with your table name and you should really change the column names to be something different than NO and Date as these are used for other things in Access.
The way you have data stored could make this impossible because there is no way to distinguish which records should be subtracted from which records. That is assuming there will be more than one record considered a positive (increase) entry. Options:
Another field that identifies record as either Increase or Decrease
Enter the values to be subtracted as negatives
Regardless of which you choose, a report is the easiest way to show a running balance because textbox on report has RunningSum property.
But if these records are not incoming and outgoing transactions and what you really want is for each record to get value from previous and subtract, then review Stackoverlow - get previous value
Why do you name fields with Kmh/mph, those values are obviously not Kmh/mph, they look like odometer readings. Better field names may be OldOdometer, NewOdometer. However odometer readings would increase, not decrease. Why do you need to add new to old? Just what is this data?

Architecture: Ordering in Rails or MySQL

I have an AngularJS front-end app that sends requests to a Rails API back-end. When a user search for items, so far the query is limited to 20 elements and is always ordered by popularity (a field stored in database). After the results are retrieved from DB, there is a complex process that calculates the item prices iterating one by one (remember only 20 elements). After that, the results are served to the user. Note: As told, item prices cannot be calculated directly in the query because an item can have different prices according to dates and also discounts can be applied.
This is how it´s working so far.
Now, I would like to introduce in the search results page an innocent order by: Price functionality. So, the array of items should come ordered by price.
As, I can´t get the prices directly with just one query, I see two choices:
To keep it as it is right now, I mean, making the query ordered by popularity and order the results after the prices are calculated. But I see a problem, If I get 20 elements each time ordered by popularity, then I can calculate prices and order by price these 20 elements, so I assume I´m not ordering correctly by price. This case, I would need to query without limit, to get all items, calculate prices, order them by price and return to user. I think I would also to develop a home-made pagination functionality.
Develop some kind of stored procedure in the database to provide the results with the complex prices calculations. I don´t know if I can order them easily. I´m worried because I don´t know stored procedures in MySQL and not sure if it´s possible to do what I need.
But, from the performance point of view, I guess the second choice should be better, right? I´d appreciate comments or any other options?
UPDATE:
According to comments, I detail how to calculate prices functionality.
A user can rent an item for many days (a week i.e.). So, there is a check-in and check-out dates.
Also, prices changes according to seasons. This means days can have different prices in a selected week. So, in order to calculate the total price, you have to get the daily price matching each selected day in the week and add it to the total price.
Once, the base price is calculated, there can be discounts. Same as prices, discounts can be applied only for some days, so first, it must be checked if there is any discount for the selected week. If so, the discount is applied to base price to get the total final price.
Please, let me know if you need the code.
This should be done on the backend (MySQL).
Like you mentioned, if you want to sort by price on the front-end, you'll have to replicate the entire database into Angular. Which is probably a bad idea.
This is the approach I would take in MySQL:
Set up a table with item_id, date, and price (or perform the joins necessary to get this table).
Apply discounts for each date. This step yields an interim table with updated prices.
Build your final query. Your SELECT clause should SUM(price). You should GROUP BY item_id. And you should ORDER BY SUM(price) DESC.
Depending on the size of your database, this query may require a lot of fine-tuning in order to return results quickly. But it definitely can (and should) be done in the backend.
Good luck!
EDIT: With a really big set of items, running this query through MySQL may become too slow, regardless of how much time you spend tuning performance. If MySQL doesn't cut it, you may need to rely on an auxiliary database like Elasticsearch.
But before turning to Elasticsearch/Hadoop/etc, you should think carefully about how "complex" your pricing algorithm really is. In all likelihood you can optimize the MySQL query to the point where it performs just fine.
This is a classical search listing problem. If you want to perform sort based on price, obviously you need the required inputs. it is worth mentioning that stored procedure won't magically improve the performance. The best what you can do is run recurring CRON job every hour/day ( bonus tip : use upset and perform mass operation) which updates the price. Now perform join and do the query and order it with "price". Also make sure to put indexes on required fields. After grabbing the result apply discount and show the result. If query is complex the pagination won't work. You have to do all sorts of operation manually. If you are using will_paginate , it has some function which also support Array instead of active record. Hope this helps.
So you have the following factors then:
User Inputs Beginning Date, End Date and sees list of items.
The Beginning - End Date period (selected period) is consistent for all items in the query.
To sort by price you must, for each item in the table (not just those retrieved)
Get Item Price for each day of the selected period.
Get available discounts for each day of the selected period.
Calculate Total Price for selected period.
Because user input is involved you will not be able to definitively calculate this and store the final product. The best you could do is calculate the gross and net prices per day and cache that, then performing the final calculation based on user input (or sending that data to browser for calculation there)
You could also use a first order approximation and just go with the current gross value, and then tweak from there. That assumes that the relationship between item prices remains more or less consistent.

query to aggregat data by day to generate charts - rows unkown

I started building a search engine monitor. I'm pulling data from the google rest api into a mysql database with the following fields: date, search-keyword, domain, url, position.
Now I got into trouble querying and outputting the data for charting. The results go up and down, new results from google come into the list which haven't been there on the first day. However for charting I have to assign the first days at least blank values to output a chart.
What I do right now: First I select every domain showing up in the period. Lets say the for the keyword searchengine I get the domains wikipedia.org, ixquick.com, yahoo.com, searchenginewatch.com When I make another request for ever domain to query an array of rankings grouped by day. leading to the ...
Problem: Is where any query (mysql/nosql) which returns for each day an average and if where is no row a default value e.g. blank?
Result should look like:
dates={01/01/2014,02,03,04,05,06,07,08,...,31}
wikipedie={1,1,1,1,1,1,1,1,...,1}
yahoo = {"","",7,5,3,3,3,...,3}
You can create a date table, select the date range you'd like, and outer join your data to it, filling in 0s for values that do not exist for a given term/date.
Edit:
Some more details.
1) Create a table that has a row for every date +- 10 years (or whatever is appropriate). You can make this one column if you'd like, or many columns (date, month, year, etc.). The second approach makes this extensible if you want to summarize by various rollups in the future.
2) Outer join your table to the date table and use a NVL statement to coerce any null averages to 0.
3) Profit!
If your results are grouped by date, how can MySQL know there's (for example) 31 days in that month?
On the other hand, you can somehow fill the holes in PHP by loop through the array and fill a zero if the value does not exist.

Copying values from one table with blank cells in between to another table with no cells in between

I'll try and explain this as basically as possible. We have a long list-like table with dates in the first column for each working day then a list on individual piercings we have completed with their respective prices and then daily totals in this format:
Date Piercing Price Daily Total
26/10/13 ear £9.50
navel £30 £39.50
28/10/13 nose £17.50 £17.50
29/10/13 ear £9.50
nipple £25
eyebrow £25 £59.50
etc
etc
As you can see the column which holds the daily totals will have a lot of blank cells in between. So what we have done is made a separate sheet with a list of dates and are hoping to find some form of lookup function which can read the existing "Daily total" column and copy only the cells which have values over in order that they appear so they match up with the right dates like this:
26/10/13 £39.50
28/10/13 £17.50
29/10/13 £59.50
etc
etc
If anyone can lend me as easy way of achieving this I will be most grateful as neither of us are talented with writing our own functions or statement codes.
If you are just looking for a list of dates and daily totals, assuming the daily totals have already been calulated and you never have negative transactions then this should work
SELECT date, MAX(DailyTotal) FROM peircings GROUP BY date;
OR
SELECT date, DailyTotal FROM peircings WHERE DailyTotal!='';
It's hard to know which one might even help you without a better understanding of your database and how you want to use the data.