Currency Conversion Logic for multiple rows - tsql

I have a question regarding the currency conversion logic. Currently, we return a result set which has got many currency fields. Now, the requirement says that the users should have an option to select the output currency format
eg:
Account name | Actual Amount | Estimated Amount | Target Amount
XYZ | $ 2000 | $ 456.78 | $ 890.45
ABC | SD 2000 | SD 456.78 | SD 890.45
if the user now selects Yen as the output format, the result set should be
Account name | Actual Amount | Estimated Amount | Target Amount
XYZ | ¥ 2233 | ¥ 42356.78 | ¥ 82390.45
ABC | ¥ 21213000 | ¥ 41156.78 | ¥ 82390.45
The exchange rate is available, and I know that we could have a function call in the select statement to convert the currency columns. But, making the function call for each record increases the execution time.
Is there any other logic that could be used to improve the execution time.

When you use a function to do conversions, you have to know that for every single record the function will be executed. So when only look at the overhead of executing a function, you'll have as much executions as you have records.
You can better make a conversion table, where you put the conversion rate in it, and join against that one. Make sure you have a column which holds the currency in both the conversion rate table and you original table, and you are good to go. In the select statement you output a formula, like rate*Actual Amount.

Related

Tableau - Return Name of Column with Max Value

I am new to Tableau visualization and need some help.
I have a set of shipping lanes which have whole numbers values based on the duration for the shipment.
Ex:
| Lane Name | 0 Day | 1 Day | 2 Day | 3 Day | 4 Day |
| SFO-LAX | 0 | 30 | 60 | 10 | 0 |
| JFK-LAX | 0 | 10 | 20 | 50 | 80 |
For each Lane Name, I want to return the column header based on the max value.
i.e. for SFO-LAX I would return '2 Day', for JFK-LAX I would return '4 Day', etc.
I then want to set this as a filter to only show 2 Day and 3 Day results in my Tableau data set.
Can someone help?
Two steps to this.
The first step is pretty easy, pivot your data. Read the Tableau help to learn how to PIVOT your data for analysis - i.e. make it look to Tableau as a longer 3 column data set with Lane, Duration, Value as the 3 columns. Tableau's PIVOT feature will let you view your data in that format (which makes analysis much easier) without actually changing the format of your data files.
The second step is a bit trickier than you'd expect at first glance, and there are a few ways to accomplish it. The Tableau features that can be used for this are LOD calcs, table calcs, filters and possibly sets. These are some of the more powerful but complicated parts of Tableau, so worth your time to learn about, but expect to take a while to spin up on them.
The easiest solution is probably to use one of the RANK() function - start as a quick table calc. Set your partitioning and addressing as desired so that the ranks are computed for the blocks of data that you desire - say partitioning on Lane and addressing or computing by Duration. Then when you are happy with the ranks you see, move the rank calculation to the filter shelf and only display data where rank = 1.
This is a quick solution once you get the hang of it, but it can get slow for very large data sets since the rank calculations are done on the client side, requiring fetching all the data that you end up not displaying. If performance becomes an issue, you might want to look at other solutions to do more of the calculations server side - possibly using LOD calcs or analytic aka windowing queries invoked from custom SQL

Is it possible to make multiple fields default to the same date, but also be individually editable?

