I need to perform a JOIN on a JOIN-ed table, and I'm not sure how to accomplish it. Hopefully, the query below demonstrates what I'm trying to do: get country names, each country's leader, and each leader's home town.
I think the problem with this query is using the JOIN-ed table "President" in the second join with President.HomeTown_Id. I don't know what else to try.
SELECT
Countries.Name AS Country,
President.Name AS Leader,
PresidentHomeTown.Name AS LeaderHomeTown
FROM Countries
LEFT OUTER JOIN PoliticalFigures AS President ON Countries.President_Id = President.Id
LEFT OUTER JOIN Cities AS PresidentHomeTown ON President.HomeTown_Id = PresidentHomeTown.Id
In VS, I'm getting the error, "The multi-part identifier "President.Id" could not be bound."
The names of tables and fields are fictitious, but I need to solve an identical problem. I changed the names to make things clearer; hopefully this will be relevant to more people.
-- update --
Maybe the original code helps:
SELECT
CaseComparisons.Directory AS CaseComparisonDir,
BaselineResult.Directory AS BaselineResultDir,
ComparisonResult.Directory AS ComparisonResultDir,
Setup.FullSvnLink AS SvnLink,
BaselineVersion.FullFilePath AS BaselineExecutableDir
FROM CaseComparisons
LEFT OUTER JOIN Results AS BaselineResult ON CaseComparisons.BaselineResult_Id = Baseline.Id
LEFT OUTER JOIN Results AS ComparisonResult ON CaseComparisons.ComparisonResult_Id = Comparison.Id
LEFT OUTER JOIN Setups AS Setup ON Baseline.Setup_Id = Setups.Id
LEFT OUTER JOIN BuildVersions AS BaselineVersion ON BaselineResult.Version_Id = BuildVersions.Id
WHERE
CaseComparisons.Status = 'Queued' OR
Baseline.Status = 'Queued' OR
Comparison.Status = 'Queued'
The errors I get when I run the query:
The multi-part identifier "Baseline.Id" could not be bound.
The multi-part identifier "Comparison.Id" could not be bound.
The multi-part identifier "Baseline.Setup_Id" could not be bound.
The multi-part identifier "Setups.Id" could not be bound.
The multi-part identifier "BuildVersions.Id" could not be bound.
The multi-part identifier "Baseline.Status" could not be bound.
You've got mismatches between the aliases you're specifying (e.g. in Results AS ComparisonResult) and the aliases you're trying to use (e.g. in Comparison.Id). So, change this:
LEFT OUTER JOIN Results AS ComparisonResult
ON CaseComparisons.ComparisonResult_Id = Comparison.Id
to either this:
LEFT OUTER JOIN Results AS ComparisonResult
ON CaseComparisons.ComparisonResult_Id = ComparisonResult.Id
or this:
LEFT OUTER JOIN Results AS Comparison
ON CaseComparisons.ComparisonResult_Id = Comparison.Id
(and similarly for all the other joins).
You don't have an Id column in President table. Double-check that.
Try this, you have your names different:
SELECT
CaseComparisons.Directory AS CaseComparisonDir,
BaselineResult.Directory AS BaselineResultDir,
ComparisonResult.Directory AS ComparisonResultDir,
Setup.FullSvnLink AS SvnLink,
BaselineVersion.FullFilePath AS BaselineExecutableDir
FROM CaseComparisons
LEFT OUTER JOIN Results AS BaselineResult ON CaseComparisons.BaselineResult_Id = BaselineResult.Id
LEFT OUTER JOIN Results AS ComparisonResult ON CaseComparisons.ComparisonResult_Id = ComparisonResult.Id
LEFT OUTER JOIN Setups AS Setup ON BaselineResult.Setup_Id = Setup.Id
LEFT OUTER JOIN BuildVersions AS BaselineVersion ON BaselineResult.Version_Id = BaselineVersion.Id
WHERE
CaseComparisons.Status = 'Queued' OR
BaselineResult.Status = 'Queued' OR
ComparisonResult.Status = 'Queued'
Related
I'm getting the error message:
Subquery returned more than 1 value. This is not permitted when the subquery follows =,!=,<,<=,>,=> or when the subquery is used as an expression.
Below is a small subset of a larger query, but the part of the query to determine the Test_Col value is essentially where I'm running into this issue. The query by itself works, but when I use it as a subquery within the larger query, I get this error message in SQL Server. Any ideas on where I'm going wrong?
select
distinct(nml.scode) Loan_Num,
(select isnull(sum(isnull(t.smtd, 0) + isnull(t.sbeginbudget, 0)), 0)
from nmloan nml
left join property p on nml.hprop = p.hmy
left join total t on p.hmy = t.hppty
where nml.hprop in (2380, 3348)
and t.umonth between '1/1/1900' and '9/30/2021'
and t.ibook = 1 and t.hacct in (1349, 1348, 1347, 1345, 1343, 1342, 1341, 1339, 1338, 1337, 1336, 1334, 1332, 1690, 1682, 1331)
group by nml.hprop) Test_Col
from
nmloan nml
left join
property p on nml.hprop = p.hmy
left join
total t on p.hmy = t.hppty
left join
acct ac on ac.hmy = t.hacct
left join
nmborrower nmb on nml.hmy = nmb.hnmloan
left join
person ps on nmb.hmyperson = ps.hmy
left join
nmloanterms nmt on nml.hmy = nmt.hloan
left join
nmcollateralxref nmx on nml.hmy = nmx.hnmloan
left join
nmcollateral nmc on nmx.hnmcollateral = nmc.hmy
left join
loanbut1 lb1 on nml.hmy = lb1.hcode
left join
NMLedger l ON nml.hmy = l.hNMLoan
left join
nmLedgerDetail d on l.hmy = d.hNMLedger
left join
loanbut7 lb on nml.hmy = lb.hcode
left join
loanbut8 lb8 on nml.hmy = lb8.hcode
left join
loanbut9 lb9 on nml.hmy = lb9.hcode
where
nml.hprop in (2380, 3348)
and lb.lrPeriod in ('9/30/2021')
and lb9.lrnDate in ('9/30/2021')
group by
nml.hprop, nml.scode
In SQL Server DB if your subquery is written after the select command where we wrote field name list, then your subquery must be return only one record and only one field, else you will get an error. In your script, you wrote subquery before the from command, after this Loan_Num,. I did a little research on your subquery. Your subquery will return more than 1 record in most cases. The reason is that you wrote group by nml.hprop and after the where command you wrote this condition nml.hprop in (2380, 3348). I would have written this query for you myself, but I don't know your business logic and what need you. If your subquery must return more than 1 record, so you must join this subquery to the main query, using inner join or left join, you can not write this subquery on the field list.
Turns out that since my subquery's alias, nml, has the same alias as the parent query, nml (for the nmLoan table), it was did not work.
Upon changing my subquery's alias to nl and leaving the parent query's alias to nml, that actually worked and I was able to generate multiple results.
I would like to join two tables into master table (on to many) but I keep getting an error
Incorrect syntax near C.showdepartmentinventory_id
My code:
CREATE VIEW view_transactionshowidea
AS
SELECT
A.showidea_id, A.showideaupdatetype_id, A.showidea_proposedtitle,
A.showidea_fixtitle,
B.showinventorycategory_id, B.showinventorycategory_name,
C.showinventorydepartment_id, C.showinventorydepartment_name,
A.shoidea_duration, A.showidea_segment, A.showidea_PIC,
A.showidea_concept, A.showidea_isdisabled
FROM
transaction_showidea AS A
LEFT OUTER JOIN
view_showideainventory AS B
LEFT OUTER JOIN
view_showideainventorydepartment AS C ON showinventorycategory_id = B.showinventorycategory_id
AND showinventorycategory_name = C.showinventorydepartment_id
What is the problem with my query?
Each JOIN should be followed by its ON clause. The sources of your JOIN keys are not clear, but something like this:
FROM transaction_showidea si LEFT OUTER JOIN
view_showideainventory sii
ON si.showinventorycategory_id = sii.showinventorycategory_id LEFT OUTER JOIN
view_showideainventorydepartment siid
ON sii.showinventorycategory_name = siid.showinventorydepartment_id
Note that this introduces meaninful table aliases -- abbreviations for the table names. And all column references are qualified.
You need to specify "ON" right after your join statement like this:
CREATE VIEW view_transactionshowidea AS
SELECT A.showidea_id, A.showideaupdatetype_id, A.showidea_proposedtitle,
A.showidea_fixtitle, B.showinventorycategory_id, B.showinventorycategory_name,
C.showinventorydepartment_id, C.showinventorydepartment_name,
A.shoidea_duration, A.showidea_segment, A.showidea_PIC, A.showidea_concept,
A.showidea_isdisabled
FROM transaction_showidea AS A
LEFT OUTER JOIN view_showideainventory AS B on showinventorycategory_id = B.showinventorycategory_id
LEFT OUTER JOIN view_showideainventorydepartment AS C ON showinventorycategory_name = C.showinventorydepartment_id
Is there any other way to write this. If the Credit cardID in Application table is NULL, then join CreditcardID from Terminatedcreditcard table. Thanks,
This is the error:
Msg 4104, Level 16, State 1, Line 18
The multi-part identifier "TC.CreditCardID" could not be bound.
Code:
SELECT DISTINCT
prg.Title AS Program, a.Patientid,
a. Applicationid,
PT.MCC,
PT.MerchantName,
PT.MerchantCity, PT.MerchantState,
PT.MerchantZip,
PT.SettlementTransactionID,
CONVERT(DATE, PT.SettlementDate) AS SettlementDate
ABS(PT.Amount) as TransactionAmount
FROM
[dbo].[StagingSettlements] PT
LEFT JOIN
dbo.Application a ON PT.[CustomId] = ISNULL(a.CreditCardId, TC.CreditCardID)
LEFT JOIN
dbo.TerminatedCreditCard TC ON TC.ApplicationId = a.ApplicationId
You can change join precedence to get this:
FROM [dbo].[StagingSettlements] AS PT
LEFT JOIN ( dbo.Application AS a
LEFT JOIN dbo.TerminatedCreditCard AS TC on TC.ApplicationId = a.ApplicationId )
ON PT.[CustomId] = ISNULL( a.CreditCardId, TC.CreditCardID )
The addition of parentheses above causes Application to TerminatedCreditCard join to be evaluated first and the result is then joined to StagingSettlements.
A couple of points:
You don't appear to actually be using a credit card number in your result set, so I'm not clear on why you want it?
You need to put the NULL check in the join on TerminatedCreditCard. You are getting the error because you are trying to reference TerminatedCreditCard before you have joined to it.
The below SQL should sort you out, I wasn't sure of what the join condition would be between StagingSettlements and application, so I have just put in a placeholder which you will need to update, I have also added a 'CreditCardId' to the select:
SELECT Distinct
prg.Title as Program
,a.Patientid
,a.Applicationid
,PT.MCC
,PT.MerchantName
,PT.MerchantCity
,PT.MerchantState
,PT.MerchantZip
,PT.SettlementTransactionID
,convert(Date,PT.SettlementDate) as SettlementDate
,ABS(PT.Amount) as TransactionAmount
,COALESCE(TC.CreditCardId, a.CreditCardId) as CreditCardId
FROM [dbo].[StagingSettlements] PT
LEFT JOIN dbo.Application a
ON a.<applicationid> = PT.<applicationid>
LEFT JOIN dbo.TerminatedCreditCard TC
ON TC.ApplicationId = a.ApplicationId
AND a.CreditCardId IS NULL
Can't figure out why I can't perform this join, I've done many like it in the past. Am I missing something?
SELECT *
FROM THB_View.PCM_PCM_BASE base
LEFT OUTER JOIN THB_View.PCM_PCM_BASE_REOCCUR rec
LEFT OUTER JOIN THB_View.ATV atv ON base.PCM_TAG = atv.ATV_OCCURRING_PCM_TAG
ON base.RECORD_KEY = rec.RECORD_KEY
WHERE
base.PCM_STATUS = 'ENROUTE'
AND rec.PCM_HRC_TAG IS NULL OR rec.PCM_HRC_TAG = ''
AND rec.PCM_EQP_TAG IS NULL OR rec.PCM_EQP_TAG = ''
The error occurs on base.PCM_TAG the 5th line in the SQL statement
Your from clause is not the typical way to write the joins. It is allowed, but this affects the scoping of the identifiers. Essentially, this is interpreted as:
from THB_View.PCM_PCM_BASE base left join
(THB_View.PCM_PCM_BASE_REOCCUR rec left join
THB_View.ATV atv
on base.PCM_TAG = atv.ATV_OCCURRING_PCM_TAG
)
on base.RECORD_KEY = rec.RECORD_KEY
Which explains why base is not recognized.
Just write the joins the standard way with the on condition right after the tables being joined:
from THB_View.PCM_PCM_BASE base left join
THB_View.PCM_PCM_BASE_REOCCUR rec
on base.RECORD_KEY = rec.RECORD_KEY left join
THB_View.ATV atv
on base.PCM_TAG = atv.ATV_OCCURRING_PCM_TAG
I can simply use a select statment on a join to pull results I'd Like using:
Select * from RESULTS (NOLOCK) left join orders on Results.ordno = orders.ordno
left join folders on folders.folderno = orders.folderno
left join pranaparms on folders.prodcode = pranaparms.prodcode and results.analyte = pranaparms.analyte
WHERE Results.s <> 'OOS-A' and Results.Final Between pranaparms.LOWERQCLIMIT and pranaparms.UPPERQCLIMIT and (pranaparms.LOWERQCLIMIT IS NOT NULL and pranaparms.UPPERQCLIMIT IS NOT NULL)
and results.ordno in (1277494)
However, is there a convenient way which I could do an update on the selected fields?
I have tried this so far:
Update RESULTS (NOLOCK) left join orders on Results.ordno = orders.ordno
left join folders on folders.folderno = orders.folderno
left join pranaparms on folders.prodcode = pranaparms.prodcode and results.analyte = pranaparms.analyte
set Results.S = 'OOS-B'
WHERE Results.s <> 'OOS-A' and Results.Final Between pranaparms.LOWERQCLIMIT and pranaparms.UPPERQCLIMIT and (pranaparms.LOWERQCLIMIT IS NOT NULL and pranaparms.UPPERQCLIMIT IS NOT NULL)
and results.ordno in (1277494)
However, it is passing an error indicating "Incorrect syntax near '('"
Is there a way for me to update off of this join or will I need to do tables individually?
Your select syntax suggests SQL Server. The correct update syntax in SQL Server is:
update r
set S = 'OOS-B'
from results r left join
orders o
on r.ordno = o.ordno left join
folders f
on f.folderno = o.folderno left join
pranaparms p
on f.prodcode = p.prodcode and r.analyte = p.analyte
where r.s <> 'OOS-A' and
r.Final Between p.LOWERQCLIMIT and p.UPPERQCLIMIT and
(p.LOWERQCLIMIT IS NOT NULL and p.UPPERQCLIMIT IS NOT NULL) and
r.ordno in (1277494);
Notes:
Table aliases make the query much easier to write and to read.
Do not use NOLOCK unless you know what you are doing. Given that you don't know the syntax rules for update for the database you are using, I will guess that you don't understand locks either.
Your WHERE conditions are turning the outer joins into inner joins. You should just specify the correct join type -- and probably move the conditions to the on clauses. I've left the logic as you wrote it.