Let's say I have a table in my database with attributes:
{ int Student_Id (primary key), float Grade, category Level },
where the category of the Level variable is completely determined by the Grade via a piecewise function. Something like:
Level = "Top" if 90 <= Grade <= 100;
"High" if 80 <= Grade < 90;
"Basic" if 60 <= Grade < 80; and
"Low" if 0 <= Grade <60.
Obviously there is a functional dependency between non-key attributes (Grade -> Level), which appears to be a 3NF violation. To eradicate that dependency, I would have to create a new table { Grade, Level }, but that table would be practically infinite, because the grades can have any real value between 0 and 100.
Also, the variable Level is very important, so I don't want to do without it in my database, having to "calculate" it every time I need it.
Is there an official way to deal with this type of dependency?
Related
I'm fairly new to SQL and can't figure out how to combine several if .. then statements.
What is the right syntax for this?
I'm using SQL Server Management Studio 2017.
I've tried to combine if... else if..statements and I tried using case statements, but I always get lost in the nesting of the statements.
I have several condtions whom have to be met before I can execute some sort of calculation.
It should be something like this:
If CalculationMethod = x
and if (Price * coefficient) < Amount
then CalculatedAmount = Amount
else CalculatedAmount = (Price * coefficient)
Where Amount has it's own if statements:
Amount =
If Category = a and DistanceFrom <= Distance >= DistanceUntill then take amount from that particular cell
If Category = b and DistanceFrom <= Distance >= DistanceUntill then take amount from that particular cell
If Category = c and DistanceFrom <= Distance >= DistanceUntill then take amount from that particular cell
In this case, Amount is a cell in a table with columns DistanceFrom, DistanceUntill, a, b and c.
CalculationMethod and Coefficient are columns in another table.
Price is a column in third table.
In the end I want the CalculatedAmount based on the Amount, Price and Coefficient.
Does this make any sense? Does anyone has an idea on how to tackle this?
If you have an IF...THEN...ELSE type of scenario I think the right direction would be to use a CASE statement such as:
SELECT CASE WHEN CalculationMethod = x AND ((Price * coefficient) < Amount) THEN Amount
ELSE (Price * coefficient) END CalculatedAmount
You can read about it here: https://learn.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql?view=sql-server-2017
An IIF clause works very well when there is only one decision branch, but if you have multiple things to choose from the CASE statement is the way to go.
SELECT IIF(CalculationMethod = x and Price * coefficient < Amount, Amount, Price * coefficient) as CalculatedAmount
FROM aTable
Say I have a nodes person and movie with a relation of [likes]. I am trying to be able to limit the amount of persons that like the same movie without limiting my results to only include 1 movie.
For example:
MATCH (p:Person)-[LIKES]->(m:Movie)
WHERE
p.age < 30
p.gender = "Male"
RETURN p,m
So in the query above I would like to get all the results but filter them so that only 2 Persons will like the same movie.
Is this possible?
This knowledge base article goes over different ways to limit match results per row.
For a non-APOC approach you can get the slice of the collection of people who liked the movie:
MATCH (p:Person)-[:LIKES]->(m:Movie)
WHERE
p.age < 30
p.gender = "Male"
RETURN m, collect(p)[..2] as peopleWhoLiked
If you want a separate row per person, then UNWIND the peopleWhoLiked list before the return.
For the second approach, you'll need APOC Procedures.
In order to use LIMIT, you'll need to first match on all movies, then perform the limited match to :Person nodes using apoc.cypher.run().
To get all movies that have exactly two (under-30 male) likers:
MATCH (p:Person)-[:LIKES]->(m:Movie)
WHERE
p.age < 30 AND
p.gender = "Male"
WITH m, COLLECT(p) AS likers
WHERE SIZE(likers) = 2
RETURN m, likers;
I want to control TPH / TPS using HP LoadRunner. In JMeter we can do it by using constant throughput timer or if any one has alternative ways then please share.
For example:
Transaction A-Login (100 TPH)
Transaction B-Search Product (1000 TPH)
Transaction C-Add Product in cart (200 TPH)
Transaction D-Payment (200 TPH)
Transaction E-Logout (100 TPH)
If all of these transactions are in different scripts, no problem, since you can set different pacing and run time settings to each script.
I assume your problem is that all of these transactions are in the same script. In this case, the only solution is to create a parameter in your script, let's call this parameter iterator, and set its type as iteration number. This way, this parameter will be with value 1 in the first iteration, value 2 in the second, etc. etc.
Now you can use this parameter before calling each transaction.
Let's say your maximum TPH is 1,000. Then set the script's run time settings pace to 1,000 TPH. But if you want a certain transaction to run less than that, let's say only 100 TPH, then you need to run it every 10th iteration only (1,000 / 100 = 10).
To do that, in your script, you can use iterator % 10:
// Cast the iterator parameter to an int
var i;
i = atoi(lr_eval_string("{iterator}"));
// This will run 100 TPH
if ((i % 10) == 0)
{
lr_start_transaction("Login");
// Do login
...
lr_end_transaction("Login", LR_AUTO);
}
And another example, to run 200 TPH, you can use iterator % 5:
// This will run 200 TPH
if ((i % 5) == 0)
{
lr_start_transaction("Add Product");
// Do Add Product
...
lr_end_transaction("Add Product", LR_AUTO);
}
Using v 6.5.0 with a Decision table
I have a compact java model, as it is delivered to the web front end intact. Thus the User class has a field that is an array of of Accounts, each with a total, amount and date field. I have written simple rules but this is beyond me ability currently. I realise it would be better to have the Accounts objects separate and with a reference to the Id of the User, but if it is possible to not have to do that then it will save time.
I need to sum the Account.total for a User and test it is within 1 of 3 different range. Then I need to take this sum and work out the % of the sum of the amount fields and test to see if that is in 1 of 3 ranges. All of this is grouped by a date range.
In SQL for one total and % range
SELECT u.id, SUM(a.total) AS TTLSUM, SUM(a.amount)/SUM(a.total) AS PCTSUM
FROM User u, Account a
WHERE u.id = a.userid
GROUP BY u.id
HAVING SUM(a.total) > 0 AND SUM(a.total) <=1000
AND SUM(a.amount)/SUM(a.total) > 0.5 AND SUM(a.amount)/SUM(a.total) <=0.75
What you do is you write two functions, one for the sum and one for the percentage:
function boolean totalInRange( User user, double lo, double hi ){
// access user's double[] and calculate the sum, do the test
return lo < sum && sum <= hi;
}
Then you write a couple of rules
rule "is sum in range x"
when
$u: User()
eval( totalInRange( $u, 0, 1000 ) )
then
// do something
end
I have attached the screenshot. Most probably that the image itself will explain my logic. Let me paste the code I have used in the fields. Product Info1 field which hold Y & N.
RemainingCosu2=If(Invoice Line Items::Product Info1 = GetAsText ("N"); Sum(Cost Total) - Invoice Line Items::VendPaid_total;0)
RemainingCosu1=If(Vendor Status="Partly Paid"; RemainingCosu2; 0)
What should I do to fix this issue?. Please check the screenshot:
Filemaker has no SumIf() function. You need to create a calculation field in the LineItems table, along the lines of:
If ( Paid = "N" ; Cost )
then sum this field at the invoice level (and/or summarize it at the LineItems table itself), instead of the Cost field.
--
BTW, it is much more convenient to define Boolean (yes/no) fields as Number and use the values of 1 for True, 0 (or empty) for False. Then the calculation can be simply:
If ( not Paid ; Cost )