Is there a way to make multiple UNION's more efficient when they all do the same joins? - sql

I have 3 tables and I'd like to make a view that aggregates data vertically (using UNIONS) from the 3 tables. I already have a query that does what I want, but it uses a lot of repeated joins for each query, and I'm wondering if there is a way to only join up all these tables once and put the unions on top of that.
I want to join 3 tables that I will call desiredTable1, desiredTable2, and desiredTable3.
They all make use of joins on joinTable, joinTable2 and joinTable3 like so:
(select desiredTable1.id, desiredTable1.created_at, joinTable1.firstname, joinTable1.lastname, 'DESCRIPTOR_1' as "descriptor", desiredTable1.amount from desiredTable1
join joinTable3 on joinTable3.id = desiredTable1.joinTable3_id
join joinTable2 on joinTable2.id = joinTable3.joinTable2_id
join joinTable1 on joinTable1.id = joinTable2.joinTable1_id)
UNION
(select desiredTable2.id, desiredTable2.created_at, joinTable1.firstname, joinTable1.lastname, 'DESCRIPTOR_2' as "descriptor", desiredTable2.amount from desiredTable2
join joinTable3 on joinTable3.id = desiredTable2.joinTable3_id
join joinTable2 on joinTable2.id = joinTable3.joinTable2_id
join joinTable1 on joinTable1.id = joinTable2.joinTable1_id)
UNION
(select desiredTable3.id, desiredTable3.created_at, joinTable1.firstname, joinTable1.lastname, 'DESCRIPTOR_3' as "descriptor", desiredTable3.amount from desiredTable3
join joinTable3 on joinTable3.id = desiredTable3.joinTable3_id
join joinTable2 on joinTable2.id = joinTable3.joinTable2_id
join joinTable1 on joinTable1.id = joinTable2.joinTable1_id)
As you can see, I need the linked information from joinTable1 in each query, but if I can help it, I would prefer not to repeat all these joins. Is there a way I could "define" a subquery that does all those joins, then do all the unions on top of that?

You can use union all before joining:
select dt.id, dt.created_at, jt1.firstname, jt1.lastname, dt.descriptor, dt.amount
from ((select dt1.*, 'DESCRIPTOR_1' as descriptor
from desiredTable1 dt1
) union all
(select dt2.*, 'DESCRIPTOR_2' as descriptor
from desiredTable2 dt2
) union all
(select dt3.*, 'DESCRIPTOR_3' as descriptor
from desiredTable3 dt3
)
) dt join
joinTable3 jt3
on jt3.id = dt.joinTable3_id join
joinTable2 jt2
on jt2.id = jt3.joinTable2_id join
joinTable1 jt1
on jt1.id = jt2.joinTable1_id;
Note: This uses dt.* for the subqueries as a convenience. If the tables don't have the same columns, list only the ones needed for the outer query.

Related

Combine two queries, one based upon the other, into one

I have two queries, one based partly on the other. Is there a way of combining them into a single query?
SELECT tblIssues.*, tblIssues.NewsletterLookup
FROM tblIssues
WHERE (((tblIssues.NewsletterLookup)=5));
SELECT tblArea.ID, tblArea.AreaName
FROM tblArea LEFT JOIN Query2 ON tblArea.ID = Query2.[AreaLookup]
WHERE (((tblArea.Dormant)=False) AND ((Query2.tblIssues.NewsletterLookup) Is Null));
If you want to do this in a single query without Query2, you can use the equivalent SQL from Query2 as a subquery in your second example:
SELECT a.ID, a.AreaName
FROM
tblArea AS a
LEFT JOIN
(
SELECT i.*
FROM tblIssues AS i
WHERE i.NewsletterLookup=5
) AS sub
ON a.ID = sub.[AreaLookup]
WHERE
a.Dormant=False
AND sub.NewsletterLookup Is Null;
You ment to perform a JOIN like
SELECT ti.*, tblArea.ID, tblArea.AreaName
FROM tblArea ta
LEFT JOIN tblIssues ti ON ta.ID = ti.[AreaLookup]
WHERE (ti.NewsletterLookup=5 OR ti.NewsletterLookup Is Null)
AND ta.Dormant=False;

Ms Access - Missing operator (LEFT JOIN)

