How can I INNER JOIN another two columns to a table? - sql

I have an SQL statement:
SELECT * FROM newsTable INNER JOIN picTable on picTable.picID = newsTable.newsPicID
This links the selected image from one table to a news story in another table.
This only works for one image, but if I was to have up to 3 images that could be added for one story, what would my new SQL statement look like?
I can't quite get my head round the syntax of the new SQL statement.
Any guidance much appreciated.
newsTable
newsID
newsTitle
newsBody
newsPicID
newsPicIDTwo
newsPicIDThree
picTable
picID
picFileName
picPath

You can join two more times to bring in the picture information:
SELECT t1.*,
COALESCE(t2.picFileName, 'NA') AS picFileName1,
COALESCE(t3.picFileName, 'NA') AS picFileName2,
COALESCE(t4.picFileName, 'NA') AS picFileName3
FROM newsTable t1
LEFT JOIN picTable t2
ON t1.newsPicID = t2.picID
LEFT JOIN picTable t3
ON t1.newsPicIDTwo = t3.picID
LEFT JOIN picTable t4
ON t1.newsPicIDThree = t4.picID
You can add more columns here from the picTable as you need, and possibly use COALESCE() again to provide a default value should a news item not have any picture information.

Maybe like this:
SELECT newsTable.*, pic.*, picTwo.*, picThree.*
FROM newsTable
INNER JOIN picTable AS pic on picTable.picID = newsTable.newsPicID
INNER JOIN picTable AS picTwo on picTable.picID = newsTable.newsPicIDTwo
INNER JOIN picTable AS picThree on picTable.picID = newsTable.newsPicIDThree
if newsPicIDTwo or newsPicIDThree are NULL you schould use an LEFT OUTER JOIN

Just left join the picTable three times:
select *
from newsTable t
left join picTable p1 on p1.picID = t.newsPicID
left join picTable p2 on p2.picID = t.newsPicIDTwo
left join picTable p3 on p3.picID = t.newsPicIDThree

Use LEFT OUTER JOIN to get three pictures :
SELECT * FROM newsTable
LEFT OUTER JOIN picTable PIC1 on PIC1.picID = newsTable.newsPicID
LEFT OUTER JOIN picTable PIC2 on PIC2.picID = newsTable.newsPicIDTwo
LEFT OUTER JOIN picTable PIC3 on PIC3.picID = newsTable.newsPicIDThree

As you describe your problem, you have 1 to many relationship from news to images. That means that each news id can have multiple picds.
Show you don't need newsPicIDTwo and newsPicIDThree fields on newsTable and you need a field newsid on table picTable. Simply, then
SELECT * FROM newsTable INNER JOIN picTable on picTable.newsID = newsTable.newsID
Will bring you the correct result if you have records on picTable that have same newsID but different picID
In other words, a classic 1-Many relationship is resolved correctly when the foreign key lies on the 'Many' table.
In your case the 'Many' table is picTable that owes to have a foreign key newsid.

Just join again on the appropraite columns. I've used Left joins in case one of the pic columns is empty
SELECT NT.*, p1.picFileName as P1File, p2.picFileName as P2File, p3.picFileName as P3File
FROM newsTable NT
LEFT JOIN picTable P1
on P1.picID = NT.newsPicID
LEFT JOIN picTable P2
on P2.picID = NT.newsPicIDtwo
LEFT JOIN picTable P3
on P3.picID = NT.newsPicIDthree

Related

Access 'Join not supported' Moving LEFT Join

