I have a relation A:
Store Product
---------- ----------
store A P1
store A P4
store B P1
store B P2
store B P3
store B P4
store C P2
store C P3
store C P4
and another relation B:
Product
----------
P1
P2
P3
How to find all stores (store B only in this case) that sell every product from relation B using Relational Algebra query?
Related
T1: | W(X) |------| R(X) |
T2: |------| R(X) |------|
X represents a database object, W represents write operation and R represents read operation.
What will be the consequences of swapping confliction actions W(X) of T1 and R(X) of T2?
Swapping conflicting operations gives you a schedule, representing different serial order of transactions. Your example corresponds to a serial order T1, T2. After swapping it will be T2, T1, giving completely different outcome.
I have two tables A and B. A has float variables X1, X2, X3, ... , X9, sumprod and 14,000 rows. B has float variables X1, X2, X3, ... , X9, a text variable 'Model' with values such as 'Model 1', 'Model 2' and so on, and 50 rows.
I am trying to join and performs a sumproduct operation using the following code:
Update A set a.sumprod = a.X1*b.X1 + a.X2*b.X2 + ... + a.X9*b.X9
from a left join b
on b.Model = 'Model 2';
I have multiple such queries with different tables as A, and corresponding different join conditions on the Model variable in table B. I have identified these queries as taking the longest time in my stored procedure and am looking for a way to make them faster.
I have tried variants of this query like below without any material changes in runtime:
Variant 1:
Update A
set a.sumprod = a.X1*b.X1 + a.X2*b.X2 + ... + a.X9*b.X9
from a left join b
on 1 = 1
where b.Model = 'Model 2';
Variant 2:
merge A
using (select X1, X2, ..., X9 from B where Model = 'Model 2') C
on 1 = 1
when matched then update
set sumprod = a.X1*c.X1 + a.X2*c.X2 + ... + a.X9*c.X9;
Edit for greater clarity:
There are multiple table A's: A1, A2, A3, ... Each table A# contains explanatory variables (X1, X2 etc) for a model (corresponding to the model number in table B).
So table A1 may be:
X1 | X2 | X3 | X4 | Sumprod
6 | 7 | 3 | 5 |
5 | 3 | 4 | 4 |
...
Table A2 would have a different number of explanatory variables, and the explanatory variables themselves would be different. Also, the number of rows would be different from A1.
Table B has model coefficients for each model like so:
Model | X1 | X2 | X3 | X4 | X5 | X6 | X7 | X8 | X9
Model 1 | 3 | 2 | 5 | 9 | 0 | 0 | 0 | 0 | 0
Model 2 | 4 | 7 | 8 | 3 | 5 | 8 | 0 | 0 | 0
...
Model 1 has four explanatory variables, so the Model 1 row in table B has zero coefficients for columns X5 onwards.
What I want to do in the sumprod column of each table A is take the sum product of the explanatory variables and the coefficients from the correct row in table B. There is no common row identifier between the table A's and the coefficient table B. I am taking the sum product of EACH row in A1 with a SINGLE row in B.
After the join, I want the sumprod column of table A1 to be populated as below:
X1 | X2 | X3 | X4 | Sumprod
1 | 7 | 3 | 5 | 6*3 + 7*2 + 3*5 + 5*9 = 92
5 | 3 | 4 | 4 | 5*3 + 3*2 + 4*5 + 4*9 = 77
...
Values for the explanatory variables are fixed but values for the model coefficients are user inputs and are expected to change fairly often.
From the initial comments, it seems that this is not a good database structure for what I want to do. Any suggestions for how I can make this faster?
FROM a LEFT JOIN b ON b.Model = 'Model 2'? I have no idea what behavior you're expecting from this join, and I suspect the query engine is equally confused. Do you actually want a CROSS JOIN? You should just say CROSS JOIN then.
Here's what I would do:
UPDATE a
SET a.sumprod = a.X1 * b1.X1 + a.X2 * b1.X2 +...+ a.X9 * b1.X9
FROM a
CROSS JOIN (
SELECT Model, X1, X2, ..., X9 FROM b where Model = 'Model 2'
) b1
WHERE a.sumprod <> a.X1 * b1.X1 + a.X2 * b1.X2 +...+ a.X9 * b1.X9
OR a.sumprod is NULL;
Is there a reason that you have to CROSS JOIN? Is there truly no relation between a and b? That seems like a design problem. You want to make everything in a.sumprod be a function of what's in one row of b? Are you planning to change that repeatedly? You've abstracted too far to tell what you're trying to accomplish.
Personally, I would create a VIEW that returned the necessary product sums rather than updating a field in a as storing aggregates is a generally poor idea, but if you're already having performance issues that may not be wise.
So I have a relation:
Cars(model, passenger)
The models are all unique, let's say, {A, B, C, D, E}.
Passengers is just the capacity of the car (any positive non-zero integer), let's say {1,2,2,3,3}
Model|Passenger
A |1
B |2
C |2
D |3
E |3
I need to find the relational algebra expression that would yield what capacities occur for more than 1 vehicle. So with the example values above, the expression should return {2, 3} since they appear more than once for different vehicles.
I have a strong inclination to think that the expression will use a join of some sort but I can't figure out how to do it.
I figured it out:
Assuming an existing relation Cars(model, passenger) that contains all of the cars in question and their passenger capacities.
CARS2(model,passenger)≔ρ_(m,p) (CARS)
Answer (passenger)≔π_passenger (CARS⋈_(model ≠ m AND passenger=p) CARS2)
I'm not sure about relational algebra expression, which might look something along the lines
π Passenger σ Count(Model) >= 2 G Passenger (Table1)
but if you're looking for a query than it doesn't include a JOIN in it
SELECT passenger
FROM table1
GROUP BY passenger
HAVING COUNT(model) >= 2
Outcome:
| PASSENGER |
|-----------|
| 2 |
| 3 |
Here is SQLFiddle demo
Suppose i have a table like below
reg sub1 sub2 sub3
1 A F F
2 F F A
3 A B B
4 A B F
5 A F F
sub1, sub2, sub3 are the three different subjects and the values A, B & F are the grades where A and B holds certain points but F is fail....Now i want the reg of students who has one 'F'(i.e Failed in only one subject) and two subjects, 3 subjects and so on....How to do tat.....
My desired output for students with one F(i.e Failed in only one subject) is...
reg sub1 sub2 sub3
4 A B F
and students with two F(i.e Failed in two subjects) is...
reg sub1 sub2 sub3
1 A F F
2 F F A
5 A F F
First, like #Mat said in a comment above, this would be much easier if you normalised your data model.
Second, I'll be assuming that you are looking for a SQL query. Third, since you didn't mention at all what database system you are using (SQL Server, DB2, Oracle, Postgres, etc.), I will show a solution that works at least for SQL Server:
SELECT *
FROM grades
WHERE (CASE sub1 WHEN 'F' THEN 1 ELSE 0 END) +
(CASE sub2 WHEN 'F' THEN 1 ELSE 0 END) +
(CASE sub3 WHEN 'F' THEN 1 ELSE 0 END) = 2
This would find all records in your table (grades) with exactly 2 F grades. What it does is to convert each F into a 1, and every other grade into 0, then it builds the sum of these numbers. That is, it computes the number of Fs for each record.
I'm currently reading "Artificial Intelligence: A Modern Approach" (Russell+Norvig) and "Machine Learning" (Mitchell) - and trying to learn basics of AINN.
In order to understand few basic things I have two 'greenhorn' questions:
Q1: In a genetic algorithm given the two parents A and B with the chromosomes 001110 and 101101, respectively, which of the following offspring could have resulted from a one-point crossover?
a: 001101
b: 001110
Q2: Which of the above offspring could have resulted from a two-point crossover? and why?
Please advise.
It is not possible to find parents if you do not know the inverse-crossover function (so that AxB => (a,b) & (any a) => (A,B)).
Usually the 1-point crossover function is:
a = A1 + B2
b = B1 + A2
Even if you know a and b you cannot solve the system (system of 2 equations with 4 variables).
If you know any 2 parts of any A or/and B then it can be solved (system of 2 equations with 2 variables). This is the case for your question as you provide both A and B.
Generally crossover function does not have inverse function and you just need to find the solution logically or, if you know parents, perform the crossover and compare.
So to make a generic formula for you we should know 2 things:
Crossover function.
Inverse-crossover function.
The 2nd one is not usually used in GAs as it is not required.
Now, I'll just answer your questions.
Q1: In a genetic algorithm given the
two parents A and B with the
chromosomes 001110 and 101101,
respectively, which of the following
offspring could have resulted from a
one-point crossover?
Looking at the a and b I can see the crossover point is here:
1 2
A: 00 | 1110
B: 10 | 1101
Usually the crossover is done using this formula:
a = A1 + B2
b = B1 + A2
so that possible children are:
a: 00 | 1101
b: 10 | 1110
which excludes option b from the question.
So the answer to Q1 is the result child is a: 001101 assuming given crossover function
Q2: Which of the above offspring could
have resulted from a two-point
crossover? and why?
Looking at the a and b I can see the crossover points can be here:
1 2 3
A: 00 | 11 | 10
B: 10 | 11 | 01
Usual formula for 2-point crossover is:
a = A1 + B2 + A3
b = B1 + A2 + B3
So the children would be:
a = 00 | 11 | 10
b = 10 | 11 | 01
Comparing them to the options you asked (small a and b) we can say the answer:
Q2. A: Neither of a or b could be result of 2-point crossover with AxB according to the given crossover function.
Again it is not possible to answer your questions without knowing the crossover function.
The functions I provided are common in GA, but you can invent so many of them so they could answer the question (see the comment below):
One point crossover is when you make one join from each parent, two point crossover is when you make two joins. i.e. two from one parent and one from the others.
See crossover (wikipedia) for further info.
Regarding Q1, (a) could have been produced by a one-point crossover, taking bits 0-4 from parent A and bit 5 from parent B. (b) could not unless your crossover algorithm allows for null contributions, i.e. parent contributions of null weight. In that case, parent A could contribute its full chromosome (bits 0-5) and parent B would contribute nil, yielding (b).
Regarding Q2, both (a) and (b) are possible. There are a few combinations to test; too tedious to write, but you can do the work with pen and paper. :-)