How to join this tables properly? - join

I have 2 tables, one is for the categories and the other is for the particulars of the corresponding categories.
I need all categories to be shown with their corresponding particulars.

I am not clear about your question and i assume you are using sql server
select t1.category,t2.particularname from categories t1 left join particulars t2 on t1.categoryid=t2.categoryid

From what you said, it seems Categories table has one-to-many relation with particulars. That means Primary Key of Categories should be refered in particulars as Foreign Key in Particulars. You can use following Query to join
SELECT * FROM categories C JOIN particulars P ON C.cat_id = P.cat_id;
And you should rename if some other columns in both tables have same name and you don't want them to be a part of JOIN.
For more example on JOIN.

Related

Can i join three tables in snappy data

I need to perform colocate join on category and subscriber tables
And i also need to perform colocate join on category and likes tables
can i denfine colocate_with option as more than one table
Define one leader region and colocate all tables with it. So by transitivity, everyone will be colocated.
e.g. table A is colocated with table B, table C is colocated with table B => Hence Table C is also colocated with table A

Sequelize : how to join on foreign keys

I'm using Sequelize and Node.js, and I need to join 2 tables on 2 foreign keys,
tableA.hasOne(tableB, { foreignKey: 'fk_tableB' });
tableB.belongsTo(tableA, {foreignKey: 'fk_tableB' });
(by the way, I don't understand the functionality of "targetKey")
but I can only obtain a join on tableA.primaryKey = tableB.fk_tableB.
How can I replace the tableA.primaryKey by tableA.fk_tableA?
I also tried to define twice tableA : in 2 different structures (one with the real primary key and the other with fk_tableA as primary key), but it's also not working (because I need the real tableA mode in another place).
Has someone an idea? Is it a bug from Sequelize?
How can I replace the tableA.primaryKey by tableA.fk_tableA?
There is no tableA.fk_tableA. But if there were, we would expect you to have named it that because column tableA.fk_tableA is a FK to a key column in tableA. Because that's the convention for naming a column fk_tableA. Similarly we would expect a belongTo like yours that adds a column that is a FK to the tableA PK to call it fk_tableA, not fk_tableB. Just like your hasOne gives tableA a column fk_tableB to the tableB PK. (If you want a FK to be to some other column than the PK then you say so via targetKey.)
If you so named FKs after their target table, you seem to want tableA.fk_tableB = tableB.fk_tableA. The way you have named them now, you seem to want tableA.fk_tableB = tableB.fk_tableB.
I need to join 2 tables on 2 foreign keys
It is extremely unlikely that you need the join above. Declaring a column to be a FK says that a value of the source/referencing column is always a value of the target/referenced column. Here targets are PKs. Such a join on a FK to one table and a FK to another table will only return rows that share the same PK value, even though the PKs are from different tables.

Join tables in Hive using LIKE

I am joining tbl_A to tbl_B, on column CustomerID in tbl_A to column Output in tbl_B which contains customer ID. However, tbl_B has all other information in related rows that I do not want to lose when joining. I tried to join using like, but I lost rows that did not contain customer ID in the output column.
Here is my join query in Hive:
select a.*, b.Output from tbl_A a
left join tbl_B b
On b.Output like concat('%', a.CustomerID, '%')
However, I lose other rows from output.
You could also achieve the objective by a simple hive query like this :)
select a.*, b.Output
from tbl_A a, tbl_B b
where b.Output like concat('%', a.CustomerID, '%')
I would suggest first extract all ID's from free floating field which in your case is 'Output' column in table B into a separate table. Then join this table with ID's to Table B again to populate in each row the ID and then this second joined table which is table B with ID's to table A.
Hope this helps.

Issues with joining multiple tables

I'm really struggling at the moment trying to work out how to join multiple tables without duplicating data.
At the moment I have 8 tables that I was wanted to get various information from per member of staff like the below:
SDQ score, Goal scores, CHI score, number of appointments, number of dna appointments
The tables and field I can see to join are as follows
tblSDQ - Assessed_By_Staff_ID
tblGoals - Recorded_By_Staff_ID
tblCHI - Recorded_By_Staff_ID
tblReferral - Staff_ID
tblStaff - Staff_ID
tblDiaryAppointment - needs to connect to tblDiaryAppointmentClinician using Clinician_Invitee_Staff_ID
I hope someone can help or advice. I just don't know if it's even possible to join all these tables using the same field, or if its possible to join them but then return a number of entries but then just count others?
Syntax depends on a rdbms you are using.
You could use join with specified join fields from both tables:
select bla-bla
from table1
join table2 on ( table1.fileld_name1 = table2.fileld_name2 )
https://dev.mysql.com/doc/refman/5.0/en/join.html
if you need outer join (to show nulls for optional tables data) you could use this:
join table2 on ( table1.fileld_name1 = table2.fileld_name2 or table2.field_name2 is null )
to join with couns you could use subqueries like this
join ( select field_name3, coint(*) as cnt from table3 goup by field_name3 ) AS table3_counts
...
where ( table3_counts.field_name3 = ... or table3_counts.field_name3 is null )
https://dev.mysql.com/doc/refman/5.0/en/from-clause-subqueries.html
PS: Joins are often slow. It's better to denormalize tables to eliminate joins and gain performance. Or do simple selects and join in backend code.

Salesforce: SOQL - Top viewed articles titles

Article title is present in KnowledgeArticleVersion table and View Normalized Score is present in KnowledgeArticleViewStat table. The parent of both tables is KnowledgeArticle, but I can't join it to KnowledgeArticleVersion, because there is no relation name present in DB schema. Of cos, I can at first execute such query
Select k.ParentId, k.NormalizedScore From KnowledgeArticleViewStat k order by k.NormalizedScore
Then
SELECT Title, UrlName, KnowledgeArticleId FROM KnowledgeArticleVersion WHERE PublishStatus='Online' AND language ='en_US' and KnowledgeArticleId in (:ids)
But my religion forbids me from executing two queries instead of one.
Maybe someone can tell me the right way to perform join in SOQL.
Assuming the k.parentIds is what you use in :ids this would work:
SELECT id,Title , UrlName, KnowledgeArticleId, PublishStatus, language
FROM KnowledgeArticleVersion
WHERE PublishStatus='Online'
AND language ='en_US'
AND KnowledgeArticleId IN (SELECT ParentId
FROM KnowledgeArticleViewStat
)

Resources