I've done some research here and I understand that in Access nested joins cause issues.
I believe that is the issue in the first example.
SELECT
Recipe_Classes.RecipeClassDescription,
Recipes.RecipeTitle,
Recipes.Preparation,
Ingredients.IngredientName,
Recipe_Ingredients.RecipeSeqNo,
Recipe_Ingredients.Amount,
Measurements.MeasurementDescription
FROM (((
Recipe_Classes
LEFT JOIN Recipes
ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID)
INNER JOIN Recipe_Ingredients
ON Recipes.RecipeID = Recipe_Ingredients.RecipeID)
INNER JOIN Ingredients
ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID)
INNER JOIN Measurements
ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID
ORDER BY RecipeTitle, RecipeSeqNo;
I made an attempt to remove the nesting and created a right join in this example
SELECT
Recipe_Classes.RecipeClassDescription,
Recipes.RecipeTitle,
Recipes.Preparation,
Ingredients.IngredientName,
Recipe_Ingredients.RecipeSeqNo,
Recipe_Ingredients.Amount,
Measurements.MeasurementDescription
FROM (((
Ingredients
INNER JOIN Recipe_Ingredients
ON Ingredeints.IngredientID = Recipe_Ingredients.IngredientID)
INNER JOIN Measurements
ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID)
INNER JOIN Recipes
ON Recipes.RecipeID = Recipe_Ingredients.RecipeID)
RIGHT JOIN Recipe_Classes
ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID
ORDER BY RecipeTitle, RecipeSeqNo;
Can anyone point me in the right direction?
The issue may be the left join that is not needed. Try this from clause:
SELECT Recipe_Classes.RecipeClassDescription,
Recipes.RecipeTitle,
Recipes.Preparation,
Ingredients.IngredientName,
Recipe_Ingredients.RecipeSeqNo,
Recipe_Ingredients.Amount,
Measurements.MeasurementDescription
FROM (((Recipes LEFT JOIN
Recipe_Classes
ON Recipe_Classes.RecipeClassID = Recipes.RecipeClassID
) LEFT JOIN
Recipe_Ingredients
ON Recipes.RecipeID = Recipe_Ingredients.RecipeID
) LEFT JOIN
Ingredients
ON Ingredients.IngredientID = Recipe_Ingredients.IngredientID
) LEFT JOIN Measurements
ON Measurements.MeasureAmountID = Recipe_Ingredients.MeasureAmountID
ORDER BY RecipeTitle, RecipeSeqNo;
Once you start with LEFT JOINs, generally all the remaining joins should also be LEFT JOINs.
Since your main focal point table is Recipe_Ingredients, consider starting with this table in FROM and then JOIN the other parent tables and even nest the Recipes and Recipe_Classes pair:
SELECT
c.RecipeClassDescription,
r.RecipeTitle,
r.Preparation,
i.IngredientName,
ri.RecipeSeqNo,
ri.Amount,
m.MeasurementDescription
FROM
((Recipe_Ingredients ri
INNER JOIN (Recipes r
RIGHT JOIN Recipe_Classes c
ON c.RecipeClassID = r.RecipeClassID)
ON r.RecipeID = ri.RecipeID)
INNER JOIN Ingredients i
ON i.IngredientID = ri.IngredientID)
INNER JOIN Measurements m
ON m.MeasureAmountID = ri.MeasureAmountID
ORDER BY r.RecipeTitle, ri.RecipeSeqNo;
Of course this is untested without data. Due to Access' JOIN requirements like parnetheses, it is often recommended for new users to build complex queries with MS Access' GUI Query Design then modify generated SQL as needed.

How to join a same table with two tables?

I want to create an Oracle database view :
create or replace force view view_ind_pta (indi_code, nat_indi_code, indi_unite, indi_symbole, indi_lib, indi_cible, pta_intitule, indi_resp,
indi_source_info, user_code, peri_mes_code, peri_mes_lib, pta_parent, deleted,obj_intitule,pta_action)
as
select distinct
i.indi_code,
i.nat_indi_code,
i.indi_unite,
i.indi_symbole,
to_char(i.indi_lib) as indi_lib,
i.indi_cible,
concat(concat(to_char(a.pta_ref),' - '),to_char(a.pta_intitule)) as pta_intitule,
i.indi_resp,
to_char(i.indi_source_info) as indi_source_info,
u.user_code,
i.peri_mes_code ,
pm.peri_mes_lib ,
concat(concat(to_char(p.pta_ref),' - '),to_char(p.pta_intitule)) as pta_parent,
i.deleted ,
to_char(o.obj_intitule) as obj_intitule,
concat(concat(to_char(action.pta_ref),' - '),to_char(action.pta_intitule)) as pta_action
from
indicateur i
left join acteur_saisie_indicateur ai on ai.indi_code = i.indi_code
left join acteur_verif_indicateur avi on avi.indi_code = i.indi_code
left join utilisateur u on ( ai.user_code = u.user_code and avi.user_code = u.user_code)
left join objectif o on i.obj_code = o.obj_code
left join pta a on o.pta_code = a.pta_code
left join pta action on a.pta_pta_code = action.pta_code
left join pta p on action.pta_pta_code = p.pta_code
left join periodicite_mesure pm on pm.peri_mes_code = i.peri_mes_code
where p.pta_definitif = 3;
In the view there is the table utilisateur which I want to join with the two tables acteur_saisie_indicateur and acteur_verif_indicateur. I tried the and operator , but I think it is not a good idea because the query will return rows only when there are joined rows in both tables ! Although this is not necessary : I want the query to return rows even if only one table has joined rows. So how to join these three tables ?
We can include the same table in a FROM clause more than once. All we need to do is use different aliases to distinguish the instances:
left join utilisateur uai
on ai.user_code = uai.user_code
left join utilisateur uavi
on avi.user_code = uavi.user_code
The other thing you need to do is handle table's columns in the view's projection. You want to display the utilisateur values regardless of which instance the values come from, say by using nvl() or the industry standard coalesce()
coalesce(uai.user_code, uavi.user_code) as user_code
You need to join the table twice:
left join acteur_saisie_indicateur ai on ai.indi_code = i.indi_code
left join acteur_verif_indicateur avi on avi.indi_code = i.indi_code
left join utilisateur u on ai.user_code = u.user_code
left join utilisateur u2 on avi.user_code = u2.user_code

