Cartesian products of tables that contains same columns - relational-algebra

I am really confused about this. I searched a lot of tutorials but i couldnot find a clear answer.
A B B D
1 X x 5
2 x y 6
x 4
I want to cross this two tables.A , B, B,d are attributes.
A B B D
1 X x 5
2 x x 5
1 X y 6
2 x y 6
1 X x 4
2 x x 4
This should be answer normally according to rule of cartesian. Cross all rows. But i am confused about same column B. Same column will seem twice?

Some relational query language/algebra relations have ordered column names. So an attribute name can name more than one column. (There should be a way to reference a column by column number.) The Cartesian product operator can take any two relations, because it is not a problem for an attribute name to appear more than once on output (or input).
Some relational query language/algebra Cartesian product operators change attribute names on output. Input relations have associated names. (The input either includes names of relation variables/constants or relation values contain names.) Each output column name is a combination of an input column name and an input relation name. The Cartesian product operator can take any two relations. Because columns that appear in different relations with the same attribute name on input will be distinguished by input relation name on output.
Some relational query languages/algebras have both. The Cartesian product operator can take any two relations.
Some relational query language/algebra relations have only one copy of an attribute name. So Cartesian product can only be called when inputs don't share any attribute names. (It is just a special case of NATURAL JOIN and/or EQUIJOIN.) If you have inputs sharing column names and you want output with a column for every input column then you need to rename columns so all are different before you join.
So you must pay close attention to what kind of relations and operators a relational query language/algebra has.
PS: SQL query language
The FROM clause of a SELECT expression makes a temporary Cartesian product of its tables. Every column name is an input column name prefixed by a given table name or table alias, plus a dot. This is the changes-names verions of Cartesian product above. The dotted names are used in the rest of the SELECT expression. The SELECT clause finally removes the prefixes and dots. So there are no dots in an SQL table value outside a SELECT expression. (The columns are also ordered, and there can be duplicate column names.)
If your SQL table expressions are AB & BD then you just need
SELECT * FROM AB ab CROSS JOIN BD bd

Dont you just need the following;
SELECT * FROM TAbleA CROSS JOIN TableB
?

Related

Custom column to sum values from second table with conditional statement

I have two tables, the first table has a list of invoice numbers and the second table has a list of products associated with each invoice. I want to sum the total cost of the products for each invoice and include it in the first table using Excel Power Query.
Table 1
[InvoiceNumbers] [OtherData]
Table 2
[Product] [Amount] [InvoiceNumber]
List.Sum() seems to be the function I need to use, but I cannot filter table 2 by invoice number using this function
Table.SelectRows() can be used to select the second table, and filter it to a specific set of rows, but I cannot seem to filter the rows of Table 2 using a column from Table 1.
I have also looked into Grouping and joining the table, but because of other factors I have left out, this is not going to work.
The full query Im working with looks like this.
List.Sum(Table.SelectRows(Table2, each [InvoiceNumber] = [InvoiceNumber])[Amount])
This just returns the sum of all the rows because [InvoiceNumber] is equal to itself.
How can I reference the Invoice Number of the row in Table 1 to use it as a condition in the Table.SelectRows() function? Or is there a better way to get the data sum the from Table 2?
Table 1 Final
[InvoiceNumber] [OtherData] [SumOfAmounts]
If there is a restriction to group the invoice details table, you could just reference it, and group the reference
1) Reference the table:
2) Group the referenced table:
3) Then merge the reference table and expand the total column
If this helps please remember to mark the answer

Codeigniter join multiple times same table and same column name

I'm having a problem, i have this table named IMOVEIS and another table named CARACTERISTICAS.
IMOVEIS must do at least 10 joins with CARACTERISTICAS, to get different caracteristics (this is a real state project).
Now I know how to make multiple joins in same table, the problem is: the column name of CARACTERISTICAS table (CT_NOME it's the column's name).
So how to change column name to each join result?

