Create Table Subquery for a join clause - sql

So the problem here is OPTION_NAME in neo_product_benefit has no relation to the other tables, but it has a relationship to the neo_claims_pmb_details table via OPTION_ID column and in the neo_claims_pmb_details table it has a BENEFIT_ID column which can be joined to the other tables.
So in short
I'm not entirely sure how the SQL would look like to get the OPTION_NAME and join it to the other tables, so I thought creating a temporary table would work and joining it and then dropping it aftewards but I have no idea how the syntax would work
Any help would be appreciated.
SELECT a.batch_id,
a.claim_id,
a.cover_no,
a.receive_date,
a.practice_no,
a.service_provider_no,
a.refering_provider_no,
b.claim_line_id,
b.dependent_code,
b.service_date_from,
b.service_date_to,
b.cheque_run_date,
b.process_date,
b.tariff_code_no,
b.tariff_amount,
b.claimed_amount,
c.amount_paid,
d.practice_name,
e.discipline,
e.discipline_description,
g.rule_no,
g.message_code,
g.long_msg_description,
h.benefit_code,
h.benefit_description,
t.option_name
FROM neo_claims a
LEFT JOIN neo_claim_line b
ON (a.claim_id = b.claim_id)
LEFT JOIN neo_claim_line_benefit c
ON (b.claim_line_id = c.claim_line_id)
LEFT JOIN neo_practice_details d
ON ( a.practice_no = d.practice_no)
LEFT JOIN neo_sub_disciplines e
ON ( d.sub_discipline = e.sub_discipline)
LEFT JOIN neo_claimline_firings g
ON (b.claim_line_id = g.claim_line_id)
LEFT JOIN neo_product_benefit h
ON (c.benefit_id = h.benefit_id)
(
SELECT i.*,
j.*
INTO temp_table
FROM neo_claims_pmb_details j,
neo_product_optin i)
LEFT JOIN temp_table t
ON ( j.benefit_id = t.benefit_id)
WHERE a.batch_id = 3496584;
DROP TABLE temp_table;

You could join the two table adding the related (left or inner) join .. in my sample the tables have alias tx and ty
and based on you comment for a create table
CREATE TABLE temp_table AS
SELECT a.batch_id,
a.claim_id,
a.cover_no,
a.receive_date,
a.practice_no,
a.service_provider_no,
a.refering_provider_no,
b.claim_line_id,
b.dependent_code,
b.service_date_from,
b.service_date_to,
b.cheque_run_date,
b.process_date,
b.tariff_code_no,
b.tariff_amount,
b.claimed_amount,
c.amount_paid,
d.practice_name,
e.discipline,
e.discipline_description,
g.rule_no,
g.message_code,
g.long_msg_description,
h.benefit_code,
h.benefit_description,
t.option_name,
ty.OPTION_NAME
FROM neo_claims a
LEFT JOIN neo_claim_line b ON (a.claim_id = b.claim_id)
LEFT JOIN neo_claim_line_benefit c ON (b.claim_line_id = c.claim_line_id)
LEFT JOIN neo_practice_details d ON ( a.practice_no = d.practice_no)
LEFT JOIN neo_sub_disciplines e ON ( d.sub_discipline = e.sub_discipline)
LEFT JOIN neo_claimline_firings g ON (b.claim_line_id = g.claim_line_id)
LEFT JOIN neo_product_benefit h ON (c.benefit_id = h.benefit_id)
LEFT JOIN neo_claims_pmb_details tx ON tx.BENEFIT_ID = h.benefit_id
LEFT JOIN neo_product_benefit ty ON tx.OPTION_ID = ty.OPTION_ID
LEFT JOIN temp_table t ON ( j.benefit_id = t.benefit_id)
WHERE a.batch_id = 3496584;

My guess is no temp table needed. Just include table expression into the original query
...
LEFT JOIN neo_product_benefit h
ON (c.benefit_id = h.benefit_id)
LEFT JOIN ( SELECT i.benefit_id, j.option_name -- correct cols as needed
FROM neo_claims_pmb_details j,
neo_product_optin i) t
ON (c.benefit_id = t.benefit_id)
WHERE a.batch_id = 3496584;