Postgresql - Conditional Join if data exist

My current query show the data from the table called "Buque" and has some references from another tables. The problem is when i execute the query it never shows the result because it consumes too much memory i guess.
The current query i have
select buq.buq_codigo, tbu.tbu_codigo, tbu.tbu_nombre, pai.pai_codigo, pai.pai_nombre,
pue.pto_codigo, pue.pto_nombre, lin.lin_codigo, lin.lin_nombre, tra.tra_codigo,
tra.tra_nombre, buq.buq_nombre, buq.buq_des, buq.num_trb, buq.num_eslora,
buq.max_tons, buq.reg_lloyd, buq.buq_codigo1, buq.codigo_omi,
case buq.buq_estado when 'A' then 'Activo' else 'Inactivo' end as buq_estado
from publico.mae_buque as buq, publico.mae_tipbuque as tbu, publico.mae_pais as pai,
publico.mae_puerto as pue, publico.mae_linea as lin, publico.mae_trafico as tra
where buq.tbu_codigo = tbu.tbu_codigo or
buq.pai_codigo = pai.pai_codigo or
buq.pto_codigo = pue.pto_codigo or
buq.lin_codigo = lin.lin_codigo or
buq.tra_codigo = tra.tra_codigo
I also tried with inner joins but the problem is it returns me the data that meets the conditions of the joins. In other words, if the join has data to compare, returns the name, if not, show the null data.
The query must return me 611 records, with inner joins returns 68 records.
Concerning your desired result, use left outer joins, which fill up any non-existing rows of the right hand side table with null-values;
Concerning the out of memory issue, note that you used or to connect your tables; this actually leads to the fact that almost every record of the involved tables is connected to almost every other record (almost a cross join / cartesian product); This can get very large if you connect 6 tables...
select buq.buq_codigo, tbu.tbu_codigo, tbu.tbu_nombre, pai.pai_codigo, pai.pai_nombre,
pue.pto_codigo, pue.pto_nombre, lin.lin_codigo, lin.lin_nombre, tra.tra_codigo,
tra.tra_nombre, buq.buq_nombre, buq.buq_des, buq.num_trb, buq.num_eslora,
buq.max_tons, buq.reg_lloyd, buq.buq_codigo1, buq.codigo_omi,
case buq.buq_estado when 'A' then 'Activo' else 'Inactivo' end as buq_estado
from publico.mae_buque as buq
left outer join publico.mae_tipbuque as tbu on buq.tbu_codigo = tbu.tbu_codigo
left outer join publico.mae_pais as pai on (buq.pai_codigo = pai.pai_codigo)
left outer join publico.mae_puerto as pue on (buq.pto_codigo = pue.pto_codigo)
left outer join publico.mae_linea as lin on (buq.lin_codigo = lin.lin_codigo)
left outer join publico.mae_trafico as tra on (buq.tra_codigo = tra.tra_codigo)
You have to use left outer join:
select *
from
publico.mae_buque as buq
left outer join publico.mae_tipbuque as tbu on (buq.tbu_codigo = tbu.tbu_codigo)
left outer join publico.mae_pais as pai on (buq.pai_codigo = pai.pai_codigo)
left outer join publico.mae_puerto as pue on (buq.pto_codigo = pue.pto_codigo )
left outer join publico.mae_linea as lin on (buq.lin_codigo = lin.lin_codigo)
left outer join publico.mae_trafico as tra on (buq.tra_codigo = tra.tra_codigo);

Reduce query complexity

