I have a query like this:
WITH CTE_KELOMPOKINFORMASI (KelompokInformasi, XBRLItem_ItemId)
AS (
SELECT a.Id AS KelompokInformasi, c.XBRLItem_ItemId
FROM XBRLNamespaces a INNER JOIN XBRLHypercubes b
ON a.XBRLView_ViewId = b.XBRLView_ViewId
INNER JOIN XBRLHypercubeDimensionItems c
ON b.XBRLHypercubeId = c.XBRLHypercube_XBRLHypercubeId
WHERE a.Id like '%KBIK_AAKL%')
SELECT f.KelompokInformasi, e.Name AS DimensionName,
ROW_NUMBER() OVER(PARTITION BY e.Name ORDER BY e.Name DESC) AS 'Number'
FROM XBRLDefinitionRoleDomainItems a
LEFT OUTER JOIN XBRLDefinitionRoleDimensionItems b
ON a.XBRLDefinitionRole_DefinitionRoleId = b.XBRLDefinitionRole_DefinitionRoleId
LEFT OUTER JOIN XBRLItems c ON a.XBRLItem_ItemId = c.ItemId
LEFT OUTER JOIN XBRLLabels d ON a.XBRLItem_ItemId = d.XBRLItem_ItemId
LEFT OUTER JOIN XBRLItems e ON b.XBRLItem_ItemId=e.ItemId
LEFT OUTER JOIN CTE_KELOMPOKINFORMASI f ON b.XBRLItem_ItemId=f.XBRLItem_ItemId
WHERE b.XBRLItem_ItemId=f.XBRLItem_ItemId
and it gives result like this:
Kelompok Informasi DimensionName Number
KBIK_AAKL JWAAKT 1
KBIK_AAKL JWAAKT 2
KBIK_AAKL JWAAKT 3
KBIK_AAKL SNOUPL 1
KBIK_AAKL SNOUPL 2
KBIK_AAKL KOKOLT 1
KBIK_AAKL KOKOLT 2
KBIK_AAKL KOKOLT 3
KBIK_AAKL KOKOLT 4
Is it possible if i DISTINCT my result again by "DimensionName", so i have a result just like this:
KelompokInformasi DimensionName Number
KBIK_AAKL JWAAKT 1
KBIK_AAKL SNOUPL 2
KBIK_AAKL KOKOLT 3
i tried to do distinct like code below but it doesn't work
SELECT DISTINCT DimensionName
FROM (SELECT f.KelompokInformasi, e.Name AS DimensionName,
ROW_NUMBER() OVER(PARTITION BY e.Name ORDER BY e.Name DESC) AS 'Number'
FROM XBRLDefinitionRoleDomainItems a
LEFT OUTER JOIN XBRLDefinitionRoleDimensionItems b
ON a.XBRLDefinitionRole_DefinitionRoleId = b.XBRLDefinitionRole_DefinitionRoleId
LEFT OUTER JOIN XBRLItems c ON a.XBRLItem_ItemId = c.ItemId
LEFT OUTER JOIN XBRLLabels d ON a.XBRLItem_ItemId = d.XBRLItem_ItemId
LEFT OUTER JOIN XBRLItems e ON b.XBRLItem_ItemId=e.ItemId
LEFT OUTER JOIN CTE_KELOMPOKINFORMASI f ON b.XBRLItem_ItemId=f.XBRLItem_ItemId
WHERE b.XBRLItem_ItemId=f.XBRLItem_ItemId)
Use your distinct on the inner subquery and row number them on the outer Select
WITH CTE_KELOMPOKINFORMASI (KelompokInformasi, XBRLItem_ItemId)
AS (
SELECT a.Id AS KelompokInformasi, c.XBRLItem_ItemId
FROM XBRLNamespaces a INNER JOIN XBRLHypercubes b
ON a.XBRLView_ViewId = b.XBRLView_ViewId
INNER JOIN XBRLHypercubeDimensionItems c
ON b.XBRLHypercubeId = c.XBRLHypercube_XBRLHypercubeId
WHERE a.Id like '%KBIK_AAKL%')
Select A.*, ROW_NUMBER() OVER(ORDER BY A.DimensionName DESC) AS 'Number'
FROM (
SELECT distinct f.KelompokInformasi, e.Name AS DimensionName
FROM XBRLDefinitionRoleDomainItems a
LEFT OUTER JOIN XBRLDefinitionRoleDimensionItems b
ON a.XBRLDefinitionRole_DefinitionRoleId = b.XBRLDefinitionRole_DefinitionRoleId
LEFT OUTER JOIN XBRLItems c ON a.XBRLItem_ItemId = c.ItemId
LEFT OUTER JOIN XBRLLabels d ON a.XBRLItem_ItemId = d.XBRLItem_ItemId
LEFT OUTER JOIN XBRLItems e ON b.XBRLItem_ItemId=e.ItemId
LEFT OUTER JOIN CTE_KELOMPOKINFORMASI f ON b.XBRLItem_ItemId=f.XBRLItem_ItemId
WHERE b.XBRLItem_ItemId=f.XBRLItem_ItemId
) AS A
Just Filter with Your Row_Number
Try like this
SELCT KelompokInformasi,DimensionName,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS 'NUMBER' FROM
(
SELECT f.KelompokInformasi, e.Name AS DimensionName,
ROW_NUMBER() OVER(PARTITION BY e.Name ORDER BY e.Name DESC) AS 'RN'
FROM XBRLDefinitionRoleDomainItems a
LEFT OUTER JOIN XBRLDefinitionRoleDimensionItems b
ON a.XBRLDefinitionRole_DefinitionRoleId = b.XBRLDefinitionRole_DefinitionRoleId
LEFT OUTER JOIN XBRLItems c ON a.XBRLItem_ItemId = c.ItemId
LEFT OUTER JOIN XBRLLabels d ON a.XBRLItem_ItemId = d.XBRLItem_ItemId
LEFT OUTER JOIN XBRLItems e ON b.XBRLItem_ItemId=e.ItemId
LEFT OUTER JOIN CTE_KELOMPOKINFORMASI f ON b.XBRLItem_ItemId=f.XBRLItem_ItemId
WHERE b.XBRLItem_ItemId=f.XBRLItem_ItemId
) AS T
WHERE RN = 1
(OR)
SELCT KelompokInformasi,DimensionName,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS 'NUMBER' FROM
(
SELECT f.KelompokInformasi, e.Name AS DimensionName,
FROM XBRLDefinitionRoleDomainItems a
LEFT OUTER JOIN XBRLDefinitionRoleDimensionItems b
ON a.XBRLDefinitionRole_DefinitionRoleId = b.XBRLDefinitionRole_DefinitionRoleId
LEFT OUTER JOIN XBRLItems c ON a.XBRLItem_ItemId = c.ItemId
LEFT OUTER JOIN XBRLLabels d ON a.XBRLItem_ItemId = d.XBRLItem_ItemId
LEFT OUTER JOIN XBRLItems e ON b.XBRLItem_ItemId=e.ItemId
LEFT OUTER JOIN CTE_KELOMPOKINFORMASI f ON b.XBRLItem_ItemId=f.XBRLItem_ItemId
WHERE b.XBRLItem_ItemId=f.XBRLItem_ItemId
GROUP BY f.KelompokInformasi, e.Name
) AS T
Related
I have a View that has a column named 'Reference' and a column named 'RateeId' and collects and computes data from another table and used SUM function to get its TotalScores. However, when I input the same RateeId but different Reference, the SUM calculates both and displays the same Scores.
This is what I have tried so far.
This is my script for my view:
select e.Id, b.Name, f.SiteName, e.Reference, e.Amount, e.DateTransaction, SUM(a.Score * (c.Weight / 100) * (d.Weight / 100)) as TotalScore, a.Status, a.CreatedByUserId
from prs_rate as a
left outer join prs_ratee as b on a.RateeId = b.Id
left outer join prs_kpi as c on a.KpiId = c.Id
left outer join prs_group as d on c.GroupId = d.Id
left outer join prs_totalratee as e on a.RateeId = e.RateeId
left outer join core_vSiteInfo as f on e.SiteCatered = f.siteCode
group by b.Name, a.Status, a.CreatedByUserId, e.Reference, e.Amount, e.DateTransaction, f.SiteName, e.Id
This is what it returned:
This is the View prs_vMainKPI. The result of the above script.
Please add order by clause and try again...
select e.Id, b.Name, f.SiteName, e.Reference, e.Amount, e.DateTransaction, SUM(a.Score * (c.Weight / 100) * (d.Weight / 100)) as TotalScore, a.Status, a.CreatedByUserId
from prs_rate as a
left outer join prs_ratee as b on a.RateeId = b.Id
left outer join prs_kpi as c on a.KpiId = c.Id
left outer join prs_group as d on c.GroupId = d.Id
left outer join prs_totalratee as e on a.RateeId = e.RateeId
left outer join core_vSiteInfo as f on e.SiteCatered = f.siteCode
group by e.Id, e.Reference, b.Name, a.Status, a.CreatedByUserId, e.Amount, e.DateTransaction, f.SiteName
order by e.Id, e.Reference
I want to select max values related to query, but all results are coming. Any idea ?
QUERY
SELECT MAXRecID,MAXSetID,MAXRID,PreReifiedValue,ComplianceState
FROM v_CI_CurrentComplianceStatus as A
INNER JOIN v_CIRules as B
ON B.CI_ID = A.CI_ID
INNER JOIN v_R_System as C
ON C.ResourceID = A.ItemKey
INNER JOIN
( SELECT PreReifiedValue,setting_CI_ID,
MAX(RecordID) AS MAXRecID,
MAX(SettingID) AS MAXSetID,
MAX(RuleID) AS MAXRID
FROM CI_CurrentRuleDetail
GROUP BY PreReifiedValue,Setting_CI_ID,instancedata
) AS D
ON D.Setting_CI_ID = A.CI_ID
GROUP by MAXRecID,MAXSetID,MAXRID,PreReifiedValue,rulename,ComplianceState
Results
MAXRecID MAXSetID MAXRID PreReifiedValue ComplianceState
72057594038117564 16780566 16780622 10 2
72057594038117565 16780570 16780620 0 2
Try this query
SELECT
MAX(tmp.MAXRecID), MAX(tmp.MAXSetID), MAX(tmp.MAXRID), MAX(tmp.PreReifiedValue), MAX(tmp.ComplianceState)
FROM (
SELECT MAXRecID,MAXSetID,MAXRID,PreReifiedValue,ComplianceState
FROM v_CI_CurrentComplianceStatus as A
INNER JOIN v_CIRules as B
ON B.CI_ID = A.CI_ID
INNER JOIN v_R_System as C
ON C.ResourceID = A.ItemKey
INNER JOIN
( SELECT PreReifiedValue,setting_CI_ID,
MAX(RecordID) AS MAXRecID,
MAX(SettingID) AS MAXSetID,
MAX(RuleID) AS MAXRID
FROM CI_CurrentRuleDetail
GROUP BY PreReifiedValue,Setting_CI_ID,instancedata
) AS D
ON D.Setting_CI_ID = A.CI_ID
) AS tmp
GROUP by tmp.MAXRecID, tmp.MAXSetID, tmp.MAXRID, tmp.PreReifiedValue, tmp.rulename, tmp.ComplianceState
I have View called VW_STOCKOPNAME
CREATE OR REPLACE VIEW VW_STOCKOPNAME AS SELECT
A.M_STOCKCODE_KL_ID,
A.M_STOCKCODE_JB_ID,
A.M_STOCKCODE_NB_ID,
A.M_STOCKCODE_SB_ID,
B."NAME" AS S1_KL,
C."NAME" AS S2_JB,
D."NAME" AS S3_NB,
E."NAME" AS S4_SB,
A.M_ASSETCODE_GOLONGAN_ID,
A.M_ASSETCODE_BIDANG_ID,
A.M_ASSETCODE_KELOMPOK_ID,
A.M_ASSETCODE_SUBKELOMPOK_ID,
A.M_ASSETCODE_SUBSUBKEL_ID,
F."NAME" AS A1_GOLONGAN,
G."NAME" AS A2_BIDANG,
H."NAME" AS A3_KELOMPOK,
I."NAME" AS A4_SUBKELOMPOK,
J."NAME" AS A5_SUBSUBKEL,
A.M_PRODUCT_ID,
K."NAME" AS PRODUCT,
L."NAME" AS UOM,
A.QTYBOOK,
A.QTYCOUNT,
A.DIFFERENCE,
N.M_WAREHOUSE_ID,
O."NAME" AS WAREHOUSE
FROM M_INVENTORYLINE A
LEFT JOIN M_STOCKCODE_KL B ON B.M_STOCKCODE_KL_ID = A.M_STOCKCODE_KL_ID
LEFT JOIN M_STOCKCODE_JB C ON C.M_STOCKCODE_JB_ID = A.M_STOCKCODE_JB_ID
LEFT JOIN M_STOCKCODE_NB D ON D.M_STOCKCODE_NB_ID = A.M_STOCKCODE_NB_ID
LEFT JOIN M_STOCKCODE_SB E ON E.M_STOCKCODE_SB_ID = A.M_STOCKCODE_SB_ID
LEFT JOIN M_ASSETCODE_GOLONGAN F ON F.M_ASSETCODE_GOLONGAN_ID = A.M_ASSETCODE_GOLONGAN_ID
LEFT JOIN M_ASSETCODE_BIDANG G ON G.M_ASSETCODE_BIDANG_ID = A.M_ASSETCODE_BIDANG_ID
LEFT JOIN M_ASSETCODE_KELOMPOK H ON H.M_ASSETCODE_KELOMPOK_ID = A.M_ASSETCODE_KELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBKELOMPOK I ON I.M_ASSETCODE_SUBKELOMPOK_ID = A.M_ASSETCODE_SUBKELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBSUBKEL J ON J.M_ASSETCODE_SUBSUBKEL_ID = A.M_ASSETCODE_SUBSUBKEL_ID
LEFT JOIN M_PRODUCT K ON K.M_PRODUCT_ID = A.M_PRODUCT_ID
LEFT JOIN C_UOM L ON L.C_UOM_ID = A.C_UOM_ID
LEFT JOIN M_INVENTORY N ON N.M_INVENTORY_ID = A.M_INVENTORY_ID
LEFT JOIN M_WAREHOUSE O ON O.M_WAREHOUSE_ID = N.M_WAREHOUSE_ID
;
I want to add another column which has command as follows :
SELECT S1_KL ||'-'|| S2_JB ||'-'|| S3_NB ||'-'|| S4_SB AS ASSET_CODE
FROM VW_STOCKOPNAME
It shows group of codes from the columns in the view to make it easier to see.
How can I add it?
Try this
CREATE OR REPLACE VIEW VW_STOCKOPNAME AS SELECT
A.M_STOCKCODE_KL_ID,
A.M_STOCKCODE_JB_ID,
A.M_STOCKCODE_NB_ID,
A.M_STOCKCODE_SB_ID,
B."NAME" AS S1_KL,
C."NAME" AS S2_JB,
D."NAME" AS S3_NB,
E."NAME" AS S4_SB,
A.M_ASSETCODE_GOLONGAN_ID,
A.M_ASSETCODE_BIDANG_ID,
A.M_ASSETCODE_KELOMPOK_ID,
A.M_ASSETCODE_SUBKELOMPOK_ID,
A.M_ASSETCODE_SUBSUBKEL_ID,
F."NAME" AS A1_GOLONGAN,
G."NAME" AS A2_BIDANG,
H."NAME" AS A3_KELOMPOK,
I."NAME" AS A4_SUBKELOMPOK,
J."NAME" AS A5_SUBSUBKEL,
A.M_PRODUCT_ID,
K."NAME" AS PRODUCT,
L."NAME" AS UOM,
A.QTYBOOK,
A.QTYCOUNT,
A.DIFFERENCE,
N.M_WAREHOUSE_ID,
O."NAME" AS WAREHOUSE,
B."NAME" ||'-'|| C."NAME" ||'-'|| D."NAME" ||'-'|| E."NAME" AS ASSET_CODE
FROM M_INVENTORYLINE A
LEFT JOIN M_STOCKCODE_KL B ON B.M_STOCKCODE_KL_ID = A.M_STOCKCODE_KL_ID
LEFT JOIN M_STOCKCODE_JB C ON C.M_STOCKCODE_JB_ID = A.M_STOCKCODE_JB_ID
LEFT JOIN M_STOCKCODE_NB D ON D.M_STOCKCODE_NB_ID = A.M_STOCKCODE_NB_ID
LEFT JOIN M_STOCKCODE_SB E ON E.M_STOCKCODE_SB_ID = A.M_STOCKCODE_SB_ID
LEFT JOIN M_ASSETCODE_GOLONGAN F ON F.M_ASSETCODE_GOLONGAN_ID = A.M_ASSETCODE_GOLONGAN_ID
LEFT JOIN M_ASSETCODE_BIDANG G ON G.M_ASSETCODE_BIDANG_ID = A.M_ASSETCODE_BIDANG_ID
LEFT JOIN M_ASSETCODE_KELOMPOK H ON H.M_ASSETCODE_KELOMPOK_ID = A.M_ASSETCODE_KELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBKELOMPOK I ON I.M_ASSETCODE_SUBKELOMPOK_ID = A.M_ASSETCODE_SUBKELOMPOK_ID
LEFT JOIN M_ASSETCODE_SUBSUBKEL J ON J.M_ASSETCODE_SUBSUBKEL_ID = A.M_ASSETCODE_SUBSUBKEL_ID
LEFT JOIN M_PRODUCT K ON K.M_PRODUCT_ID = A.M_PRODUCT_ID
LEFT JOIN C_UOM L ON L.C_UOM_ID = A.C_UOM_ID
LEFT JOIN M_INVENTORY N ON N.M_INVENTORY_ID = A.M_INVENTORY_ID
LEFT JOIN M_WAREHOUSE O ON O.M_WAREHOUSE_ID = N.M_WAREHOUSE_ID
;
I am struggling with a sql query. I want to include the sum from an other table.
SELECT DISTINCT
tblProject.CompanyID,
tblCompany.Name,
tblCompany.AvtalsKund,
tblProject.ProjectName,
tblProject.Estimate,
tblProject.ProjectStart,
tblProject.Deadline,
CONVERT(VARCHAR(8), tblProject.Deadline, 2) AS [YY.MM.DD] ,
tblProject.PreOffered,
tblProject.ProjectType,
tblProjectType.ProjType,
tblOrdered.FirstName + + tblOrdered.LastName as OrderedFullName,
tblProject.ProjectID,
tblProject.RegDate,
tblProject.ProjectNr,
tblProject.ProjectNr
FROM tblProject
INNER JOIN tblCompany ON tblProject.CompanyID = tblCompany.CompanyID
---> INNER JOIN (SELECT tblTimeRecord.ProjectID, SUM(CONVERT(float,replace([Hours],',','') ))
FROM tblTimeRecord group by tblTimeRecord.ProjectID) as b
ON b.ProjectID = tblProject.ProjectID
INNER JOIN tblTimeRecord ON tblTimeRecord.ProjectID = tblProject.ProjectID
INNER JOIN tblProjectType ON tblProject.ProjectType = tblProjectType.ProjTypeID
LEFT OUTER JOIN tblOrdered ON tblProject.OrderedBy = tblOrdered.OrderedID
LEFT OUTER JOIN tblRel_WorkerProject ON tblProject.ProjectID = tblRel_WorkerProject.ProjectID
LEFT OUTER JOIN tblPerson ON tblPerson.PersonID = tblRel_WorkerProject.WorkerID
LEFT OUTER JOIN tblRel_StatusWorkerProject ON tblProject.ProjectID = tblRel_StatusWorkerProject.ProjectID
I want to include this sum-block from table tblTimeRecord.
I get a sum of timerapports with this code
SELECT tblTimeRecord.ProjectID,
SUM(CONVERT(float,replace([Hours],',','') ))
FROM tblTimeRecord where ProjectID=1312 group by tblTimeRecord.ProjectID
Guess i do it in join?
Got it working with this.
SELECT DISTINCT
tblProject.ProjectID,
Summa,
tblProject.CompanyID,
tblCompany.Name,
tblCompany.AvtalsKund,
tblProject.ProjectName,
tblProject.Estimate,
tblProject.ProjectStart,
tblProject.Deadline,
CONVERT(VARCHAR(8), tblProject.Deadline, 2) AS [YY.MM.DD] ,
tblProject.PreOffered,
tblProject.ProjectType,
tblProjectType.ProjType,
tblOrdered.FirstName + + tblOrdered.LastName as OrderedFullName,
tblProject.ProjectID,
tblProject.RegDate,
tblProject.ProjectNr,
tblProject.ProjectNr
FROM tblProject
INNER JOIN tblCompany ON tblProject.CompanyID = tblCompany.CompanyID
INNER JOIN (SELECT tblTimeRecord.ProjectID, SUM(CONVERT(float,replace([Hours],',','') )) as Summa FROM tblTimeRecord group by tblTimeRecord.ProjectID) as b
ON b.ProjectID = tblProject.ProjectID
INNER JOIN tblTimeRecord ON tblTimeRecord.ProjectID = tblProject.ProjectID
INNER JOIN tblProjectType ON tblProject.ProjectType = tblProjectType.ProjTypeID
LEFT OUTER JOIN tblOrdered ON tblProject.OrderedBy = tblOrdered.OrderedID
LEFT OUTER JOIN tblRel_WorkerProject ON tblProject.ProjectID = tblRel_WorkerProject.ProjectID
LEFT OUTER JOIN tblPerson ON tblPerson.PersonID = tblRel_WorkerProject.WorkerID
LEFT OUTER JOIN tblRel_StatusWorkerProject ON tblProject.ProjectID = tblRel_StatusWorkerProject.ProjectID
There are two ways to do this.
You can use a WITH clause to create the aggregate table then join this to the main query.
Or do it this way:
SELECT m.BLAH
,m.FOO
,x.AMOUNT
FROM MAINTABLE m
LEFT JOIN
(
SELECT FOO
,SUM(AMOUNT) as AMOUNT
FROM OTHERTABLE
GROUP BY FOO
) x
ON m.FOO = x.FOO
I prefer the second way.
Here is my query. I removed the table names along with other things I am selecting just to make it easier to read.
Select
b.Key1,
b.Key2
From
b
left join c
on c.MailingKey = b.MailingKey
left join d
on d.OrderLineKey = c.OrderLineKey
left join e
on e.ShipKey = d.ShipKey
left join g
on d.ShipKey5 = g.ShipKey5
left join a
on a.DocKey = b.OrderKey
left join f
on f.pMethKey = b.MethKey
I need to use Key1 and Key2 that I select in another query to get more information about that key (location, ID, etc.) from another table. I am thinking I will need to make 2 loops within this original query and join them all up. So something like this:
Select b.Key1, b.Key2, h.*, i.*
From b left join
c on c.MailingKey = b.MailingKey left join
d on d.OrderLineKey = c.OrderLineKey left join
e on e.ShipKey = d.ShipKey left join
g on d.ShipKey5 = g.ShipKey5 left join
a on a.DocKey = b.OrderKey left join
f on f.pMethKey = b.MethKey left join
(
Select*
From table l
Where ID = b.Key1
) h left join (
Select*
From table l
Where ID = b.Key2
) i
I am just not sure how to write equal to b.Key1 and b.Key2.
I am using Microsoft Sql Server Management.
Common table expression:
with cte as (
Select b.Key1, b.Key2
From b left join
c on c.MailingKey = b.MailingKey left join
d on d.OrderLineKey = c.OrderLineKey left join
e on e.ShipKey = d.ShipKey left join
g on d.ShipKey5 = g.ShipKey5 left join
a on a.DocKey = b.OrderKey left join
f on f.pMethKey = b.MethKey)
Select * from CTE
INNER JOIN othertable o
on b.key1=o.key1
and b.key2=o.key2
or do you mean...
Inline view:
Select *
from otherTable
INNER JOIN (
Select b.Key1, b.Key2
From b left join
c on c.MailingKey = b.MailingKey left join
d on d.OrderLineKey = c.OrderLineKey left join
e on e.ShipKey = d.ShipKey left join
g on d.ShipKey5 = g.ShipKey5 left join
a on a.DocKey = b.OrderKey left join
f on f.pMethKey = b.MethKey) CTE
on b.key1=o.key1
and b.key2=o.key2
Or do you mean.... something else?
Perhaps:... using an OR statement...
Select *
from otherTable o
INNER JOIN (
Select b.Key1, b.Key2
From b left join
c on c.MailingKey = b.MailingKey left join
d on d.OrderLineKey = c.OrderLineKey left join
e on e.ShipKey = d.ShipKey left join
g on d.ShipKey5 = g.ShipKey5 left join
a on a.DocKey = b.OrderKey left join
f on f.pMethKey = b.MethKey) CTE
on o.key1=cte.key1
OR o.key2=cte.key2
or maybe... an in statement (within this set)
Select *
from otherTable o
INNER JOIN (
Select b.Key1, b.Key2
From b left join
c on c.MailingKey = b.MailingKey left join
d on d.OrderLineKey = c.OrderLineKey left join
e on e.ShipKey = d.ShipKey left join
g on d.ShipKey5 = g.ShipKey5 left join
a on a.DocKey = b.OrderKey left join
f on f.pMethKey = b.MethKey) CTE
on o.key1 IN (cte.key1, cte.key2)