How do I correctly use LINQ groupby for two tables joined via an intermediate table? - entity-framework

I have three tables.
Interviews
Interviewers
InterviewSchedule.
An interviewer can be scheduled for multiple interviews
An interview can have multiple interviewers.
So,
InterviewSchedule table has columns interviewid, interviewerid. (Many to many relationship)
Interviewtable has columns - InterviewId, InterviewLocation, InterviewSubject.
Interviewer table has columns - InterviewerId, InterviewerName, InterviewerTitle.
Now, I want to generate a report of interviews with the interviewer details.
I created a dataobject as InterviewId, InterviewLocation, InterviewSubject, List<Interviewer>;
I am trying to make one LINQ query to get my output. I use entityframework and already have the context created.
I am kind of new to LINQ, but I see this should be possible and I saw multipleposts from people groupby, using Id.
I think my problem is I want to select multiple fields from both the tables via the intermediate table.
var output = (from i in Interview
join ia in InterviewSchedule on i.interviewid equals ia.interviewid
join iw in Interviewers on ia.interviewerid equals iw.interviewerid)
group i by i.interviewid into g
select new {i, interviewers = new {interviewername, interviewertitle} };
I am lost at this point. Is this not the right approach? Do I have to make a 'for' loop to add all the interviewers to the list, one by one?

Please, try this
var output = (from i in Interview
group i by i.InterviewId into g
join ia in InterviewSchedule on g.Key equals ia.InterviewId
join iw in Interviewers on g.Key equals iw.InterviewerId
select new { g.Key, interviewers = iw }).ToList();
see LINQ: combining join and group by

Related

How does PostgreSQL interpret these two join statements?

I have a question between two very similar PostgreSQL statements:
UPDATE classes SET year = 1
FROM professors WHERE (professors.class = classes.class)
AND professors.name = 'Smith'`
This one seems to inner join the classes table and the professors table, and update only the record in classes where the corresponding professor's name is Smith.
UPDATE classes c SET year = 1
FROM classes cl JOIN professors on (professors.class_id = cl.class_id)
WHERE professors.name = 'Smith'`
This updates every single record in classes. Why is this statement different from the first one?
In the second, you are referring to classes twice. These are two separate references, and the c and cl references are not correlated. In fact, there are no conditions on c, so all rows are updated.
You could add a correlation condition:
UPDATE classes
SET year = 1
FROM classes cl JOIN
professors p
ON p.class_id = cl.class_id
WHERE p.name = 'Smith' AND cl.class_id = classes.class_id;
However, the JOIN is unnecessary and the first query is a better approach (for this purpose).

Laravel 4.2 order by another collections field or result of a function

I have a mongo database and I'm trying to write an Eloquent code to change some fields before using them in WHERE or ORDER BY clauses. something like this SQL query:
Select ag.*, ht.*
from agency as ag inner join hotel as ht on ag.hotel_id = ht.id
Where ht.title = 'OrangeHotel'
-- or --
Select ag.*, ht.*
from agency as ag inner join hotel as ht on ag.hotel_id = ht.id
Order by ht.title
sometimes there is no other table and I just need to use calculated field in Where or Order By clause:
Select *
from agency
Where func(agency_admin) = 'testAdmin'
Select *
from agency
Order by func(agency_admin)
where func() is my custom function.
any suggestion?
and I have read Laravel 4/5, order by a foreign column for half of my problem, but I don't know how can I use it.
For the first query: mongodb only support "join" partially with the aggregation pipeline, which limits your aggregation in one collection. For "join"s between different collections/tables, just select from collections one by one, first the one containing the "where" field, then the one who should "join" with the former, and so on.
The second question just puzzled me for some minutes until I see this question and realized it's the same as your first question: sort the collection containing your sort field and retrive some data, then go to another.
For the 3rd question, this question should serve you well.

Where clause versus join clause in Spark SQL

