The multi-part identifier could not be bound. 3 table join - sql

USE pubs
SELECT DISTINCT L.au_fname, L.au_lname
FROM dbo.authors L, dbo.titles B
JOIN dbo.titleauthor C ON dbo.authors.au_id=C.au_id
INNER JOIN dbo.titleauthor C1 ON B.title_id=C1.title_id
WHERE B.price >= 13
ORDER BY L.au_lname ASC
The bolded dbo.authors in this code keeps producing the error below... I clearly don't understand aliases because i cannot for the life of me, determine what is wrong. I tried changing it to "L" but the same error happens. I didn't think a double join would be this hard.
To explain what I am trying to do here. I am trying to get information from table authors to display based on a WHERE statement on table titles. however they only have common PK's on a 3rd table named title author. There for I am trying to join all 3 tables together via the common PK's.

At one location you are using cartesian product and at one location you are using joins..thats not allowed.
You need to use like below- Add join conditions....I dont have ur column names.
USE pubs
GO
SELECT DISTINCT L.au_fname, L.au_lname
FROM dbo.authors L
INNER JOIN dbo.titleauthor C ON L.au_id=C.au_id
INNER JOIN dbo.titles B ON B.title_id=C.title_id
WHERE B.price >= 13
ORDER BY L.au_lname ASC

Related

ORA-00904 for one INNER JOIN but not for another