fact table is being populated with too many records

there are 62000 records in my fact table which is not correct because I only have six records in my time dim, 240 records in my student dim and 140 records in my placement dim, does it have something to do with my where clause any help would be mostly appreciated.
INSERT INTO fact_placements (
report_id,
no_of_placements,
no_of_students,
fk1_time_id,
fk2_placement_id,
fk3_student_id )
SELECT
fact_seq.nextval,
no_of_placements,
no_of_students,
time_id,
placement_id,
student_id
FROM
time_dim,
placement_dim,
student_dim
WHERE
placement_dim.year = time_dim.year AND
student_dim.year = time_dim.year;
Unless you do a cartesian join i.e. without any WHERE clause, you will get less than 140 (placement) * 240 (student) * 6 (time) = 201600 fact records. Your current SQL uses the year column in the 3 tables to join, this is filtering down the records to the 62000 you are getting.
Your question title says that even this is "too many". If that is the case, then you would need to understand the granularity of your dimensions and the fact before joining these on any criteria. Are these all at the "year" level, if so do you have 1 record per year in each of these tables and no duplicates based on year?
If not, you might need to re-think the fact tables granularity or alternatively would need to join unique records based on year in each dimension to get the actual (less) number of records you are expecting, which can also be done by summarizing these tables based on year.
Ideally the fact table contains the combinations of the dimension keys with additional column i.e. the factual metrics (in this case no_of_placements and no_of_students). But depending on the available data not all combinations will be present in the fact table.
Also you might want to change the SQL syntax to use the INNER JOIN clause instead of the implied joins using the commas between table names in the FROM clause, as shown below
FROM time_dim
INNER
JOIN placement_dim
ON placement_dim.year = time_dim.year
INNER
JOIN student_dim
ON placement_dim.year = student_dim.year
There's no relationship between placement and student that's why you have so many records.
Your query is saying: Give me all the students and all the placements where year is the same.
I'm not sure that's what you want. What is really strange here is that you are loading a fact table with dimensions tables.

How do I link two parse tables per common field?

I have a simple relationship between two parse tables (objects).
Both contain a 'name' string column.
All I need to do is to create a join between the two tables per 'name' column.
For example:
Table A (main) <--> Table B (ancillary)
where
'A.name' == 'B.name'.
Table A is the driving table: all criteria focuses on Table A.
Table B is the ancillary table that has a particular field (column) needed for Table A's result.
Note: the tables don't have any formal relation with each other.
What is the correct Parse syntax to allow this to happen?
...or must I make two queries instead on one merged query?

Do views only perform the joins that they need to, or all joins always?

I am on an oracle DB. Lets say I have one view that joins to three tables. The view has two fields each. Each field only needs data from two of the three tables.
If I query the view and return only one field, does the view still join to three tables or just to the two tables that it needs to calculate the field?
Generally it will have to hit the three tables.
Consider
SELECT A.VAL, B.VAL, C.VAL FROM A JOIN B ON A.ID = B.ID JOIN C ON A.ID = C.ID
It is possible that a single ID in "A" to have zero, 1 or multiple matches in either B or C. If table "C" were empty, the view would never return a row, so even just querying A.VAL or B.VAL, it would still need to see if there was a corresponding row in "C".
The exception is when, because of an enforced referential integrity constraint, the optimizer knows that a row in 'B' will always have a parent row in 'A'. In that case, a select of B.VAL would not need to actually check the existence of the parent row in 'A'. This is demonstrated by this article
That likely depends on the type of join being used. If they are all inner joins, it will definitely need to examine all three tables.
In general, the database engine would join all three tables to ensure it got the right answer.
Oracle will sometimes eleminate one of the tables where this does not change the result.
This can be done if:-
There is a foreign key constraint to the table to be eleminated (i.e. a row in the table
can be guaranteed to be found)
The table is otherwise unused. i.e. not selected from, in the where clause, etc.

Resources