I have this multi join query, is there any way to reduce the number of joins? Or maybe to split the query to 2 parts but then still get the same result set?
Having too many joins makes the query execute very slowly and inefficently
SELECT
MD.EVENT_ID,
AUI.ATLAS_USER_ID,
EVENT_TIME,
EVENT_TYPE,
INTERACTION_TOKEN,
CC.COUNTRY_CODE,
AP.ADV_PROJECT_ID, AP.ADV_PROJECT_NAME,
AC.ADV_CAMPAIGN_ID,
ADV_CAMPAIGN_NAME,
PC.PRT_CAMPAIGN_ID, PC.PRT_CAMPAIGN_NAME,
IP.IP_ADDRESS,
OS.OS,
BR.BROWSER,
FU.FULL_USER_AGENT,
RAW_ACTION_ID,
SE.SELLER_NETWORK_ID,
RIURL.RAW_INPUT as URL,
RIREF.RAW_INPUT as REF_URL,
MVG1.MAP_VALUE as TABOOLA ,
MVG2.MAP_VALUE as APPNEXUS ,
MVG3.MAP_VALUE as ETAG ,
MVG4.MAP_VALUE as FACEBOOK ,
MVG5.MAP_VALUE as MEDIAMATH ,
MVG6.MAP_VALUE as COOKIEID ,
MVG7.MAP_VALUE as IDFA ,
MVG8.MAP_VALUE as ADVLOGIN ,
MVG9.MAP_VALUE as OPENX ,
MVG10.MAP_VALUE as ADTRUTH ,
MVG11.MAP_VALUE as GOOGLE ,
MVG12.MAP_VALUE as ANDROID_ADV_ID ,
MVG13.MAP_VALUE as SDGUPI ,
MVG15.MAP_VALUE as RMX
FROM
EVENT_124_2 BASE
INNER JOIN
atlas__atlas_events MD ON BASE.EVENT_ID = MD.EVENT_ID
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG1 ON MVG1.MAP_VALUE_GK = BASE.TABOOLA_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG2 ON MVG2.MAP_VALUE_GK = BASE.APPNEXUS_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG3 ON MVG3.MAP_VALUE_GK = BASE.ETAG_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG4 ON MVG4.MAP_VALUE_GK = BASE.FACEBOOK_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG5 ON MVG5.MAP_VALUE_GK = BASE.MEDIAMATH_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG6 ON MVG6.MAP_VALUE_GK = BASE.COOKIEID_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG7 ON MVG7.MAP_VALUE_GK = BASE.IDFA_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG8 ON MVG8.MAP_VALUE_GK = BASE.ADVLOGIN_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG9 ON MVG9.MAP_VALUE_GK = BASE.OPENX_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG10 ON MVG10.MAP_VALUE_GK = BASE.ADTRUTH_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG11 ON MVG11.MAP_VALUE_GK = BASE.GOOGLE_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG12 ON MVG12.MAP_VALUE_GK = BASE.ANDROID_ADV_ID_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG13 ON MVG13.MAP_VALUE_GK = BASE.SDGUPI_HASH
LEFT JOIN
LOOKUP_STG__MAP_VALUE MVG15 ON MVG15.MAP_VALUE_GK = BASE.RMX_HASH
LEFT JOIN
LOOKUP_STG__ATLAS_USER_ID AUI ON AUI.ATLAS_USER_ID_GK = MD.ATLAS_USER_ID
LEFT JOIN
GAYA__ADV_PROJECTS AP ON AP.ADV_PROJECT_ID = MD.ADV_PROJECT_ID
LEFT JOIN
GAYA__ADV_CAMPAIGNS AC ON AC.ADV_CAMPAIGN_ID = MD.ADV_CAMPAIGN_ID
LEFT JOIN
GAYA__PRT_CAMPAIGNS PC ON PC.PRT_CAMPAIGN_ID = MD.PRT_CAMPAIGN_ID
LEFT JOIN
LOOKUP_STG__RAW_INPUT RIURL ON RIURL.RAW_INPUT_GK = BASE.URL_HASH
LEFT JOIN
LOOKUP_STG__RAW_INPUT RIREF ON RIREF.RAW_INPUT_GK = BASE.REF_HASH
LEFT JOIN
LOOKUP_STG__OS OS ON OS.OS_GK = MD.COUNTRY_CODE
LEFT JOIN
LOOKUP_STG__COUNTRY_CODE CC ON CC.COUNTRY_CODE_GK = MD.COUNTRY_CODE
LEFT JOIN
LOOKUP_STG__BROWSER BR ON BR.BROWSER_GK = MD.BROWSER
LEFT JOIN
LOOKUP_STG__IP_ADDRESS IP ON IP.IP_ADDRESS_GK = MD.IP_ADDRESS
LEFT JOIN
LOOKUP_STG__SELLER_NETWORK_ID SE ON SE.SELLER_NETWORK_ID_GK = MD.SELLER_NETWORK_ID
LEFT JOIN
LOOKUP_STG__FULL_USER_AGENT FU ON FU.FULL_USER_AGENT_GK = MD.FULL_USER_AGENT
;
Do you have any indexes on your key columns? You might try to put bitmap indexes on each key column in the main fact table and also in the dimension tables. That should give you a considerable speedup.
You say that the number of joins makes the query inefficient. But what you should be looking at is whether the tables that are joined with have the proper indexes in them. You obviously need all those fields from the tables you join with, so you can't simply not join with them.
For every JOIN you make in the query, verify that there exists an INDEX on the field that is joined with. For instance:
INNER JOIN
atlas__atlas_events MD ON BASE.EVENT_ID = MD.EVENT_ID
Does the table atlas__atlas_events have an INDEX on the EVENT_ID column?
You need to verify this for every such JOIN in your query. If such an INDEX does not exist you should create one.
If you execute this query in SQL Server Management Studio and include the actual execution plan, you will probably already see indications that you are missing indexes.