for one particular query I've written up the ORA-00904 error has occurred
SELECT '103040698' as StudID, M.Title, R.SHORTDESC, C.COLOURNAME
FROM MOVIE0698 M
INNER JOIN RATING0698 R
ON M.SHORTDESC = R.SHORTDESC
INNER JOIN COLOURTYPE0698 C
ON M.COLOURNAME = C.COLOURNAME
ORDER BY Title ASC;
specfically for ON **M.**SHORTDESC = R.SHORTDESC
However, the previous query I had just written beforehand that used an INNER JOIN as well
SELECT '103040698' as StudID, A.FullName, M.Title, M.RelYear
FROM CASTING0698 C
INNER JOIN ACTOR0698 A
ON C.ActorNo = A.ActorNo
INNER JOIN MOVIE0698 M
ON C.MovieNo = M.MovieNo
ORDER BY RelYear DESC;
this query runs perfectly, so I'm just unclear what I've done wrong.
From what I remember INNER JOINs are written with the child table first then the parent table e.g. ON C.ActorNo = A.ActorNo but I could be wrong since I know some software will accept either way while others are sensitive to it.
for context, I'm using SQLJunior for my school work
here is the whole error
ON M.shortdesc = R.shortdesc
*
Error at line 4: ORA-00904: "M"."SHORTDESC": invalid identifier
In plain English the error message means "the MOVIES0698 table doesn't have a column called SHORTDESC"
Remember that Oracle column names are only case insensitive if they are not enclosed in quotes, or were quoted allcaps. If a column name is enclosed in quotes, and is not all uppercase, when it is created then it must forced more be referred to using quotes
SHORTDESC -- not case sensitive, you could write SELECT shortDesc
"SHORTDESC" -- not case sensitive, because it's all caps anyway, can write SELECT shortdesc
"ShortDesc" -- case sens, must refer to this column using quotes forever more: SELECT "ShortDesc"... JOIN ON M."ShortDesc" etc
In summary, never enclosed oracle column names in quotes, for any reason. If you want to call a column something that is a reserved word, call it something else
If movies has no such column you have to join on a different column (SHORTDESC seems like it would be a text column, which probably makes for a poor join target anyway; is there no other column that is intended to relate movies and ratings better? Such as a "movieid" in the ratings table? It seems to me like one movie could have one or more ratings (some critics rate it high, others low, and you average the scores of many ratings..?)
I was using the wrong column names to join these tables together, i was writing it as
SELECT '103040698' as StudID, M.Title, R.SHORTDESC, C.COLOURNAME
FROM MOVIE0698 M
INNER JOIN RATING0698 R
ON M.SHORTDESC = R.SHORTDESC
INNER JOIN COLOURTYPE0698 C
ON M.COLOURNAME = C.COLOURNAME
ORDER BY Title ASC;
instead of
SELECT '103040698' as StudID, M.Title, R.SHORTDESC, C.COLOURNAME
FROM MOVIE0698 M
INNER JOIN RATING0698 R
ON M.ratingcode= R.ratingcode
INNER JOIN COLOURTYPE0698 C
ON M.colourcode= C.colourcode
ORDER BY Title ASC;
TL:DR
I had to use the proper foreign/primary key for INNER JOIN

Join four tables together when only two tables are connected to the junction table

I have four tables created and I need join all the four tables and get the result. Following is the table structure. The CAT table is the junction table here and from it, it has one to many relations to CATFOOD and CUBS tables. From the CATFOOD table, there's another one to many relationship to the table BRAND.
I tried the following query to first join the BRAND and CATFOOD tables and next join the result of that with CAT table. But it didn't work. Please note that I also join the table CUBS with the table CAT too. Here's what I tried,
SELECT CAT.CAT_ID,
CAT_TYPE,
CAT_COLOR,CUBS.CUB_ID,
CUBS.CUB_NAME,
CATFOOD.CATFOOD_TYPE,
CATFOOD.CATFOOD_ID,
CATFOOD.CATFOOD_STATUS,
CATFOOD.SELLER_ID,
BRAND.BRAND_ID,
BRAND.FLAVOUR
FROM CAT RIGHT JOIN CUBS
ON CAT.CAT_ID = CUBS.CAT_ID
RIGHT JOIN (
SELECT CATFOOD.CATFOOD_ID,
CAT_ID,
CATFOOD_TYPE,
SELLER_ID,
CATFOOD_STATUS,
BRAND.BRAND_ID,
FLAVOUR
FROM CATFOOD RIGHT JOIN BRAND
ON CATFOOD.CATFOOD_ID = BRAND.CATFOOD_ID)
AS TEMP ON CAT.CAT_ID = TEMP.CAT_ID
WHERE CAT.CAT_ID = 'some_id_in_the_db';
When I execute this, I get the following error.
Unknown column 'CATFOOD.CATFOOD_TYPE' in 'field list'
When I remove the columns that the error message mention from the select statement, it doesn't give me any errors but returns an empty result.
What I simply want to achieve is get all the details related to a provided CAT_ID so in the java level, I can construct the response appropriately.
Please if anyone know how to achieve this, I appreciate it very much. Thanks in advance.
Update: previously, CATFOOD_TYPE field was mistakenly mentioned as CARFOOD_TYPE in the question. It is corrected now.
You are joining a subquery which contains the column CATFOOD.CATFOOD_TYPE, but this column is not visible to the outer query by that alias/name.
You should alias the subquery, say t and refer to the column as t.CATFOOD_TYPE.
But there is no need to complicate the requirement.
You can join the table cat with all the other tables with LEFT joins:
SELECT c.cat_id, c.cat_type, c.cat_color,
cu. cub_name,
f.catfood_type, f.catfood_id, f.catfood_status,
b.brand_id, b.flavour
FROM cat c
LEFT JOIN cubs cu ON cu.cat_id = c.cat_id
LEFT JOIN catfood f ON f.cat_id = c.cat_id
LEFT JOIN brand b ON b.catfood_id = f.catfood_id
WHERE c.cat_id = 'some_id_in_the_db';
You got a typo, should be CATFOOD_TYPE, not CARFOOD_TYPE
I think you can try this =>
SELECT DISTINCT c.CAT_ID,
CAT_TYPE,
CAT_COLOR,cb.CUB_ID,
cb.CUB_NAME,
cf.CATFOOD_TYPE,
cf.CATFOOD_ID,
cf.CATFOOD_STATUS,
cf.SELLER_ID,
bd.BRAND_ID,bd.FLAVOUR
FROM CAT c
LEFT JOIN CUBS cb ON c.CAT_ID=cb.CAT_ID
LEFT JOIN CATFOOD cf ON c.CAT_ID=cf.CAT_ID
LEFT JOIN BRAND bd ON cf.CATFOOD_ID=bd.CATFOOD_ID

How to use Except clause in Bigquery?

I am trying to use the existing Except clause in Bigquery. Please find my query below
select * EXCEPT (b.hosp_id, b.person_id,c.hosp_id) from
person a
inner join hospital b
on a.hosp_id= b.hosp_id
inner join reading c
on a.hosp_id= c.hosp_id
As you can see I am using 3 tables. All the 3 tables have the hosp_id column, so I would like to remove duplicate columns which are b.hosp_id and c.hosp_id. Simlarly, I would like to remove b.person_id column as well.
When I execute the above query, I get the syntax error as shown below
Syntax error: Expected ")" or "," but got "." at [9:19]
Please note that all the columns that I am using in Except clause is present in the tables used. Additional info is all the tables used are temp tables created using with clause. When I do the same manually by selecting column of interest, it works fine. But I have several columns and can't do this manually.
Can you help? I am trying to learn Bigquery. Your inputs would help
I use the EXCEPT on a per-table basis:
select p.* EXCEPT (hosp_id, person_id),
h.*,
r.* EXCEPT (hosp_id)
from person p inner join
hospital h
on p.hosp_id = h.hosp_id inner join
reading r
on p.hosp_id = r.hosp_id;
Note that this also uses meaningful abbreviations for table aliases, which makes the query much simpler to understand.
In your case, I don't think you need EXCEPT at all if you use the USING clause.
Try this instead:
select * EXCEPT (person_id) from
person a
inner join hospital b
using (hosp_id)
inner join reading c
using (hosp_id)
You can only put column names (not paths) in the EXCEPT list, and you can simply avoid projecting the duplicate columns with USING instead of ON.

Getting way more results than expected in SQL left join query

My code is such:
SELECT COUNT(*)
FROM earned_dollars a
LEFT JOIN product_reference b ON a.product_code = b.product_code
WHERE a.activity_year = '2015'
I'm trying to match two tables based on their product codes. I would expect the same number of results back from this as total records in table a (with a year of 2015). But for some reason I'm getting close to 3 million.
Table a has about 40,000,000 records and table b has 2000. When I run this statement without the join I get 2,500,000 results, so I would expect this even with the left join, but somehow I'm getting 300,000,000. Any ideas? I even refered to the diagram in this post.
it means either your left join is using only part of foreign key, which causes row multiplication, or there are simply duplicate rows in the joined table.
use COUNT(DISTINCT a.product_code)
What is the question are are trying to answer with the tsql?
instead of select count(*) try select a.product_code, b.product_code. That will show you which records match and which don't.
Should also add a where b.product_code is not null. That should exclude the records that don't match.
b is the parent table and a is the child table? try a right join instead.
Or use the table's unique identifier, i.e.
SELECT COUNT(a.earned_dollars_id)
Not sure what your datamodel looks like and how it is structured, but i'm guessing you only care about earned_dollars?
SELECT COUNT(*)
FROM earned_dollars a
WHERE a.activity_year = '2015'
and exists (select 1 from product_reference b ON a.product_code = b.product_code)

Error while posting to a table

Platform used:
SQL Server 2008 and C++ Builder
I am doing an inner join between 2 tables which was giving me an error:
Row cannot be located for updating
Query:
SELECT DISTINCT
b.Acc, b.Region, b.Off, b.Sale, a.OrgDate
FROM
sales b
INNER JOIN
dates a ON (a.Acc = b.Acc and a.Region = b.Region and a.year= b.year)
WHERE
(a.xdate <> a.yDate)
and (b.Sale = a.SaleDate)
and b.year = 2010
Note: Acc, Region, Off are primary keys of table b and are also present in table a.
Table a has an id which is the primary key which does not appear in the query.
It turned out that my inner join was returning duplicate rows.
I changed my inner join query to use 'DISTINCT' so that only distinct rows are returned and not duplicate. The query runs but then I get the error:
Insufficient key column information for updating or refreshing.
It does turn out that the fields which are primary keys in Table A have the same names as the fields in Table B
I found that this is a bug which occurs while updating ADO record-sets.
BUG: Problem Updating ADO Hierarchical Recordset When Join Tables Share Same Column Name
I have the following 2 questions:
Is it not a good idea to use Distinct on an inner join query?
Has anyone found a resolution for that bug associated with TADO Query's?
Thank you,
The way I would solve this is to construct an update query by hand and run it through TADOQuery.ExecSQL. That assumes you actually know what you are doing.
The question is WHY are you working on a recordset that results in multiples of the same row, on all fields? You should be inspecting your query and fixing it. DISTINCT doesn't help, because SQL Server has picked one record but ADO won't know which one it picked, since there isn't enough information to properly identify the source on each side of the JOIN.
This query pulls in a.id to make the source records identifiable:
SELECT Acc,Region,Off,Sale,OrgDate,id
FROM
(
SELECT b.Acc,b.Region,b.Off,b.Sale,a.OrgDate, a.id,
rn=row_number() over (partition by b.Acc,b.Region,b.Off order by a.id asc)
FROM sales b
JOIN dates a ON(a.Acc = b.Acc and a.Region = b.Region and a.year= b.year)
WHERE a.xdate <> a.yDate
and b.Sale = a.SaleDate
and b.year = 2010
) X
WHERE rn=1;
Not tested, but it should work with ADO