SQL join using EF Core - sql

I am a bit stuck and would appreciate some help.
I have the following data structure as shown in the image below.
So I want to link C and D basically apply the list to list. And if possible how would I do this in ef core. I did try a different query
SELECT * FROM A a
JOIN B b on a.PKA= B.PKB
But I only get the multiplication of list every record from table B got a pair for every record of table A. One more question if records in table C don't have the same number of record in table D it values should be null

Related

Merging 2 tables from two different datasets SQL Using Google BigQuery

Doing a project and have been stuck.
The two datasets are called mimic3_demo (c) and mimic3_demo_omop (m).
I need to merge one table from c to one table from m. The first table from dataset c is PATIENTS and the second table from dataset m is condition_occurrence.
I'd like to join them on SUBJECT_ID from database c table PATIENTS and person_id from database m table condition_occurrence.
This is the simple code I've used so far but it keeps saying "there is no data to display"
I am using Google Big Query to code this was what the course provided us.
Select *
from mimic3_demo.dbo.PATIENTS c
Join mimic3_demo_omop.dbo.condition_occurrence m on c.SUBJECT_ID = m.person_id
Please help! Thanks.

Yii2 : Joining child table to two parent tables

I have two parent tables and 1 child table in this format -
A - a_id, a_name (parent)
B - b_id, b_name (parent)
C - c_id, a_id, b_id
aid and bid are foreign keys in table C.
I am trying to create a dataProvider starting from Model A, and want to join C to the picture such that the code looks like
$query->joinWith([B,A&B.C]
But I don't know how to make C join to both A and B for the given query using Yii2 standards. Any help would be appreciated.
Thanks
To make JOIN with Yii2 through multiple DataProvider models you can use simple:
$results = $tableADataProvider->joinWith(['cRelation.bRelation')
->where([...]);
And of course in A table DataProvider you need to have relation to C table, in this example cRelation and in C table DataProvider you need to have relation to B table bRelation.
Also to mention IF you don't use any column from tables B and C in WHERE clause, but just you want to retrieve results from B then it's much better to use $tableADataProvider->with([...]) which fetches data trough another query. This way even if they are pivot tables, you will get correct count of Table A.
Worthy to note that you probably didn't get correct count (count that you wanted) if table(s) is/are pivot table(s).
(Pivot table is a table that have more than 1 record that have same unique key of other table)

Joining two tables to update results in third table using like

I am trying to join two tables and update the results in the third table.
So table A is the results table and it has the columns customer number and score.
Table B has customer number and ind_code and table C has ind_code and ind_score.
So the output of the query should be such that the ind_code in table B and C should join together based on the first two digits and ind_score should be updated in Table A in the score column. Table A and Table B should be joined on the basis of customer number.
Could anyone please help. I tried multiple queries but nothing seems to work. i am using oracle sql developer
Generally, the JOIN operation mustn't cut field information but if your structure (for me not correct) is that ...
If I understand better:
UPDATE TableA
SET score =
(SELECT MAX(C.ind_score)
FROM TableC C
JOIN TableB B
ON C.ind_code = SUBSTRING(B.ind_code, 1, 2)
WHERE B.customernumber = TableA.customernumber)
I use on subquery MAX aggregate function, because I don't know if your cut of ind_code of TableB can be not unique (i.e. you have ind_code 5555 and 5554)

How to know the number difference between 2 tables in sql

I really need to know the query to display another field which is I want to name it as "number_difference" between 2 tables that required a numeric (in this case as a quantity).
I have 2 tables that totally same, let say, I modify the value from table A so some value in table A different with table B. And I want to join it into 1 table that display rows that some values were modified. I already get the result by this query :
**
select a.T1, a.T2 a.T3 ... from A where not exists (select * from B
where a.T1=b.T1 and a.T2=b.T2 and a.T3=b.T3)
**
This query works well. But, I want to add more field, the difference number between this 2 field (quantity) in 2 different tables. So let say, a.T3 and b.T3 are quantities. And want to display it as "number_difference" next to field (T2) which I display. Sorry I can't post images, they say I need at least 10 reputation to post. Please help me master, how can I make it everytime I use join/inner join it always display soo many rows that I only need the rows which a value from 1 table I have modified.
Thanks in advance.
You can get something like this
Select * From A Where A.id not in( Select Id from B)
or
Select A.* From A left join B on A.id = B.id Where A.id <> B.id
If you are specifically targeting a situation with two identical tables, where one underwent UPDATE operations only (no INSERT), and you want to identify those records which were modified, then:
select a.* from a, b where a.id=b.id and ( a.c1!=b.c1 or a.c2!=b.c2)

Trying to design a summary table in SQL using multiple others

Very new to SQL programming so apologies if my terminology is a little off.
I need some help combining a few tables, however I'm worried that it's not even possible!! (but what do I know)
So I have 1 table that is just a list of feeds. A, B, C, D etc (15 rows in total) and each feed has other information attached to it such as Full name, location etc etc. I am only interested in the Feed column.
Each feed then has a corresponding data table which contains a list of records. Eg Feed A data is contained in TableA, Feed B data is contains in TableB and so on.
Basically what I need is a combined table that shows the list of Feeds in the 1st Column ( So A, B, C, D…..) and then a second column which counts the records from each separate data table corresponding to that feed.
So the end result would look something like this:
Feed No of Records
A 4 (from TableA)
B 7 (from TableB)
C 8 (from TableC)
D 1 (from TableD)
Possible?
Any help on sql code would be much appreciated.
Thanks