Django foreign key column replace - django

I have class A in database for example:
ID COL1 COL2 COL3
1 a b c
And column B in database:
ID COL1 COL2 FK_COL
1 d e 1
2 dd ee 1
3 ddd eee 1
class B have foreign key to class A. How can I create a query to get all objects from table B where foreign key is 1 but instead of ID from A to be COL1 from A.
ID COL1 COL2 FK_COL
1 d e a
2 dd ee a
3 ddd eee a

If I understand your question correctly, you can use ForeignKey.to_field. From the documentation:
ForeignKey.to_field
The field on the related object that the relation is to. By default, Django uses the primary key of the related object.
EDIT: For the additional question in the comment, you'd do something like:
b_obj = B.objects.filter(a=1).values('colX')
Or, if you're coming from the A object:
a_obj = A.objects.get(pk=1)
b_objs = a_obj.b_subset.values('colX')
colX is the column you want displayed, and b_subset will depend on the name of the related model. If you do dir(a_obj) it will be obvious which one is it, but it should be the name of the model plus "_set".
SECOND EDIT: values can also follow relations, so to answer what is ultimately your question, you would do something like:
b_objs = B.objects.filter(a=1).values('COl1', 'COL2', 'FK_COL__COL1')
This gets you the value of the COL1 from the A table (note the double underscore to follow the relation).

Related

Create an indicator variable for a table relationship after an outer join

I am working with data from a data cube, meaning I cannot easily control the structures of my underlying data. My underlying data looks like the below. As a Cube datasource, each one of these columns also is its own table with define join relationships between one another. For example, to select the Group1 column on its own would look like 'Group1'[Group1].
Group 1
Group2
Group3
Desired Column
1
a
x
1
2
a
y
1
3
a
y
1
4
b
y
1
5
b
y
1
6
c
z
0
I am trying to create a variable that reflects what is shown in "Desired Column" so that I can include results for any value of Group2 that matches to the "y" value of Group3.
I am not very fluent with DAX, but my preliminary thoughts on an approach are to run a FILTER on Group3 for "y", select values of Group2, and then somehow use those Group2 values in a CONTAINS statement in a further FILTER. Here is my current, non-functional attempt:
Is_Y = CONTAINS(NATURALLEFTOUTERJOIN('Group2','Group3'), 'Group3'[Group3], "Y")

Check if value is in another table and add columns in Power BI

