Typecast to a FLOAT - casting

The following query was not returning my expected/desired results for slope and intercept. I finally realized that slope and intercept are typecasted as integers in my anchor query, and remain integers through the recursion.
WITH RECURSIVE t AS (
SELECT id parentId, idPublic parentIdPublic, name parentName,
slope parentSlope, intercept parentIntercept,
id, idPublic, name, type, 1 slope, 0 intercept, value
FROM measurements
WHERE id IN (414,415,416,417,491)
UNION ALL
SELECT t.parentId, t.parentIdPublic, t.parentName,
t.parentSlope, t.parentIntercept,
m.id, m.idPublic, m.name, m.type,
t.slope*pchm.sign*m.slope slope,
t.intercept+t.slope*pchm.sign*m.intercept intercept, m.value
FROM t
INNER JOIN subpoints sp ON sp.measurementId=t.id
INNER JOIN measurements m ON m.id=sp.measurementId
)
SELECT slope, intercept FROM t;
slope and intercept needs to be able to store widely different values and exact accuracy is not important, so I feel a FLOAT is best. My schema for slope and intercept is as shown:
`slope` FLOAT NOT NULL DEFAULT 1,
`intercept` FLOAT NOT NULL DEFAULT 0,
As a hack solution, I changed my anchor query to:
SELECT id parentId, ..., 1.0 slope, 0.0 intercept, value
I think the right solution is to typecase these two values to a FLOAT, but the documentation does not seem to allow. I could do DECIMAL, however, I have the issue with slope and intercept potentially being a wide range of values.
Am I able to cast slope and intercept to FLOAT? If not, what is the best solution?

I think that UNION is the villain, not WITH, etc. UNION likes to define the datatypes based on the first SELECT. Of course, with WITH RECURSIVE, you don't have the option of swapping the SELECTs.
So... I vote for your 'hack solution' of 0.0 and 1.0. Or, these might be clearer: 0e0 and 1e0. (Still a kludge, but it points at FLOAT, not DECIMAL.)
I, too, have bemoaned the absence of CASTing to FLOAT (or DOUBLE).

Related

Adding formatted value to plot title in Stata

