SQL joining issue - 1 table in multiple join missing an entry - sql

I have a query that joins multiple tables.
QString str1 = "SELECT DISTINCT f.pk_file_ID, f.file_name"
" FROM"
" File_Properties f"
", Video v "
", Audio a"
", Transport_Stream ts"
", TS_Data tsd"
", Signal_Root sr"
", Test_Case tc"
" WHERE"
" v.fk_file_ID = f.pk_file_ID"
" AND a.fk_file_ID = f.pk_file_ID"
" AND ts.fk_file_ID = f.pk_file_ID"
" AND tsd.fk_file_ID = f.pk_file_ID"
" AND sr.fk_file_ID = f.pk_file_ID"
" AND tc.fk_file_ID = f.pk_file_ID" + clause;
My app uses comboboxes that allow a user to select video file criteria which is used to fetched and display file data in a table. The selected criteria is stored in the clause variable and is used to retrieve the matching files in the database.
The problem here is that a video file can have 0 to many Audio and Video entries. If, for example, a file does not contain any Audio data, there is no entry made in the Audio table. Therefore, this join does not work for retrieving files that do not have an Audio/Video entry.
One way around this would be to make a blank record in the tables where a video file has no Audio/Video data. However, I don't want to have blank records in the tables.
Is there a better way to join the tables?
Thanks

Have a look at the Left outer Join. Values of the left (in this case file_properties) table are always displayed, even when they have no corresponding record in the right (in this case audio and video) table.

Related

Access SQL formatting question inner join with subselect

I have some access DB tables. EMails with a subject line, Emp_Names with FN and LN, and keywords keywords.
I want to get all emails with a subject line that has the keywords and doesn't have any of the Employee's first and last name.
First part is easy:
Select * From Emails Inner Join keywords on InStr(" " & Emails.Subject, " " & keywords.keywords)
The space is added to if a keyword is Pinion I won't get opinion and adding to start of subject if it exists as first word.
However, I can't seem to either properly SQL Code access to Select the derivative Table to only include those subject of emails where Emp_Names.FN and LN don't appear in the subject. I've tried sub-selecting a derivative table of just the subjects without employee names before looking for keywords, but I always get syntax errors.
SELECT * From (
Select * From Emails inner Join Emp_names on InStr(Emails.subject, emp_names.FN & " " emp_names.LN)=0)
Inner Join keywords on InStr(" " & Emails.Subject, " " & keywords.keywords)
I get syntax error on Join Operation, highlights Emails first instance.

Dynamic Selection Query with Criteria in Ms Access | SQL