Related

SQL Subquery containing Joins

I'm using Hive hql. I am trying to inner join two tables filtering on issue_type='Impediments'
Now I have a new requirement to join dm_jira__label to include the label and issue_id columns. I have tried having a subquery adding the issue_id and label by using a left join with dm_jira__label on issue_id
INNER JOIN datamart_core.dm_jira__release
ON dm_jira.issue_id = dm_jira__release.issue_id;
(
SELECT b.issue_id, b.label AS jira_label
FROM datamart_core.dm_jira__label as B, datamart_core.dm_jira__release AS K
LEFT JOIN b
ON b.issue_id=k.issue_id
);
WHERE dm_jira.issue_type = 'Impediment') AS J
I am getting the following error:
AnalysisException: Illegal table reference to non-collection type: 'b' Path resolved to type: STRUCT<issue_id:DOUBLE,label:STRING>
See the full code below. thanks in advance.
SELECT DISTINCT
j.project_key AS jira_project_key,
j.issue_type,
j.issue_assignee AS impediment_owner,
j.issue_status AS impediment_status,
j.issue_priority AS impediment_priority,
j.issue_summary AS impediment_summary,
j.`release` AS jira_release,
j.sow AS sow_num,
j.issue_due_date_utc AS jira_issue_due_date_utc,
j.issue_id AS jira_issue_id,
s.sow_family
from (
--Subquery to combine dm_jira and dm_jira__release
SELECT dm_jira.project_key,
dm_jira.issue_type,
dm_jira.issue_assignee,
dm_jira.issue_status,
dm_jira.issue_priority,
dm_jira.issue_summary,
dm_jira.issue_due_date_utc,
dm_jira.issue_id,
dm_jira__release.`release`,
dm_jira__release.sow
from datamart_core.dm_jira
INNER JOIN datamart_core.dm_jira__release
ON dm_jira.issue_id = dm_jira__release.issue_id;
(
SELECT b.issue_id, b.label AS jira_label
FROM datamart_core.dm_jira__label as B, datamart_core.dm_jira__release AS K
LEFT JOIN b
ON b.issue_id=k.issue_id
);
WHERE dm_jira.issue_type = 'Impediment') AS J
INNER JOIN datamart_core.dm_asoe_jira_scrum_summary AS S
ON j.`release` = s.jira_release
AND j.sow = s.sow_num
AND j.project_key = s.jira_project_key;
; ends a whole statement, don't use it at the end of sub-queries or joins.
Using meaningless aliases such as B or K or J harms readability, don't do it.
FROM x, y is the same as FROM x CROSS JOIN y, it's not a list of tables you're going to join. This means that you have the following code...
(
SELECT
b.issue_id, b.label AS jira_label
FROM
datamart_core.dm_jira__label as B
CROSS JOIN
datamart_core.dm_jira__release AS K
LEFT JOIN
b
ON b.issue_id=k.issue_id
)
The b in the LEFT JOIN isn't a table, and causes your syntax error.
Then, your sub query just sits in the middle of the code, it's not joined on or used in any way. I think you intended a pattern more like this...
FROM
datamart_core.dm_jira
INNER JOIN
datamart_core.dm_jira__release
ON dm_jira.issue_id = dm_jira__release.issue_id;
LEFT JOIN
(
<your sub-query>
)
AS fubar
ON fubar.something = something.else
WHERE
dm_jira.issue_type = 'Impediment'
Even then, you don't actually need nested sub-queries at all. You can just keep adding joins, such as this...
SELECT
jira.project_key AS jira_project_key,
jira.issue_type,
jira.issue_assignee AS impediment_owner,
jira.issue_status AS impediment_status,
jira.issue_priority AS impediment_priority,
jira.issue_summary AS impediment_summary,
jrel.`release` AS jira_release,
jrel.sow AS sow_num,
jira.issue_due_date_utc AS jira_issue_due_date_utc,
jira.issue_id AS jira_issue_id,
jlab.label,
summ.sow_family
FROM
datamart_core.dm_jira AS jira
INNER JOIN
datamart_core.dm_jira__release AS jrel
ON jrel.issue_id = jira.issue_id
LEFT JOIN
datamart_core.dm_jira__label AS jlab
ON jlab.issue_id = jrel.issue_id
INNER JOIN
datamart_core.dm_asoe_jira_scrum_summary AS summ
ON summ.jira_release = jrel.`release`
AND summ.sow_num = jrel.sow
AND summ.jira_project_key = jira.project_key
WHERE
jira.issue_type = 'Impediment'
;