I have problem with LEFT JOIN query which is working in SQL, but when I try to query MDB database it doesn´t work.
Here it is:
Select PH.[ID], PH.[SText], PH.[Datum] ,PHpol.[SText], PHpol.[Mnozstvi], PHpol.[kcJedn], PHpol.[RelSzDPH], PHpol.[SDph], skst.[ID]
from PH LEFT JOIN PHpol ON PH.[ID] = PHpol.[RefAg]
LEFT JOIN Skz ON PHpol.[RefSKz] = Skz.[ID]
LEFT JOIN Skst ON Skz.[RefStruct] = Skst.[ID]
WHERE RelforUh = 2
AND RelCr=43
AND Datum BETWEEN #2015-01-01# AND #2015-09-01#
In MS Access, you need parentheses for multiple joins:
Select PH.[ID], PH.[SText], PH.[Datum] ,PHpol.[SText], PHpol.[Mnozstvi], PHpol.[kcJedn], PHpol.[RelSzDPH], PHpol.[SDph], skst.[ID]
from ((PH LEFT JOIN
PHpol
ON PH.[ID] = PHpol.[RefAg]
) LEFT JOIN
Skz
ON PHpol.[RefSKz] = Skz.[ID]
) LEFT JOIN
Skst
ON Skz.[RefStruct] = Skst.[ID]
WHERE RelforUh = 2 AND RelCr=43 AND
Datum BETWEEN #2015-01-01# AND #2015-09-01#
In MS-Access you need to add parentheses if you have to query on more than two tables. Structure goes like this..
Syntax for when you have two tables
Select <column list>
From Table1 Join Table2
on Table1.Col = Table2.col
where <your conditions>
Syntax for more than two tables.
Select <column list>
From (Table1 Join Table2
on Table1.Col = Table2.col)
Join Table3 on Table2.col = Table3.col
where <your conditions>
() make sure that whatever is written inside it, act as a table. If you have more tables then you can join them in the same fashion. You can read more about joining multiple tables in MS-Access here.
Well you can re-write your query as follows.
Select PH.[ID], PH.[SText], PH.[Datum] ,PHpol.[SText], PHpol.[Mnozstvi], PHpol.[kcJedn], PHpol.[RelSzDPH], PHpol.[SDph], skst.[ID]
from ((PH LEFT JOIN PHpol ON PH.[ID] = PHpol.[RefAg])
LEFT JOIN Skz ON PHpol.[RefSKz] = Skz.[ID])
LEFT JOIN Skst ON Skz.[RefStruct] = Skst.[ID]
WHERE RelforUh = 2
AND RelCr=43
AND Datum BETWEEN #2015-01-01# AND #2015-09-01#

Sql subquery for DB2