I have a query in which I select multiple values. To create my form.
The results look like this :
the last Column [Candidat Nom] is a drop down list with an other query (lets call it Query 1) that select my drop down list values.
The selection is good and is what I'm looking for. Except I get the same value for all lines when they need to be different.
To simplify
let's take the following exemple
I have the following candidate that wants to join for the following job (Represented with their ID).
As we can see 2 candidates wants job N° 12. and 1 candidate for each other job.
What I get
All candidates are listed for every job.
What I want
What I actually did
is I put the following query (Query 1) on my column.
SELECT T_SALARIE.SALARIE_nom & " " & T_SALARIE.SALARIE_prenom AS Candidat Nom
FROM T_EMPLOI INNER JOIN (T_SALARIE INNER JOIN (T_SALARIE_EMPLOI LEFT JOIN T_STATUT_EMPLOI ON T_SALARIE_EMPLOI.SALARIE_EMPLOI_statut_id = T_STATUT_EMPLOI.STATUT_EMPLOI_id) ON T_SALARIE.SALARIE_NNI = T_SALARIE_EMPLOI.SALARIE_EMPLOI_salarie_nni) ON T_EMPLOI.EMPLOI_identifiant = T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant
WHERE (((T_STATUT_EMPLOI.STATUT_EMPLOI_statut) Like "*valid*" Or (T_STATUT_EMPLOI.STATUT_EMPLOI_statut) Like "*décidé*") AND ((T_EMPLOI.EMPLOI_entreprise_id)=1));
This gave me the result I want but with the issue I mentioned previously (Same result for each line)
So
I thought I needed a new Criteria.
I added one, where It's going to select the candidate when the two "emploi ID" of my actual table (Shown before) and the one helping me select the candidates are equal.
With the following query:
SELECT T_SALARIE.SALARIE_nom & " " & T_SALARIE.SALARIE_prenom AS Candidat, T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant
FROM T_EMPLOI INNER JOIN (T_SALARIE INNER JOIN (T_SALARIE_EMPLOI LEFT JOIN T_STATUT_EMPLOI ON T_SALARIE_EMPLOI.SALARIE_EMPLOI_statut_id = T_STATUT_EMPLOI.STATUT_EMPLOI_id) ON T_SALARIE.SALARIE_NNI = T_SALARIE_EMPLOI.SALARIE_EMPLOI_salarie_nni) ON T_EMPLOI.EMPLOI_identifiant = T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant
WHERE (((T_STATUT_EMPLOI.STATUT_EMPLOI_statut) Like "*valid*" Or (T_STATUT_EMPLOI.STATUT_EMPLOI_statut) Like "*décidé*") AND ((T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant)=[R_Select_COMOB]![ACTION_identifiant_emploi]));
But I keep on getting the following pop up that asks me to enter a Job ID
So how can I make the query for each line compare and select the right values?
I hope I was clear in explaining. If not please let me know so that I can add more details.
Thank you !
Thanks to your help and specially #June7 for his propositions, I found a solution regarding my problem :
I added a criteria to select values based on JobID that I wasn't selecting in the first hand.
And then based on the column (jobID) select the values needed
here is my final query :
SELECT
[SALARIE_nom] & " " & [SALARIE_prenom] & " (" & [SALARIE_NNI] & ")" AS Salarié, T_SALARIE_EMPLOI.SALARIE_EMPLOI_salarie_nni, T_SALARIE_EMPLOI.SALARIE_EMPLOI_id, T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant
FROM
(T_STATUT_EMPLOI INNER JOIN T_SALARIE_EMPLOI ON T_STATUT_EMPLOI.STATUT_EMPLOI_id = T_SALARIE_EMPLOI.SALARIE_EMPLOI_statut_id) LEFT JOIN R_Select_Salarie ON T_SALARIE_EMPLOI.SALARIE_EMPLOI_salarie_nni = R_Select_Salarie.SALARIE_NNI
WHERE
(((T_SALARIE_EMPLOI.SALARIE_EMPLOI_emploi_identifiant)=[Formulaires]![F_COMOB]![ACTION_identifiant_emploi]) AND ((T_STATUT_EMPLOI.STATUT_EMPLOI_statut) Like "*validé*") AND ((T_SALARIE_EMPLOI.SALARIE_EMPLOI_Entreprise) Like "*RTE*"));
And Then to update my values for each line. I added a VBA code that requery on input.
Private Sub ACTION_Candidats_P_Enter()
ACTION_Candidats_P.Requery
End Sub
With that my problem is solved.

How to query twice at once from one table with inner join SQL ASP.NET

I have two tables first table called TFile contains two columns: FromCity and ToCity.
They will have different values but from one column of the second table (TCity) and specifically from the column called CityName.Second table name TCity they have two column : IdCity AND CityName.
My problem I need to display data for two columns they got from second table FromCity and ToCity with inner join for two times.
I use this code to do that:
SqlCommand comm = new SqlCommand("select * from TFile " +
"inner join TCity AS A ON TFile.FromCity = A.IdCity " +
"inner join TCity AS B ON TFile.ToCity = B.IdCity " + " WHERE " + "TFile.Name", con);
Then display data to users as:
SqlDataReader srd = comm.ExecuteReader();
if (srd.HasRows)
{
while (srd.Read())
{
//FromCity
TextFrom.Text = srd["CityName"].ToString();
//ToCity
TextTo.Text = srd["CityName"].ToString();//=======================here problem
}
}
In the first line of the data display I can get the name of the city but if I repeat that in the second line it will just repeat the data. Here problem.I can't use a different name to access the second query instead of the field name CityName.This is the name of the field in the second table for which I display the names of the cities.
How can I access to data in this query:
"inner join TCity AS B ON TFile.ToCity = B.IdCity
So if I access to it then can display second data in this line:
TextTo.Text = srd["CityName"].ToString();
How can solve this problem ?
I bet you need to create field aliases to differentiate between values from the multiple joins on the same table. By the way, please make sure you protect yourself from "SQL injection" when using these types of queries. If you don't know what "SQL Injection" is then please take some time to look it up.
"select FromCityName=A.CityName, ToCityName=B.CityName, * from TFile "
And
//FromCity
TextFrom.Text = srd["FromCityName"].ToString();
//ToCity
TextTo.Text = srd["ToCityName"].ToString();//=======================here problem

Use joined column for further calculations after Join in Qlik Sense