I am writing a query to get records from Table A which satisfies a condition from records in Table B. For example:
Table A is:
Name Profession City
John Engineer Palo Alto
Jack Doctor SF
Table B is:
Profession City NewJobOffer
Engineer SF Yes
and I'm interested to get Table c:
Name Profession City NewJobOffer
Jack Engineer SF Yes
I can do this in two ways using where clause or join query which one is faster and why in spark sql?
Where clause to compare the columns add select those records or join on the column itself, which is better?
It's better to provide filter in WHERE clause. These two expressions are not equivalent.
When you provide filtering in JOIN clause, you will have two data sources retrieved and then joined on specified condition. Since join is done through shuffling (redistributing between executors) data first, you are going to shuffle a lot of data.
When you provide filter in WHERE clause, Spark can recognize it and you will have two data sources filtered and then joined. This way you will shuffle less amount of data. What might be even more important is that this way Spark may also be able to do a filter-pushdown, filtering data at datasource level, which means even less network pressure.

Filtering in join query

Using crystal reports version 14, MS sql server 2008
I am joining two tables and I need to filter in the join, so if a certain value exists in one of the table, I want to join to that record, if it does not exist, I want to have a null-record. I.e:
select * from sample left outer join test
on(sample.sample_number=test.sample_number and test.name='PREP')
I can run that in Sql server studio and get exactly what I want
What I can get in crystal reports is
select * from sample left outer join test
on(sample.sample_number=test.sample_number)
where test.name='PREP'
In the latter case, rows where test.name='PREP' does not exist will be removed and if there are samples that have no test.name='PREP', those samples will be removed.
Are there any ways I can do this in CR 14?
dummy tables:
Sample
sample_number,name
1,A
2,B
3,C
Test
sample_number,name
1,PREP
1,SOMETHING
2,SOMETHING
3,SOMETHING_ELSE
3,PREP
What I want:
1,A,1,PREP
2,B,NULL,NULL
3,C,3,PREP
(of course there are more fields in the tables and a selection of which fields, but this should illustrate what I want)
I know I can make views and query them directly in crystal, but if possible, I would avoid doing that.
Bah, found it:
Database expert - add table, select data source, add command. Then a custom sql can be added.

Transact-SQL Ambiguous column name

I'm having trouble with the 'Ambiguous column name' issue in Transact-SQL, using the Microsoft SQL 2012 Server Management Studio.
I´ve been looking through some of the answers already posted on Stackoverflow, but they don´t seem to work for me, and parts of it I simply don´t understand or loses the general view of.
Executing the following script :
USE CDD
SELECT Artist, Album_title, track_title, track_number, Release_Year, EAN_code
FROM Artists AS a INNER JOIN CD_Albumtitles AS c
ON a.artist_id = c.artist_id
INNER JOIN Track_lists AS t
ON c.title_id = t.title_id
WHERE track_title = 'bohemian rhapsody'
triggers the following error message :
Msg 209, Level 16, State 1, Line 3
Ambiguous column name 'EAN_code'.
Not that this is a CD database with artists names, album titles and track lists. Both the tables 'CD_Albumtitles' and 'Track_lists' have a column, with identical EAN codes. The EAN code is an important internationel code used to uniquely identify CD albums, which is why I would like to keep using it.
You need to put the alias in front of all the columns in your select list and your where clause. You're getting that error because one of the columns you have currently is coming from multiple tables in your join. If you alias the columns, it will essentially pick one or the other of the tables.
SELECT a.Artist,c.Album_title,t.track_title,t.track_number,c.Release_Year,t.EAN_code
FROM Artists AS a INNER JOIN CD_Albumtitles AS c
ON a.artist_id = c.artist_id
INNER JOIN Track_lists AS t
ON c.title_id = t.title_id
WHERE t.track_title = 'bohemian rhapsody'
so choose one of the source tables, prefixing the field with the alias (or table name)
SELECT Artist,Album_title,track_title,track_number,Release_Year,
c.EAN_code -- or t.EAN_code, which should retrieve the same value
By the way, try to prefix all the fields (in the select, the join, the group by, etc.), it's easier for maintenance.