The sql query requires 4 tables to be joined, which i did, and i have to display few columns out of them that satisfy a condition. Say this is the query in Where clause. Now how do I write a subquery.. to display another column (ORG_NAME,that is there in ORG_UNIT) whose contents is based on the rows that is satified by the query in Where clause.
I wrote this code, but it is not working for me:
SELECT T33.CONTRACT_NUM, T135.MINOR_ORG_NUM, T96.ORG_TYPE,T22.CFD_FLAG,
(SELECT T96.ORG_NAME
FROM ORG_UNIT T96, SUB_UNIT T135
WHERE T96.ORG_NUMBER IN (T135.MAJOR_ORG_NUMBER)) AS HEAD_ORG_NAME
FROM
ORG_UNIT T96, SUB_UNIT T135, CUST_CONTRACT T33, CONT_ASSIGNMT T22
WHERE
T96.ORG_NUMBER = T22.ORG_NUMBER
AND T22.CTR_SYS_NUM = T33.CTR_SYS_NUM
AND T96.ORG_NUMBER = T135.MINOR_ORG_NUMBER
AND T135.RELTN_TYPE = 'HOS'
AND T22.CFD_FLAG = 'Y';
For the record, T135 contains head offices numbers (MAJOR_ORG_NUMBER) and their sub - offices numbers (MINOR_ORG_NUMBER)
In SQL, use JOIN to "merge" together tables based on their a common columns.
Here is a simple guide that would give you the base idea: SQL JOIN
In SQL, it's always best to draw what you want to do, so refer to this link to see a "LEFT JOIN" picture example: LEFT JOIN
Using a "LEFT JOIN" to merge your tables (where : ORG_UNIT.ORG_NUMBER = SUB_UNIT.MAJOR_ORG_NUMBER), will look like this:
LEFT JOIN SUB_UNIT T135 ON T96.ORG_NUMBER = T135.MAJOR_ORG_NUMBER
In a query, you put a JOIN right after the "FROM", and BEFORE the "WHERE":
SELECT
T33.CONTRACT_NUM,
T135.MINOR_ORG_NUM,
T96.ORG_TYPE,
T22.CFD_FLAG,
T135.ORG_NAME AS HEAD_ORG_NAME
FROM
ORG_UNIT T96,
CUST_CONTRACT T33,
CONT_ASSIGNMT T22
LEFT JOIN SUB_UNIT T135 ON T96.ORG_NUMBER = T135.MAJOR_ORG_NUMBER
WHERE
T96.ORG_NUMBER = T22.ORG_NUMBER
AND T22.CTR_SYS_NUM = T33.CTR_SYS_NUM
AND T96.ORG_NUMBER = T135.MINOR_ORG_NUMBER
AND T135.RELTN_TYPE = 'HOS'
AND T22.CFD_FLAG = 'Y';
Notice, that you could (and SHOULD) use JOIN for merging all the tables (and avoiding using an expensive WHERE condition):
SELECT
T33.CONTRACT_NUM,
T135.MINOR_ORG_NUM,
T96.ORG_TYPE,
T22.CFD_FLAG,
T135.ORG_NAME AS HEAD_ORG_NAME
FROM
ORG_UNIT T96
LEFT JOIN SUB_UNIT T135 ON
T96.ORG_NUMBER = T135.MAJOR_ORG_NUMBER
AND T96.ORG_NUMBER = T135.MINOR_ORG_NUMBER
LEFT JOIN ON
CONT_ASSIGNMT T22 ON T96.ORG_NUMBER = T22.ORG_NUMBER
LEFT JOIN ON
CUST_CONTRACT T33 ON T22.CTR_SYS_NUM = T33.CTR_SYS_NUM
WHERE
T135.RELTN_TYPE = 'HOS'
AND T22.CFD_FLAG = 'Y';
There are several JOIN types (LEFT/RIGHT/INNER/OUTER), so see your using the one you need.

Recursive query with outer joins?