I am going to create a dashbord for our self activities and therefore have joined two fact tables.
Now, i want to make further calculations with the new columne but QlikSense shows an error and indiacted that the columne "DeliveredQuantity" couldn't be found.
I will show you my statement:
ORDERPOSITION:
LOAD
AUFK_NR&'|'&FIRM_NR as %AUFK,
(FIRM_NR&'|'&AUFK_NR&'|'&AUFP_NR) AS %AUFPdelivered,
AUFP_NR,
ART_NR as %ART_NR,
ART_NR,
(AUFP_PREIS2/AUFP_PEHFAKTOR/AUFP_PREISFAKTOR)*DeliveredQuantity AS TurnoverOrderPosition,
FROM [lib://QVD/HKTRS2_V_AUFP.qvd]
(qvd);
LEFT JOIN (ORDERPOSITION)
LOAD (FIRM_NR&'|'&AUFK_NR&'|'&AUFP_NR) AS %AUFPdelivered,
AUFL_MENGE AS DeliveredQuantity
FROM [lib://QVD/HKTRS2_V_AUFL.qvd]
(qvd);
You have to first join the tables. Once the tables are joined and all fields are in the same table, then the field can be used to perform the calculation
Something like this:
// First step is to join both tables
// Prepare the calculated field TurnoverOrderPosition_Temp
ORDERPOSITION_Temp:
Load
AUFK_NR & '|' & FIRM_NR as %AUFK,
FIRM_NR & '|' & AUFK_NR & '|' & AUFP_NR AS %AUFPdelivered,
AUFP_NR,
ART_NR as %ART_NR,
ART_NR,
AUFP_PREIS2 / AUFP_PEHFAKTOR / AUFP_PREISFAKTOR AS TurnoverOrderPosition_Temp,
From
[lib://QVD/HKTRS2_V_AUFP.qvd]
(qvd)
;
Left Join (ORDERPOSITION)
Load
FIRM_NR & '|' & AUFK_NR & '|' & AUFP_NR AS %AUFPdelivered,
AUFL_MENGE AS DeliveredQuantity
From
[lib://QVD/HKTRS2_V_AUFL.qvd]
(qvd)
;
// Once the tables are joined then we can
// create the final calculation
ORDERPOSITION:
Load
*,
TurnoverOrderPosition_Temp * DeliveredQuantity as TurnoverOrderPosition
Resident
ORDERPOSITION_Temp
;
// we dont need this table anymore
Drop Table ORDERPOSITION_Temp;
// we dont need this field anymore
Drop Field TurnoverOrderPosition_Temp From ORDERPOSITION;

SQL - select 5 columns where 3 are distinct using multiple JOINs

I searched Stack Overflow before asking and looked through the 5 most relevant questions, but they did not seem to answer this.
I need to select 5 columns from a table using multiple INNER JOINs, but I only want to get the records where 3 of the 5 columns are distinct. If I use:
"select DISTINCT pos_segments.pos, ",
" pos_segments.org, ",
" pos_segments.obj, ",
" pos_segments.proja, ",
" pos_segments.eff_dt ",
" from pos_segments ",
"INNER JOIN PersonnelPositions ",
"ON pos_segments.pos = PersonnelPositions.Position ",
"AND pos_segments.eff_dt = PersonnelPositions.EffectiveDate ",
"INNER JOIN Accounts ",
"ON PersonnelPositions.PayrollGLAccountId = Accounts.Id ",
" WHERE <where clause here>
I get back over 22k records. I need to get the pos_segments columns only where the combination of the following three columns are distinct:
pos_segments.pos, pos_segments.proja, pos_segments.eff_dt
These three columns taken together serve as a unique key for this table. How can I only get back the records which are distinct based on the combination of these three columns?
P.S. - we are using MS SQL Server
Thanks!
Edit: As Sean pointed out, the ORDER BY statement is not optional. I never tried without it so I wasn't sure. And it would appear that the code block below is more of a suggestion than a copy paste so take it with a grain of salt. However, this approach should still work with some tweaking to fit your application. Since you mentioned that it may be arbitrary which distinct row is grabbed, you can put either of the other two columns after the ORDER BY statement.
I very recently needed to solve a very similar issue. I was able to accomplish what I needed by using a partition in conjunction with a where statement. You code would be modified to look like this:
"select * from ("
"select ROW_NUMBER() OVER (PARTITION BY pos_segments.pos,
pos_segments.proja, pos_segments.eff_dt ORDER BY pos_segments.org)
AS row_num_throwaway"
" pos_segments.pos, ",
" pos_segments.org, ",
" pos_segments.obj, ",
" pos_segments.proja, ",
" pos_segments.eff_dt ",
" from pos_segments ",
"INNER JOIN PersonnelPositions ",
"ON pos_segments.pos = PersonnelPositions.Position ",
"AND pos_segments.eff_dt = PersonnelPositions.EffectiveDate ",
"INNER JOIN Accounts ",
"ON PersonnelPositions.PayrollGLAccountId = Accounts.Id ",
" WHERE <where clause here>)"
"WHERE row_num_throwaway = 1"
That first row_number partition line just finds distinct versions of the three columns specified and assigns a row number that counts up as more instances of that distinct group are found. By only looking where row_num_throwaway = 1, you are only getting the first time that combo is present. To ensure you retrieve the correct entry, you can always add an ORDER BY statement where I showed with the stars.
Hope this helps!