Join Case and User in Salesforce (SOQL) - join

I'm used to PostgreSQL but very new to SOQL and a bit confused about it. I would like to join rows from Case with rows from User based on User.Id.
I've looked online and tried various solutions but nothing worked out.
Does anyone know I could do that? What type of relationship binds Case and User? Do I have to build a custom relationship between them?
Thanks

SOQL supports joins by following the relationship from the FK (using the relationship name), many of the schema explorer tools (SoqlX, Workbench, etc) will help you discover these and build/test SOQL queries.
In this case you want something like
select CaseNumber, LastModifiedBy.Name from Case Where ....

Try this, https://salesforce.stackexchange.com/questions/20572/soql-join-between-two-standard-objects
Select CaseNumber, LastModifiedById, (Select name From User)
From Case WHERE day_only(convertTimezone(ClosedDate))=TODAY

Related

Can't use my joining table from DbContext

I have database with different tables. But I can't get access to some tables, and I'm curious why? For example, there is Projects table and Users table in database, and I need to get list of all users that are in specific Project by Id. These tables have many to many relationship, so there is joining table with ProjectId and UserId. It's logic to use some Join to get users that I need from table Users with help of this joining table from db. But how can I make Join when my DbContext doesn't have joining table? How can I get access to joining table using DbContext, or maybe there are some different ways to get list of users by project id?
If someone have similar problem, here is a simple way to use all the data from collections in model
var project = _db.Projects.Include(x => x.Tickets).ThenInclude(y => y.Users).SingleOrDefault(p => p.Id == id);
so after that you gonna have your model and all the data from collections that you need
ProjectUser - is joining table, use for link two tables. This tables must have (usually) Collection for access to this many-to-many data.
So you can will have collection of users for this project. And doing filtering by users from your Ticket (example from image).
Yes, exists few different ways - setting with ef fluent, or attribute, or dbcontext. Better to find complete guide for many-to-many setting in your application.

Rails HABTM: Select everything a that a record 'has'

I have three tables: projects, technologies, and projects_technologies (a map from project IDs to/from technology IDs). I have two models: projects and technologies. Projects have and belong to many (HABTM) technologies and vice versa. Now I'm trying to get all the names (or active records, if there's a way to do that) of technologies that belong to a given project. Here's the SQL statement I've made, using 5 as the given project ID:
SELECT name FROM technologies WHERE id IN (
SELECT projects_technologies.technology_id FROM projects_technologies
WHERE projects_technologies.project_id = 5
);
I could just connect to the database and run the query, but since I already have the models set up with HABTM relationships, I'm wondering if there's a "Rails way" to execute this. I've been looking at this documentation, http://guides.rubyonrails.org/active_record_querying.html, but I can't figure out if and how I can apply that to this kind of query.
UPDATE
Turns out all I needed was this:
Project.find(5).technologies
The equivalent active record query would be
Technology.select('name').where("id IN (?)", ProjectTechnology.where("project_id = ?", 5).pluck(:technology_id))
Hope this helps!

Neo4j cypher query simulate sql join query

I have two different kinds of node entities without any relationships between them, i wonder how to write a cypher query to join these two node entities by common property?
For example, if i want to return a blog by passing blogId as a parameter, but i also want to return creator username by joining user entity. Is it possible? Thanks in advance.
Why don't you have a relationship between user and blog? As far as graph dbs are concerned to connect data, this will help you to get users' blogs. Further, you can connect the blogs according to their topics etc.

Join queries in Doctrine on tables without specified relations

I have two tables that do not have a relation defined with each other in the schema.yml.
However, table 1 has a foreign key reference to the primary key of table 2. Clearly, I goofed up by not designing the database well, but now it's mitigation time.
I must do a left join between the two tables coupled with a where clause that will retrieve the select rows I want. And to do this, I do:
Doctrine_Query::create()->select('t.*, l.lid')->from('Taxonomy t')->leftJoin('t.Cid c') ->leftJoin('c.Lesson l')->where('t.section = ?','Critical reading');
This should typically do it, but it does not because what it returns is all the rows from taxonomy table irrespective of the where condition. I am thinking, is this because of the relation not being specified in the column? That would be ridiculous cause the query works, only in a doctrine context it does not.
Thanks
In doctrine you can only join using the relations you defined on your schema, this is a know limitation. You may use the Native SQL feature as a workaround.

SQL Relationships

I'm using MS SQL Server 2008R2, but I believe this is database agnostic.
I'm redesigning some of my sql structure, and I'm looking for the best way to set up 1 to many relationships.
I have 3 tables, Companies, Suppliers and Utilities, any of these can have a 1 to many relationship with another table called VanInfo.
A van info record can either belong to a company, supplier or utility.
I originally had a company_id in the VanInfo table that pointed to the company table, but then when I added suppliers, they needed vaninfo records as well, so I added another column in VanInfo for supplier_id, and set a constraint that either supplier_id or company_id was set and the other was null.
Now I've added Utilities, and now they need access to the VanInfo table, and I'm realizing that this is not the optimum structure.
What would be the proper way of setting up these relationships? Or should I just continue adding foreign keys to the VanInfo table? or set up some sort of cross reference table.
The application isn't technically live yet, but I want to make sure that this is set up using the best possible practices.
UPDATE:
Thank you for all the quick responses.
I've read all the suggestions, checked out all the links. My main criteria is something that would be easy to modify and maintain as clients requirements always tend to change without a lot of notice. After studying, research and planning, I'm thinking it is best to go with a cross reference table of sorts named Organizations, and 1 to 1 relationships between Companies/Utilities/Suppliers and the Organizations table, allowing a clean relationship to the Vaninfo table. This is going to be easy to maintain and still properly model my business objects.
With your example I would always go for 'some sort of cross reference table' - adding columns to the VanInfo table smells.
Ultimately you'll have more joins in your SP's but I think the overhead is worth it.
When you design a database you should not think about where the primary/foreign key goes because those are concepts that doesn’t belong to the design stage. I know it sound weird but you should not think about tables as well ! (you could implement your E/R model using XML/Files/Whatever
Sticking to E/R relationship design you should just indentify your entity (in your case Company/supplier/utilities/vanInfo) and then think about what kind of relationship there is between them(if there are any). For example you said the company can have one or more VanInfo but the Van Info can belong only to one Company. We are talking about a one – to- many relationship as you have already guessed. At this point when you “convert” you design model (a one-to many relationship) to a Database table you will know where to put the keys/ foreign keys. In the case of a one-to-Many relationship the foreign key should go to the “Many” side. In this case the van info will have a foreign keys to company (so the vaninfo table will contain the company id) . You have to follow this way for all the others tables
Have a look at the link below:
https://homepages.westminster.org.uk/it_new/BTEC%20Development/Advanced/Advanced%20Data%20Handling/ERdiagrams/build.htm
Consider making Com, Sup and Util PKs a GUID, this should be enough to solve the problem. However this sutiation may be a good indicator of poor database design, but to propose a different solution one should know more broad database context, i.e. that you are trying to achive. To me this seems like a VanInfo should be just a separate entity for each of the tables (yes, exact duplicate like Com_VanInfo, Sup_VanInfo etc), unless VanInfo isn't shared between this entities (then relationships should be inverted, i.e. Com, Sup and Util should contain FK for VanInfo).
Your database basically need normalization and I think you're database should be on its fifth normal form where you have two tables linked by one table. Please see this article, this will help you:
http://en.wikipedia.org/wiki/Fifth_normal_form
You may also want to see this, database normalization:
http://en.wikipedia.org/wiki/Database_normalization

Resources