Multiple Join not working on two string attributes

I have the following issue:
I have several tables in my Database, in order to check for a specific criteria I have to join several tables, where I use the following statement:
SELECT *
FROM (SELECT * FROM (((((((((SELECT * FROM table1 WHERE tab1_id = 1) AS A
INNER JOIN (SELECT * FROM table2 WHERE tab2_variable IS NOT NULL) AS B
ON A.variable = B.variable)
INNER JOIN (SELECT table3.*, IIF(year='XXXX', 0, 1) AS flag FROM table3) AS C
ON A.h_name = C.h_name)
INNER JOIN (SELECT * FROM table4 WHERE s_flag = 0) AS D
ON C.v_type = D.v_type)
INNER JOIN table5
ON C.ng_type = table5.ng_type)
INNER JOIN table6
ON C.part = table6.part)
INNER JOIN table7
ON C.ifg = table7.ifg)
INNER JOIN table8
ON C.v_type = table8.v_type AND C.ng_typ = table8.ng_typ AND C.ntr_flag = table8.ntr_flag)
INNER JOIN table9
ON table8.ifg_sii_id = table9.ifg_sii_id)) AS F
LEFT JOIN table10
ON F.risk = table10.risk AND C.v_type = table10.v_type
AND F.series = table10.series
This statement fails. But when I remove one the following two join-conditions in the last Left JOIN, it works as intended:
F.risk = table10.risk and/or C.v_type = table10.v_type
They are both of type CHAR, whereas series is type TINYINT, I guess it has something to do with joining on multiple conditions with strings, but I'm not able to find a workaround, any ideas?
according to your current SQL, Table C is not visible as it's a sub query within F. instead of C.v_type you need to use F.c_v_type and the c_v_type field must come from C table like. select v_type as c_v_type from table3... as C
In the last part of your Query You could try to actually select the table and include the Where Clause Like so:
LEFT JOIN (Select * from table10 Where C.v_type = v_type AND F.series = series) as xx
ON F.risk = xx.risk
Hope this helps

Nested SQL - Distinct Load Left Join in one statement