I have 2 tables, table1 contains some survey data and table2 is a full list of students involved. I want to check if Name in table2 is also found in table1. If yes, add Age and Level information in table2, otherwise, fill these columns with no data.
table1:
id Name Age Level
32 Anne 13 Secondary school
35 Jimmy 5 Primary school
38 Becky 10 Primary school
40 Anne 13 Secondary school
table2:
id Name
1 Anne
2 Jimmy
3 Becky
4 Jack
Expected output:
id Name Age Level
1 Anne 13 Secondary school
2 Jimmy 5 Primary school
3 Becky 10 Primary school
4 Jack no data no data
Update:
I created a relationship between table1 and table2 using the common column id(which can be repeated in table1).
Then I used:
Column = RELATED(table1[AGE])
but it caught error:
The column 'table1[AGE]' either doesn't exist or doesn't have a relationship to any table available in the current context.
There are various ways to achieve the desired output, but the simplest of them I found is to use the RELATED DAX function. If you found this answer then mark it as the answer.
Create a relationship between table1 and table2 using 'Name` column.
Create a calculated column in table2 as:
Column = RELATED(table1[AGE])
Repeat the same step for the Level column also.
Column 2 = RELATED(table1[LEVEL])
This will give you a table with ID, Name, Age, and Level for the common names between the two tables.
Now to fill those empty rows as no data, simply create another calculated column with following DAX:
Column 3 = IF(ISBLANK(table2[Column]), "no data", table2[Column])
Column 4 = IF(ISBLANK(table2[Column 2]), "no data", table2[Column 2])
This will give you the desired output.
EDIT:- You can also use the following formula to do the same thing in a single column
Column =
VAR X = RELATED(table`[AGE])
VAR RES = IF(ISBLANK(X), "no data", X)
RETURN
RES
and
Column 2 =
VAR X = RELATED(table1[LEVEL])
VAR RES = IF(ISBLANK(X), "no data", X)
RETURN
RES
This will also give you the same output.

data model in Power BI with multiple tables each has an FK

Assume I have the following tables:
Table 1:
Prj_id, name
1 , prj1
2 , prj2
3 , prj3
Table 2
prj_id, cost, year
1 ,100 ,1999
1 ,200 ,2000
1 ,300 ,3000
2 ,150 ,1998
Table 3
Prj_id, manager
1 , xxx
1 , yyy
2 , xxx
3 , zzz
Now my question is:
Shall I connect all tables together in the model (relation tab) of BI, if I have all the attributes as filters on the page
i.e.
Table 1 to 2 ->Both direction
Table 1 to 3 ->Both direction
Table 2 to 3 ->Both direction
or shall I need to only join tables as follows:
Table 1 to 2->Both direction
Table 1 to 3->Both direction
That highly depends on what your final goal is.
However I would create a relationship between this tables. As I looks, these data coming from a database with a good structure, so why joining every together again.

Redshift table update using two tables

I have a requirement to work on an update. The requirement is to update table 2 using the data from table 1. Please find below sample records from the two tables:
TABLE A
-----------------
colA | colB | colC
-----------------
1 AAA ABC
2 BBB DEF
3 CCC GHI
3 CCC HIJ
TABLE B
-----------------
colA1 | colB1 | colC1
-----------------
1 AAA
2 BBB
3 CCC
3 CCC
I need to update the colC1 with values of ColC. Expected output is shown below
TABLE B
-----------------
colA1 | colB1 | colC1
-----------------
1 AAA ABC
2 BBB DEF
3 CCC GHI
3 CCC HIJ
Do we need to use a cursor for this or a simple update statement like shown below would do?
Update table B
set colC1 = table A.colC
from TABLE A, TABLE B
where colA1 = colA
and colB1 = colB;
Your SQL seems perfectly fine.
Cursors are normally used for programmatic access to a database, where the program is stepping through the results one-at-a-time, with the cursor pointing to the 'current record'. That isn't needed in normal SQL update statements.
One thing to note... In Amazon Redshift, using an UDPATE on a row causes the existing row to be marked for deletion and a new row is created. (This is a side-effect of using a columnar database.) If many rows are updated, it means that the disk storage becomes less efficient. It can be improved by occasionally running VACUUM tablename, which will remove the deleted storage.

Entity Relationship Diagram: Composition of primary and foreign keys

I have defined the following tables with their attributes:
Table A) Table B) Table C)
- ID - ID - ID1
- Name - xxx - ID2
- Address - yyy - zzz
- ...
- ...
I have two questions:
1) In Table A), ID is my primary key. The ID attribute in Table A) and Table B) are the same and I am not sure if in Table B) ID can be used as a primary and foreign key? Apparently, ID is also a primary key in Table B) but at the same time a foreign key for Table A). Is that allowed?
2) In Table C) I need both attributes ID1 and ID2. None of the two by itself is a primary but they ned the attribute zzz to be. However, the two attributes ID1 and ID2 are more or less interchangeable so that I have the possibility to create two different primary keys: 1) ID1 + zzz OR 2) ID2 + zzz. I am not sure if it matters which key to choose or if this scenario is allowed at all. Or is the primary key in this particular case the combination of all 3 attributes ID1, ID2 and zzz?
If ID in A and B is the same then you can merge the two tables and put same ID:
Table AB)
-ID
-name
-address
-xxx
-yyy
so ID should not be the same...
You can use the primary key as combination of ID1 and ID2