I'm attempting the following query,
DECLARE #EntityType varchar(25)
SET #EntityType = 'Accessory';
WITH Entities (
E_ID, E_Type,
P_ID, P_Name, P_DataType, P_Required, P_OnlyOne,
PV_ID, PV_Value, PV_EntityID, PV_ValueEntityID,
PV_UnitValueID, PV_UnitID, PV_UnitName, PV_UnitDesc, PV_MeasureID, PV_MeasureName, PV_UnitValue,
PV_SelectionID, PV_DropDownID, PV_DropDownName, PV_DropDownOptionID, PV_DropDownOptionName, PV_DropDownOptionDesc,
RecursiveLevel
)
AS
(
-- Original Query
SELECT dbo.Entity.ID AS E_ID, dbo.EntityType.Name AS E_Type,
dbo.Property.ID AS P_ID, dbo.Property.Name AS P_Name, DataType.Name AS P_DataType, Required AS P_Required, OnlyOne AS P_OnlyOne,
dbo.PropertyValue.ID AS PV_ID, dbo.PropertyValue.Value AS PV_Value, dbo.PropertyValue.EntityID AS PV_EntityID, dbo.PropertyValue.ValueEntityID AS PV_ValueEntityID,
dbo.UnitValue.ID AS PV_UnitValueID, dbo.UnitOfMeasure.ID AS PV_UnitID, dbo.UnitOfMeasure.Name AS PV_UnitName, dbo.UnitOfMeasure.Description AS PV_UnitDesc, dbo.Measure.ID AS PV_MeasureID, dbo.Measure.Name AS PV_MeasureName, dbo.UnitValue.UnitValue AS PV_UnitValue,
dbo.DropDownSelection.ID AS PV_SelectionID, dbo.DropDown.ID AS PV_DropDownID, dbo.DropDown.Name AS PV_DropDownName, dbo.DropDownOption.ID AS PV_DropDownOptionID, dbo.DropDownOption.Name AS PV_DropDownOptionName, dbo.DropDownOption.Description AS PV_DropDownOptionDesc,
0 AS RecursiveLevel
FROM dbo.Entity
INNER JOIN dbo.EntityType ON dbo.EntityType.ID = dbo.Entity.TypeID
INNER JOIN dbo.Property ON dbo.Property.EntityTypeID = dbo.Entity.TypeID
INNER JOIN dbo.PropertyValue ON dbo.Property.ID = dbo.PropertyValue.PropertyID AND dbo.PropertyValue.EntityID = dbo.Entity.ID
INNER JOIN dbo.DataType ON dbo.DataType.ID = dbo.Property.DataTypeID
LEFT JOIN dbo.UnitValue ON dbo.UnitValue.ID = dbo.PropertyValue.UnitValueID
LEFT JOIN dbo.UnitOfMeasure ON dbo.UnitOfMeasure.ID = dbo.UnitValue.UnitOfMeasureID
LEFT JOIN dbo.Measure ON dbo.Measure.ID = dbo.UnitOfMeasure.MeasureID
LEFT JOIN dbo.DropDownSelection ON dbo.DropDownSelection.ID = dbo.PropertyValue.DropDownSelectedID
LEFT JOIN dbo.DropDownOption ON dbo.DropDownOption.ID = dbo.DropDownSelection.SelectedOptionID
LEFT JOIN dbo.DropDown ON dbo.DropDown.ID = dbo.DropDownSelection.DropDownID
WHERE dbo.EntityType.Name = #EntityType
UNION ALL
-- Recursive Query?
SELECT E2.E_ID AS E_ID, dbo.EntityType.Name AS E_Type,
dbo.Property.ID AS P_ID, dbo.Property.Name AS P_Name, DataType.Name AS P_DataType, Required AS P_Required, OnlyOne AS P_OnlyOne,
dbo.PropertyValue.ID AS PV_ID, dbo.PropertyValue.Value AS PV_Value, dbo.PropertyValue.EntityID AS PV_EntityID, dbo.PropertyValue.ValueEntityID AS PV_ValueEntityID,
dbo.UnitValue.ID AS PV_UnitValueID, dbo.UnitOfMeasure.ID AS PV_UnitID, dbo.UnitOfMeasure.Name AS PV_UnitName, dbo.UnitOfMeasure.Description AS PV_UnitDesc, dbo.Measure.ID AS PV_MeasureID, dbo.Measure.Name AS PV_MeasureName, dbo.UnitValue.UnitValue AS PV_UnitValue,
dbo.DropDownSelection.ID AS PV_SelectionID, dbo.DropDown.ID AS PV_DropDownID, dbo.DropDown.Name AS PV_DropDownName, dbo.DropDownOption.ID AS PV_DropDownOptionID, dbo.DropDownOption.Name AS PV_DropDownOptionName, dbo.DropDownOption.Description AS PV_DropDownOptionDesc,
(RecursiveLevel + 1)
FROM Entities AS E2
INNER JOIN dbo.Entity ON dbo.Entity.ID = E2.PV_ValueEntityID
INNER JOIN dbo.EntityType ON dbo.EntityType.ID = dbo.Entity.TypeID
INNER JOIN dbo.Property ON dbo.Property.EntityTypeID = dbo.Entity.TypeID
INNER JOIN dbo.PropertyValue ON dbo.Property.ID = dbo.PropertyValue.PropertyID AND dbo.PropertyValue.EntityID = E2.E_ID
INNER JOIN dbo.DataType ON dbo.DataType.ID = dbo.Property.DataTypeID
INNER JOIN dbo.UnitValue ON dbo.UnitValue.ID = dbo.PropertyValue.UnitValueID
INNER JOIN dbo.UnitOfMeasure ON dbo.UnitOfMeasure.ID = dbo.UnitValue.UnitOfMeasureID
INNER JOIN dbo.Measure ON dbo.Measure.ID = dbo.UnitOfMeasure.MeasureID
INNER JOIN dbo.DropDownSelection ON dbo.DropDownSelection.ID = dbo.PropertyValue.DropDownSelectedID
INNER JOIN dbo.DropDownOption ON dbo.DropDownOption.ID = dbo.DropDownSelection.SelectedOptionID
INNER JOIN dbo.DropDown ON dbo.DropDown.ID = dbo.DropDownSelection.DropDownID
)
SELECT E_ID, E_Type,
P_ID, P_Name, P_DataType, P_Required, P_OnlyOne,
PV_ID, PV_Value, PV_EntityID, PV_ValueEntityID,
PV_UnitValueID, PV_UnitID, PV_UnitName, PV_UnitDesc, PV_MeasureID, PV_MeasureName, PV_UnitValue,
PV_SelectionID, PV_DropDownID, PV_DropDownName, PV_DropDownOptionID, PV_DropDownOptionName, PV_DropDownOptionDesc,
RecursiveLevel
FROM Entities
INNER JOIN [dbo].[Entity] AS dE
ON dE.ID = PV_EntityID
The problem is the second query, the "recursive one" is getting the data I expect since I can't do the LEFT JOINs like in the first query. (At least to my understanding).
If I remove the fetching of the data that requires the LEFT (Outer) JOINs then the recursion works perfectly. My problem is I need both. Is there a way I can accomplish this?
Per http://msdn.microsoft.com/en-us/library/ms175972.aspx you can not have a left/right/outer join in a recursive CTE.
For a recursive CTE you can't use a subquery either so I sugest following this example.
They use two CTE's. The first is not recursive and does the left join to get the data it needs. The second CTE is recursive and inner joins on the first CTE. Since CTE1 is not recursive it can left join and supply default values for the missing rows and is guarenteed to work in the inner join.
However, you can also duplicate a left join with a union and subselect though it isn't really useful normally but it is interesting.
In that case, you would keep your first statement how it is. It will match all rows that join successfully.
Then UNION that query with another query that removes the join, but has a
NOT EXISTS(SELECT 1 FROM MISSING_ROWS_TABLE WHERE MAIN_TABLE.JOIN_CONDITION = MISSING_ROWS_TABLE.JOIN_CONDITION)
This gets all the rows that failed the previous join condition in query 1. You can replace the colmuns you would get from MISSING_ROWS_TABLE with NULL. I had to do this once using a coding framework that didn't support outer joins. Since recursive CTE's don't allow subqueries you have to use the first solution.