I have variables, y and x, and I have run a regression to obtain the slope of the regression line. I can retrieve the p value (and other parameters) from the regression model using the method described in https://journals.sagepub.com/doi/pdf/10.1177/1536867X0800700408, such as:
regress y x
local t = _b[x]/_se[x]
local p = 2*ttail(e(df_r),abs(`t'))
I want to produce a plot of the same data with a line predictor such as:
graph twoway (lfitci x y) (scatter x y), note("Simple regression p value = `p'")
However, in the above case, the p value that is presented in the plot's note area is expressed to full precision (and without the leading zero). How can I restrict the number of decimal places in the plot to a sensible number? I've tried including %9.3f but haven't been able to work out the correct syntax.
local p : di %04.3f `p'
If you want spaces in front, put spaces in front.
Examples of similar technique can be found inside the code for aaplot on SSC.

PowerBI empty values row not displayed

I have a confusing mystery...
Simple DIVIDE formula works correctly. However blank rows are not displayed.
I attempted a different method using IF, and now the blank row is correctly displayed.
However this line is only displayed if I include the IF formula (which gives a zero value I don't want).
Formula 1:
Completion % =
DIVIDE(SUM(Courses[Completed]),SUM(Courses[Attended]),BLANK())
Formula 2:
Completion % with IF =
IF(SUM(Courses[Attended])=0,0,DIVIDE(SUM(Courses[Completed]),SUM(Courses[Attended])))
With only the DIVIDE formula:
Including the IF formula:
It appears that Power BI is capable of showing this row without error, but only if I inlude the additional IF formula. I'm guessing it's because there is now a value (0) to display.
However I want to be able show all courses, including those that have no values, without the inaccurate zero value.
I don't understand why the table doesn't include these lines. Can anyone explain/help?
The point is very simple, by default Power BI shows only elements for which there is at least one non-blank measure.
The DIVIDE operator under-the-hood execute the following:
IF(ISBLANK(B), BLANK(), A / B))
You can change its behaviour by defining the optimal parameter in order to show 0 instead of BLANK:
DIVIDE(A, B, 0) will be translated in the following:
IF(ISBLANK(B), 0, A/B))
Proposed solution
Those mentioned avobe might all be possible solutions to your problem, however, my personal suggestion is to simply enable the option "show item with no data" in your visualization.
While DIVIDE(A, B, 0) will return zero when when B is zero or blank, I think a blank A will still return a blank.
One possibility is to simply append +0 (or prepend 0+) to your measure so that it always returns a numeric value.
DIVIDE ( SUM ( Courses[Completed] ), SUM ( Courses[Attended] ) ) + 0

Using MINIF/MAXIF with strings containing numbers?

I want to find the minimum number with given conditions(is writer and is under probation), the below code works if D contains numbers, but how do I do it if the number is a part of a string, like a fraction for example? Like how do I use this formula if numbers in D look like "1/8", "31/688", "21/33", etc?
=MINIFS(D3:D1007, A3:A1007, "Writer", C3:C1007, "Probation")
I already have another formula that I use that calculates a decimal value given the fraction, If the fraction is in cell D21 then it would look like this:
=left(D21,find("/",D21)-1)/(right(D21,len(D21)-find("/",D21)))
but how do I apply this kind of formula in a minif/maxif?
I have attached a picture to show what I mean, what I'm trying to do is to put a formula in the passed/total column of package stats(probation), and it will get the lowest passed/total value out of the ones with that package name and importance level. as you can see, the entire writer package's pass rate is 5/8 because the lowest pass rate out of the writer package 5/8 is the lowest pass rate out people with package=writers and importance = probation. But at the moment I have to enter the 5/8s manually, I want it to be able to get it automatically using the formula I'm trying to figure out above.
try:
=ARRAYFORMULA(MIN(IF((A3:A="writer")*(C3:C="probation"),
IFERROR(REGEXEXTRACT(D3:D, "\d+")/REGEXEXTRACT(D3:D, "/(\d+)"), D3:D), )))
or to return fraction:
=ARRAYFORMULA(VLOOKUP(MIN(IF((A3:A="writer")*(C3:C="probation"),
IFERROR(REGEXEXTRACT(D3:D, "\d+")/REGEXEXTRACT(D3:D, "/(\d+)"), D3:D), )),
{IF((A3:A="writer")*(C3:C="probation"),
IFERROR(REGEXEXTRACT(D3:D, "\d+")/REGEXEXTRACT(D3:D, "/(\d+)"), D3:D), ), D3:D}, 2, 0))
also make sure fractions are formatted as plain text not date

Adding values from multiple .rrd file

Problem =====>
Basically there are three .rrd which are generated for three departments.
From that we fetch three values (MIN, MAX, CURRENT) and print ins 3x3 format. There is a python script which does that.
eg -
Dept1: Min=10 Max=20 Cur=15
Dept2: Min=0 Max=10 Cur=5
Dept3: Min=10 Max=30 Cur=25
Now I want to add the values together (Min, Max, Cur) and print in one line.
eg -
Dept: Min=20 Max=60 Cur=45
Issue I am facing =====>
No matter what CDEF i write, I am breaking the graph. :(
This is the part I hate as i do not get any error message.
As far as I understand(please correct me if i am wrong) I definitely cannot store the value anywhere in my program as a graph is returned.
What would be a proper way to add the values in this condition.
Please let me know if my describing the problem is lacking more detail.
You can do this with a VDEF over a CDEF'd sum.
DEF:a=dept1.rrd:ds0:AVERAGE
DEF:b=dept2.rrd:ds0:AVERAGE
DEF:maxa=dept1.rrd:ds0:MAXIMUM
DEF:maxb=dept2.rrd:ds0:MAXIMUM
CDEF:maxall=maxa,maxb,+
CDEF:all=a,b,+
VDEF:maxalltime=maxall,MAXIMUM
VDEF:alltimeavg=all,AVERAGE
PRINT:maxalltime:Max=%f
PRINT:alltimeavg:Avg=%f
LINE:all#ff0000:AllDepartments
However, you should note that, apart form at the highest granularity, the Min and Max totals will be wrong! This is because max(a+b) != max(a) + max(b). If you dont calculate the min/max aggregate at time of storage, the granularity will be gone at time of display.
For example, if a = (1, 2, 3) and b = (3, 2, 1), then max(a) + max(b) = 6; however the maximum at any point in time is in fact 4. The same issue applies to using min(a) + min(b).

How to use Ruby's Enumerable .map method to do something similar to map in C++

map(-30, -89.75, 89.75, 0, 360)
I'm looking for something like this where:
-30 is the input value.
-89.75 to 89.75 is the range of possible input values
0 - 360 is the final range to be mapped to
I was told there is a way to do this using http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-map
.. however its not readily apparent !
If I'm understanding correctly, I think you just want to uniformly map one range onto another. So, we just need to calculate how far through the input range it is, and return that fraction of the output range.
def map_range(input, in_low, in_high, out_low, out_high)
# map onto [0,1] using input range
frac = (input - in_low) / (in_high-in_low)
# map onto output range
frac * (out_high-out_low) + out_low
end
Also, I should note that map has a bit of a different meaning in ruby, and a more appropriate description would probably be transform.