I wanted to left join two queries:
First:
SELECT TIG_TOL.sName AS Maschine,
TIG_TOL.lTolRef,
Max(TIG_JOB.tActBegin) AS MaxvontActBegin
FROM TIG_JOB LEFT JOIN TIG_TOL ON TIG_JOB.lMacRef = TIG_TOL.lTolRef
WHERE (((TIG_JOB.sState)="Run" Or (TIG_JOB.sState)="Ready"))
GROUP BY TIG_TOL.sName, TIG_TOL.lTolRef;
Second:
SELECT TIG_JOB.sName AS Auftrag,
TIG_JOB.lJobRef,
TIG_TOL.sName AS Artikel,
TIG_TOL.sDescript AS Artikel_Bezeichnung
FROM (TIG_JOB LEFT JOIN TIG_TOL_BOK ON TIG_JOB.lJobRef = TIG_TOL_BOK.lJobRef)
LEFT JOIN TIG_TOL ON (TIG_TOL_BOK.lTolRef = TIG_TOL.lTolRef)
AND (TIG_TOL_BOK.lTolTypRef = TIG_TOL.lTolTypRef)
WHERE (((TIG_TOL.lTolTypRef)=10));
Over a left join
on First.MaxvontActBegin = Second.TIG_JOB.tActBegin
AND First.TIG_TOL.lTolRef = Second.TIG_JOB.lMacRef
Is that possible? In Access Im doing it over two queries, where the second is using the first..
I (blindly) added TIG_JOB.tActBegin and TIG_JOB.lMacRef to the 2nd SELECT (hoping they exist) in order to JOIN the two results.
I used SELECT * only because you did not specify the column selection.
SELECT *
FROM
(
SELECT TIG_TOL.sName AS Maschine,
TIG_TOL.lTolRef,
Max(TIG_JOB.tActBegin) AS MaxvontActBegin
FROM TIG_JOB LEFT JOIN TIG_TOL ON TIG_JOB.lMacRef = TIG_TOL.lTolRef
WHERE (((TIG_JOB.sState)="Run" Or (TIG_JOB.sState)="Ready"))
GROUP BY TIG_TOL.sName, TIG_TOL.lTolRef
) AS FirstTable
LEFT JOIN
(
SELECT TIG_JOB.sName AS Auftrag,
TIG_JOB.lJobRef,
TIG_TOL.sName AS Artikel,
TIG_TOL.sDescript AS Artikel_Bezeichnung,
TIG_JOB.tActBegin,
TIG_JOB.lMacRef
FROM (TIG_JOB LEFT JOIN TIG_TOL_BOK ON TIG_JOB.lJobRef = TIG_TOL_BOK.lJobRef)
LEFT JOIN TIG_TOL ON (TIG_TOL_BOK.lTolRef = TIG_TOL.lTolRef)
AND (TIG_TOL_BOK.lTolTypRef = TIG_TOL.lTolTypRef)
WHERE (((TIG_TOL.lTolTypRef)=10))
) AS SecondTable
ON FirstTable.MaxvontActBegin = SecondTable.tActBegin
AND FirstTable.lTolRef = SecondTable.lMacRef`

SQL with left outer join and 3 tables

I would like to add an ADD at the end of my code. Please have a look on my code and thanks for your support:
SELECT Area.org,
Supervisors.NomSup,
Supervisors.PrenomSup,
Employees.NomEmp,
Employees.PrenomEmp,
Employees.NoIdAlcanEmp,
Competencies.CodeCompetencies,
Competencies.CompetencyName,
LinkResultComp.AssNote,
LinkResultComp.AssDate
FROM ((((((
Area INNER JOIN Supervisors ON Area.IdArea = Supervisors.IdArea
)
INNER JOIN Employees ON Supervisors.IdSupervisor = Employees.IdSupervisor
)
INNER JOIN LinkProfilesEmployees ON Employees.IdEmp = LinkProfilesEmployees.IdEmp
)
INNER JOIN Profiles ON Profiles.IdProfiles = LinkProfilesEmployees.IdProfiles
)
INNER JOIN LinkProfComp ON Profiles.IdProfiles = LinkProfComp.IdProfiles
)
INNER JOIN Competencies ON Competencies.IdCompetencies = LinkProfComp.IdCompetencies
)
LEFT OUTER JOIN LinkResultComp ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies AND ON Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
WHERE Area.org LIKE "*20*" AND Competencies.CodeCompetencies LIKE "khse2010-05"
ORDER BY Supervisors.NomSup, Employees.NomEmp;
Just remove the extra ON that you added
So change this
LEFT OUTER JOIN LinkResultComp
ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies
AND ON Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
------^^ This one
to this
LEFT OUTER JOIN LinkResultComp
ON (Competencies.IdCompetencies = LinkResultComp.IdCompetencies
AND Competencies.IdCompetencies = LinkResultComp.IdCompetencies)
Of course I assume you meant different fields for the second condition

Sql Outer Join: Extracting values from multible tables

I am extracting data from multiple tables. mt query is as follows:
SELECT p.Record_Num as RecordNum
,p.GCD_ID as GCDID
,p.Project_Desc as ProjectDesc
,p.Proponent_Name as ProponentName
,st.Station_Name as StationName
,p.OpCentre as OpCentre
,s.Sector_Name as SectorName
,p.PLZone as PLZone
,f.Feeder_Desc as FeederDesc
,d.DxTx_Desc as DxTx
,op.Op_Control_Desc as OpControl
,t.Type_Desc as Type
,c.Conn_Desc as ConnectionKV
,ss.Status_Desc as Status
,p.MW as MW
,p.Subject as Subject
,p.Ip_Num as IpNum
,p.H1N_ID as H1NID
,p.NOMS_Slip_Num as NomsSlipNum
,p.NMS_Updated as NmsUpdated
,p.Received_Date as ReceivedDate
,p.Actual_IS_Date as ActualISDate
,p.Scheduled_IS_Date as ScheduledIsDate
,stst.Station_Name as UpStation
,ff.Feeder_Desc as UpFeeder
,p.HV_Circuit as HVCircuit
,p.SIA_Required as SIAReqd
FROM Project_Detail p,
Station st, Sector s, Feeder f, DxTx d, Operational_Control op, Type t,
Connection_Kv c, Status ss, Station stst, Feeder ff
WHERE
p.Station_ID = st.Station_ID and
p.Sector_ID = s.Sector_ID and
p.Feeder = f.Feeder_ID and
p.DxTx_ID = d.DxTx_ID and
p.OpControl_ID = op.Op_Control_ID and
p.Type_ID= t.Type_ID and
p.ConnKV_ID = c.Conn_ID and
p.Status_ID = ss.Status_ID and
p.UP_Station_ID = stst.Station_ID and
p.UP_Feeder_ID = ff.Feeder_ID
The problem with this query is if it doesnot find an associated value in the second table, it doesnot show the row.
for example : every project has feeders. so if a project_detail table has a feederid which doesnot have an association in the feeder table, then it wont show the row. also, there are times when the feeders are not assigned to a project.
i think i have to use outer joins to get the values. but i cannot figure out how to do that.
please help.
SELECT *
FROM Project_Detail p
LEFT JOIN
Station st
ON p.Station_ID = st.Station_ID
LEFT JOIN
Sector s
ON p.Sector_ID = s.Sector_ID
…
You need LEFT OR FULL OUTER JOINS instead of the inner joins you are now using with your where clause.
SELECT p.Record_Num as RecordNum
,p.GCD_ID as GCDID
,p.Project_Desc as ProjectDesc
,p.Proponent_Name as ProponentName
,st.Station_Name as StationName
,p.OpCentre as OpCentre
,s.Sector_Name as SectorName
,p.PLZone as PLZone
,f.Feeder_Desc as FeederDesc
,d.DxTx_Desc as DxTx
,op.Op_Control_Desc as OpControl
,t.Type_Desc as Type
,c.Conn_Desc as ConnectionKV
,ss.Status_Desc as Status
,p.MW as MW
,p.Subject as Subject
,p.Ip_Num as IpNum
,p.H1N_ID as H1NID
,p.NOMS_Slip_Num as NomsSlipNum
,p.NMS_Updated as NmsUpdated
,p.Received_Date as ReceivedDate
,p.Actual_IS_Date as ActualISDate
,p.Scheduled_IS_Date as ScheduledIsDate
,stst.Station_Name as UpStation
,ff.Feeder_Desc as UpFeeder
,p.HV_Circuit as HVCircuit
,p.SIA_Required as SIAReqd
FROM Project_Detail p
LEFT OUTER JOIN Station st ON p.Station_ID = st.Station_ID
LEFT OUTER JOIN Sector s ON p.Sector_ID = s.Sector_ID
LEFT OUTER JOIN Feeder f ON p.Feeder = f.Feeder_ID
LEFT OUTER JOIN DxTx d ON p.DxTx_ID = d.DxTx_ID
LEFT OUTER JOIN Operational_Control op ON p.OpControl_ID = op.Op_Control_ID
LEFT OUTER JOIN Type t ON p.Type_ID= t.Type_ID
LEFT OUTER JOIN Connection_Kv c ON p.ConnKV_ID = c.Conn_ID
LEFT OUTER JOIN Status ss ON p.Status_ID = ss.Status_ID
LEFT OUTER JOIN Station stst ON p.UP_Station_ID = stst.Station_ID
LEFT OUTER JOIN Feeder ff ON p.UP_Feeder_ID = ff.Feeder_ID