I am VERY new to Access - I was sort of thrust into designing a database for a research project I'm involved in. So, please bear with me because I know next to nothing :) The problem I am having is thus:
My database is for a medical research project, and is very time and date dependent, by which I mean I need to capture the date and time for each piece of data so that we end up with a sort of timeline of events for each subject.
As is, I have something like the following for each piece of data: (Each in it's own field)
ArrivalDate
ArrivalTime
HeartRateDate
HeartRateTime
HeartRateData
TemperatureDate
TemperatureTime
TemperatureData
BloodPressureDate
BloodPressureTime
BloodPressureData
There are around 200 similar pieces of data that I need to collect for each patient. To avoid having to re-enter the same data over and over, and also to reduce the potential for error, I would like to have all of the date fields in a given patient record default to the first one that is entered, in this case "Arrival Date". However, I also need each date field to be editable without affecting the others. The reason for this is that in the event that a patient's visit occurs over the span of a few days we can accurately record that.
I have tried messing around with the default value setting, as well as setting the control source to reference the "Arrival Date" field, but then of course any changes to one field affect them all. I am not even sure that what I am trying to do is possible but I will appreciate any help and/or suggestions!
Thank you in advance
Having all this data in separate columns of a big table isn't going to work. You don't measure things like temperature or blood pressure only once per patient, do you?
This is a classic one-to-many relation.
You should have a separate Measurements table, looking e.g. like this:
+--------+-----------+---------------+------------------+-----------+
| MeasID | PatientID | MeasType | MeasDateTime | MeasValue |
+--------+-----------+---------------+------------------+-----------+
| 1 | 1 | Temperature | 2017-05-17 14:30 | 38.2 |
| 2 | 1 | BloodPressure | 2017-05-17 14:30 | 130/90 |
| 3 | 1 | Temperature | 2017-05-17 18:00 | 38.5 |
| 4 | 2 | Temperature | etc. | |
+--------+-----------+---------------+------------------+-----------+
As Barmar wrote, there is no reason to have separate columns for date and time.
In the form where measurements are entered, you can use the BeforeInsert event to set MeasDateTime to the current time, with the Now() function.
So the user never has to enter it manually, but they can edit it if the measurement was at a different time than entering the data.

Time-series Stock Data in Matlab

I'm a MatLab beginner, and have no idea what I'm doing.
I have stock data in CSV format which is something like this:
+--------+--------+------+------+-----+-------+
| Ticker | Date | Open | High | Low | Close |
+--------+--------+------+------+-----+-------+
| APPL | 25-Oct | 10 | 12 | 9 | 12 |
| XYZ | 25-Oct | 10 | 12 | 9 | 12 |
| APPL | 26-Oct | 12 | 15 | 10 | 15 |
+--------+--------+------+------+-----+-------+
There are many stock tickers each day. The file is many rows long listing daily stock prices for each ticket on a particular stock exchange.
I'm aiming to do some fun time-series analysis on the 'close' price for each ticker.
To start with making simple charts of a single ticker over time, or multiple tickers over time would be awesome.
Questions:
1. Best way to import data.
I have a big long CSV. But am lost as to which import method is best. Column Vectors, Numeric Matrix, Cell Array or Table?
2. I need to create a time-series object for each ticker, right?
How would one go about that? I've been looking at this guide, but I'm unsure how to make an object for each ticker, over the span of time defined in the file.
http://www.mathworks.com/help/matlab/ref/timeseries-class.html
Any advice, pointers and resources that are good for beginners are appreciated massively!
Thanks!
There are a ton of ways to import data into MATLAB. Before you import data, I would make sure numeric columns hold ONLY numeric data or MATLAB can complain. Some options in my personal order of preference:
d = readtable('mycsvfile.csv'); % puts data in nice table datatype. I find it makes code more readable.
d = csvread('myfile.csv',1,0); % the 1 skips the first row which is probably header names for the csv file. Puts all the data in a matrix and you have to keep track of what column is what.
xlsread is good for reading excel files
Copy and paste the data into a variable in your workspace. Do save blahblah.mat so you can easily load the data later.
I personally wouldn't bother with financial time series objects. It's just going to complicate your life if you're new to MATLAB. If you loaded the data using tableread (i.e. option 1) you can then execute something like:
aapl_indicator = strcmp(d.Ticker, 'AAPL');
to get a vector indicating whether a row in your table is AAPL or not. Then:
close_price_aapl = d.Close(aapl_indicator);
will give you a vector of Apple's closing prices.
When you get down to doing math, you want to be using the matrices.

merging rows in postgres with matching fields

I have a table format that appears in this type of format:
email | interest | major | employed |inserttime
jake#example.com | soccer | | true | 12:00
jake#example.com | | CS | true | 12:01
Essentially, this is a survey application and users sometimes hit the back button to add new fields. I later changed the INSERT logic to UPSERT so it just updated the row where email=currentUsersEmail , however for the data inserted prior to this code change there are many duplicate entries for single users. i have tried some group by's with no luck, as it continually says the
ID column must appear in the GROUP BY clause or be used in an
aggregate function.
Certainly there will be edge cases where there may be clashing data, in this case maybe user entered true for the employed column and then in the second he/she could have enter false. For now I am not going to take this into account yet.
I simply want to merge or flatten these values into a single row, in this case it would look like:
email | interest | major | employed |inserttime
jake#example.com | soccer | CS | true | 12:01
I am guessing I would take the most recent inserttime. I have been writing the web application in scala/play, but for this task i think probably using a language like python might be easier if i cannot do it directly through psql.
You can GROUP BY and flatten using MAX():
SELECT email, MAX(interest) AS interest,
MAX(major) AS major,MAX(employed) AS employed,
MAX(inserttime) AS inserttime
FROM your_table
GROUP BY email

Calculate the percentage of a column which has redundant rows in Tableau

I want to calculate the percentage of a column which has redundant rows.
For example ... I would like to calculate the percentage of "Success" for A and B in the below table
+-------+---------+
| Name | Result |
+-------+---------+
| A | Success |
| B | Success |
| A | Fail |
| A | Success |
| B | Fail |
| B | Fail |
| A | Success |
+-------+---------+
I tried using Calculated field by putting If([NAME]) = "Success" then 1 else 0
and then editing the table calculation to Percentage -> table across and down... but didn't work :(
You can absolutely use the "Percent of Total" table calculation for this. The tricky bit is going into the "Edit Table Calculation" dialog and telling Tableau how you want it to perform the calculation.
Here's an example of how to do this that you can adjust to fit your specific needs. Place [Name] and [Result] in the Rows shelf. Then place SUM(Number of Records) into Text. You'll end up with something like this:
Name Result | |
---------------------+-----+
A Fail | 1 |
Success | 3 |
--------------------+-----+
B Fail | 2 |
Success | 1 |
--------------------+-----+
Then right click on SUM(Number of Records) and click "Add Table Calculation...". At the top of the Table Calculation dialog, go to "Calculation Type:" and choose "Percent of Total". In "Summarize the values from:", it will default to "Table (Down)". Go ahead and hit Apply at this point and see what happens. Bad news - it's wrong.
Name Result | |
---------------------+-----+
A Fail | 14% |
Success | 43% |
--------------------+-----+
B Fail | 29% |
Success | 14% |
--------------------+-----+
The default "Table (Down)" is almost never what you actually want. That says to calculate the percent of total for your entire partition, but you'd actually like to see that percent of total for each Name.
Until you get really good at this part (and maybe even after you've become a Tableau Zen Master and a Tableau god among men), I recommend always going to the advanced menu when you're defining your table calculations. It's a good opportunity to really think through exactly how you want Tableau to perform the calculation. In this case, you want to calculate the percent of each result (Success and Fail) for each name.
Go to the Advanced dialog (under "Summarize the values from:"). You'll see Name and Result under Partitioning and nothing under Addressing. Move Result over to Addressing and leave Name under Partitioning. What you're saying here is "I want Tableau to calculate the percent of each result (Success or Failure). I want it to do this for each name."
Apply those changes, and you should see something like this:
Name Result | |
---------------------+-----+
A Fail | 25% |
Success | 75% |
--------------------+-----+
B Fail | 67% |
Success | 33% |
--------------------+-----+
Perfect. If you just want to see the Successes, just right click on "Fail" in the table and click Hide. Do NOT filter them out. That will remove those rows from your partition, and thus from the total that is considered in the percent of total calculation. By hiding the Fails instead of filtering them, you keep them in the partition but don't show them in the data view.