Joining two tables on a key and then left outer joining a table on a number of criteria

I'm attempting to join 3 tables together in a single query. The first two have a key so each entry has a matching entry. This joined table will then be joined by a third table that could produce multiple entries for each entry from the first table (the joined ones).
select * from
(select a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession
from trade_monthly a, trade_monthly_second b
where
a.bidentifier = b.jidentifier AND
a.bsession = b.JSession)
left outer join
trade c
on c.symbol = a.symbol
order by a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.symbol
There will be more criteria (not just c.symbol = a.symbol) on the left outer join but for now this should be useful. How can I nest the queries this way? I'm gettin gan SQL command not properly ended error.
Any help is appreciated.
Thanks
For what I know every derived table must be given a name; so try something like this:
SELECT * FROM
(SELECT a.bidentifier, ....
...
a.bsession = b.JSession) t
LEFT JOIN trade c
ON c.symbol = t.symbol
ORDER BY t.bidentifier, ...
Anyway I think you could use a simpler query:
SELECT a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.*
FROM trade_monthly a
INNER JOIN trade_monthly_second b
ON a.bidentifier = b.jidentifier
AND a.bsession = b.JSession
LEFT JOIN trade c
ON c.symbol = a.symbol
ORDER BY a.bidentifier, a.bsession, a.symbol, b.jidentifier, b.JSession, c.symbol
Try this:
SELECT
`trade_monthly`.`bidentifier` AS `bidentifier`,
`trade_monthly`.`bsession` AS `bsession`,
`trade_monthly`.`symbol` AS `symbol`,
`trade_monthly_second`.`jidentifier` AS `jidentifier`,
`trade_monthly_second`.`jsession` AS `jsession`
FROM
(
(
`trade_monthly`
JOIN `trade_monthly_second` ON(
(
(
`trade_monthly`.`bidentifier` = `trade_monthly_second`.`jidentifier`
)
AND(
`trade_monthly`.`bsession` = `trade_monthly_second`.`jsession`
)
)
)
)
JOIN `trade` ON(
(
`trade`.`symbol` = `trade_monthly`.`symbol`
)
)
)
ORDER BY
`trade_monthly`.`bidentifier`,
`trade_monthly`.`bsession`,
`trade_monthly`.`symbol`,
`trade_monthly_second`.`jidentifier`,
`trade_monthly_second`.`jsession`,
`trade`.`symbol`
Why don't you just create a view of the two inner joined tables. Then you can build a query that joins this view to the trade table using the left outer join matching criteria.
In my opinion, views are one of the most overlooked solutions to a lot of complex queries.