How to improve the performance of a SQL query even after adding indexes?

I am trying to execute the following sql query but it takes 22 seconds to execute. the number of returned items is 554192. I need to make this faster and have already put indexes in all the tables involved.
SELECT mc.name AS MediaName,
lcc.name AS Country,
i.overridedate AS Date,
oi.rating,
bl1.firstname + ' ' + bl1.surname AS Byline,
b.id BatchNo,
i.numinbatch ItemNumberInBatch,
bah.changedatutc AS BatchDate,
pri.code AS IssueNo,
pri.name AS Issue,
lm.neptunemessageid AS MessageNo,
lmt.name AS MessageType,
bl2.firstname + ' ' + bl2.surname AS SourceFullName,
lst.name AS SourceTypeDesc
FROM profiles P
INNER JOIN profileresults PR
ON P.id = PR.profileid
INNER JOIN items i
ON PR.itemid = I.id
INNER JOIN batches b
ON b.id = i.batchid
INNER JOIN itemorganisations oi
ON i.id = oi.itemid
INNER JOIN lookup_mediachannels mc
ON i.mediachannelid = mc.id
LEFT OUTER JOIN lookup_cities lc
ON lc.id = mc.cityid
LEFT OUTER JOIN lookup_countries lcc
ON lcc.id = mc.countryid
LEFT OUTER JOIN itembylines ib
ON ib.itemid = i.id
LEFT OUTER JOIN bylines bl1
ON bl1.id = ib.bylineid
LEFT OUTER JOIN batchactionhistory bah
ON b.id = bah.batchid
INNER JOIN itemorganisationissues ioi
ON ioi.itemorganisationid = oi.id
INNER JOIN projectissues pri
ON pri.id = ioi.issueid
LEFT OUTER JOIN itemorganisationmessages iom
ON iom.itemorganisationid = oi.id
LEFT OUTER JOIN lookup_messages lm
ON iom.messageid = lm.id
LEFT OUTER JOIN lookup_messagetypes lmt
ON lmt.id = lm.messagetypeid
LEFT OUTER JOIN itemorganisationsources ios
ON ios.itemorganisationid = oi.id
LEFT OUTER JOIN bylines bl2
ON bl2.id = ios.bylineid
LEFT OUTER JOIN lookup_sourcetypes lst
ON lst.id = ios.sourcetypeid
WHERE p.id = #profileID
AND b.statusid IN ( 6, 7 )
AND bah.batchactionid = 6
AND i.statusid = 2
AND i.isrelevant = 1
when looking at the execution plan I can see an step which is costing 42%. Is there any way I could get this to a lower threshold or any way that I can improve the performance of the whole query.
Remove the profiles table as it is not needed and change the WHERE clause to
WHERE PR.profileid = #profileID
You have a left outer join on the batchactionhistory table but also have a condition in your WHERE clause which turns it back into an inner join. Change you code to this:
LEFT OUTER JOIN batchactionhistory bah
ON b.id = bah.batchid
AND bah.batchactionid = 6
You don't need the batches table as it is used to join other tables which could be joined directly and to show the id in you SELECT which is also available in other tables. Make the following changes:
i.batchidid AS BatchNo,
LEFT OUTER JOIN batchactionhistory bah
ON i.batchidid = bah.batchid
Are any of the fields that are used in joins or the WHERE clause from tables that contain large amounts of data but are not indexed. If so try adding an index on at time to the largest table.
Do you need every field in the result - if you could loose one or to you maybe could reduce the number of tables further.
First, if this is not a stored procedure, make it one. That's a lot of text for sql server to complile.
Next, my experience is that "worst practices" are occasionally a good idea. Specifically, I have been able to improve performance by splitting large queries into a couple or three small ones and assembling the results.
If this query is associated with a .net, coldfusion, java, etc application, you might be able to do the split/re-assemble in your application code. If not, a temporary table might come in handy.