SQL Error: ambiguous column nameor syntax error [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Tried to change my code many times, need help with these errors, please!
Result: ambiguous column name: main.Guides.Guide_ID
SELECT *
FROM Guides
INNER JOIN Guides ON Guides_Countries.Guide_ID = Guides_Countries.Guide_ID
INNER JOIN Countries ON Countries.Country_ID = Guides_Countries.Country_ID
INNER JOIN Guides_Languages ON Guides.Guide_ID = Guides_Languages.Guide_ID
INNER JOIN Languages ON Languages.Language_ID = Guides_Languages.Language_ID
WHERE Countries.Name="Kazakhstan" AND (Languages.Name="German" OR Languages.Name="English") AND Guides.Guide_ID NOT IN
(SELECT Guide_ID
FROM GuidesUnavailableDates
INNER JOIN GuidesUnavailableDates ON GuidesUnavailableDates.UnDate_G_ID=Guides_UnDate.UnDate_G_ID
WHERE (Start_date<="21/06/2020" and End_date>="21/06/2020")
OR (Start_date<="30/06/2020" and End_date>="30/06/2020")
OR (Start_date>="21/06/2020" and End_date<="30/06/2020")
)
;

You have multiple table names appearing multiple times in your FROM clauses. That is causing your problem. I think you want:
SELECT *
FROM Guides g JOIN
Guides_Countries gc
ON gc.Guide_ID = g.Guide_ID JOIN
Countries c
ON c.Country_ID = gc.Country_ID JOIN
Guides_Languages gl
ON g.Guide_ID = gl.Guide_ID JOIN
Languages l
ON l.Language_ID = gl.Language_ID
WHERE c.Name = 'Kazakhstan' AND
l.Name IN ('German', 'English') AND
g.Guide_ID NOT IN (SELECT gud.Guide_ID
FROM GuidesUnavailableDates gud
WHERE gud.Start_date <= '2020-06-30' AND
gud.End_date >= '2020-06-21'
);
Notes:
Guide_Countries is not in your FROM list although Guides is there twice.
IN is much simpler than mutiple ORs.
All columns are qualified so it is clear what table they are coming from.
All tables have simple table aliases which are abbreviations for the table names.
There is no need for a JOIN in the subquery.
Use well formatted dates.
I am guessing that the weird date logic is to find an overlap with the time period mentioned so I simplified the logic. (You haven't explained the logic, so this is a guess and your original code doesn't make sense.)
I strongly recommend using NOT EXISTS with subqueries rather than NOT IN. However, I did not change the code here (mostly because I can't really tell what the subquery is supposed to be doing).

Related

How to join tables on calculation - Oracle [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
i am trying to join these two tables, but I can only do so if I change values of columns while joining like "substr(to_char(p.personnummer),3)" and "substr(k.ip_id,1,10)" . Can anyone tell me what is the order I am supposed to do it?
select x.ip_id, x.organisationsnummer
from (select
substr(to_char(p.personnummer),3) as ip_id_jnr,
substr(k.ip_id,1,10) as ipo_id
from customer k
inner join customer_VIEW p on ipo_id = ip_id_jnr) x
;
Your query doesn't show which table contains organisationsnummer; I used the k alias (as if it belongs to customer); fix it if necessary.
So, inline view:
select k.organisationsnummer,
k.ip_id
from customer k join customer_view p
on substr(to_char(p.personnummer),3) = substr(k.ip_id,1,10);
Now use it:
select x.ip_id,
x.organisationsnummer
from (select k.organisationsnummer,
k.ip_id
from customer k join customer_view p
on substr(to_char(p.personnummer),3) = substr(k.ip_id,1,10)
) x;

COUNT() function is not working for ID's in oracle ? Checked all themes in the sites ,no one works for me [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
SELECT *
FROM (SELECT Count(q.test_disciplina_id)"|BROI|",
p.predmet "|Predmet|",
t.naimenovanie_test "|TEST|",
data
FROM qvqvaniq_na_test q
inner join test_disciplina td
ON ( q.test_disciplina_id = td.id_disciplina_test )
inner join test t
ON ( td.test_id_test = t.id_test )
inner join disciplina d
ON ( td.disciplina_id_disciplina = d.id_disciplina )
inner join predmeti p
ON ( d.predmeti_id_predmet = p.id_predmet )
GROUP BY predmet,
naimenovanie_test,
data)
WHERE To_date(data) BETWEEN To_date('2018-01-01') AND To_date('2019-10-10');
Start over with
select count(q.test_disciplina_id)
from qvqvaniq_na_test q
and make sure query returns something. Then add another table; test, and so forth. If you don't get expected result in any of those steps, don't go any further until you fix the error.
Query you wrote looks OK. With almost no information, it is difficult to guess what might be wrong; here are a few possibilities:
you should have used outer join
why did you use an inline view? I don't see any reason. Remove it, and put WHERE clause where it belongs
WHERE clause should be rewritten. Currently, it is
WHERE To_date(data) BETWEEN To_date('2018-01-01') AND To_date('2019-10-10');
what is DATA's datatype? If it is a string TO_DATE should have appropriate format mask, e.g. to_date(data, 'yyyy-mm-dd'). If it is a date, then remove TO_DATE entirely
to_date('2018-01-01') also misses format mask. Don't rely on Oracle's implicit conversion, it'll fail sooner or later. Either apply format mask (as previously), e.g. to_date('2018-01-01', 'yyyy-mm-dd') or use a date literal, date '2018-01-01'
Fixed, it might look like
where to_date(data, 'yyyy-mm-dd') between date '2018-01-01' and date '2019-10-10'
Now, your turn.

incomplete output ms access 2007 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
SELECT temp.hhid, temp.country, temp.max_prod, temp.max_area, gen.price_seed, gen.qty_seed, gen.price
FROM (SELECT hhid, country, MAX(area) AS max_area, max(total_prod) AS max_prod FROM gen GROUP BY hhid, country) AS temp, gen
WHERE (((temp.hhid)=gen.hhid) And ((temp.country)=gen.country) And ((temp.max_prod)=gen.total_prod) And ((temp.max_area)=gen.area))
ORDER BY temp.hhid;
Why do some of the results were not seen?
I have atleast 100 hhid , each one has 3 area , 3 productions , quantity of seed , price etc ...
all of the hhid was shown in the ouput query except for 1 hhid ,
what might be wrong ?
The problem is that the maximum of prod and the maximum of area are rarely in the same row.
You should also learn to use explicit join syntax. A simple rule: never use a comma in the from clause.
This may be what you want:
SELECT temp.hhid, temp.country, temp.max_prod, temp.max_area, gen.price_seed,
gen.qty_seed, gen.price
FROM gen INNER JOIN
(SELECT hhid, country, MAX(area) AS max_area, max(total_prod) AS max_prod
FROM gen
GROUP BY hhid, country
) AS temp
ON temp.hhid = gen.hhid AND temp.country = gen.country
WHERE (temp.max_prod = gen.total_prod or temp.max_area = gen.area)
ORDER BY temp.hhid;
I left the WHERE clause in, because MS Access has strange restrictions on joins. Logically it should go in the ON clause, but I'm not 100% sure that Access accepts that syntax.

Does anyone know why I am not getting any results with this query? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I will just go straight to the problem.
This query is supposed to a list of records where PFlag is set to 'No'
However, when I run it, I get blank results.
If I remove the WHERE clause (where pFlag='No'), I get results.
So, the WHERE clause is presenting a problem.
Any idea what is wrong?
Here is the code I am currently running.
SELECT DISTINCT t.username,
lg.Email,
lg.fullname,
c.CourseName,
l.location,
d.trainingDates,
d.trainingTime,
t.processedFlag,
i.instructorName
FROM tblTrainings t
INNER JOIN tblCourses c on t.courseId = c.courseId
INNER JOIN tblLocations l on t.locationId = l.LocationId
INNER JOIN tblTrainingDates d on t.dateid=d.dateid
INNER JOIN tblCourseInstructor ic on c.courseId = ic.CourseId
INNER JOIN tblInstructors i on ic.instructorId = i.instructorId
INNER JOIN tblLogin lg on t.username = lg.username
WHERE t.PFlag = 'No'
ORDER BY lg.fullname DESC
It seems simple enough.
Thanks a lot in advance
PFlag
0x59006500
0x59006500
0x59006500
try rewriting the WHERE statement like this:
WHERE t.PFlag like '%No%'
hope it helps.
Check your table columns definitions.
if the type of field pFlag is CHAR with particular length, e.g. CHAR[4], then any string in that column will be filled up to 4 chars.
In that case the real data stored in your table will be No[space][space].
If this is the case, convert your column type to VARCHAR, and do an update to that column with trim().
Try running it like #user2065377 said :
WHERE t.PFlag like '%No%'
Also :
Run the query without the where clause and add this column : SELECT ...., CAST(t.PFlag AS VARBINARY(4)) and see the distinct values.
maybe you have whitespace (invisible) chars
also paste the result for :
select distinct ( t.PFlag) from .....
edit
SELECT CONVERT(VARCHAR(100), 0x59006500) yields Y
Where is the Yes word ?
even so , it's not an ordinary Y :
SELECT CASE WHEN CONVERT(nVARCHAR(100), 0x59006500)='Y' THEN 1 ELSE 0 END ( yields 0)

Why does this not pull any data? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
The following snippet is a join on multiple tables. I need to display all order number, customer name, product name, price and quantity ordered from anyone from Australia. I'm getting table headings but no rows. Is there something wrong with this?
SELECT
"order".orderno AS ord,
customer.cname,
product.prodname,
customer.country_code,
orderdetl.price,
orderdetl.qty,
country.country_code
FROM
prema01.country,
prema01.customer,
prema01."order",
prema01.orderdetl,
prema01.product
WHERE
customer.country_code = 'AUS'
I've changed the code, verified there is data in the tables and it still comes out blank.
I am completely stumped.
SELECT O.ORDERNO, C.CNAME, PN.PRODNAME, ODT.PRICE, ODT.QTY, ODT.QTY * PN.PRODSELL AS TOTAL
FROM prema01.ORDER O, prema01.CUSTOMER C, prema01.ORDERDETL ODT, prema01.PRODUCT PN, prema01.COUNTRY CT
WHERE CT.COUNTRY_NAME = 'Australia'
AND C.COUNTRY_CODE = CT.COUNTRY_CODE
AND C.CUSTNO = O.CUSTNO
AND O.ORDERNO = ODT.ORDERNO
AND PN.PRODNO = ODT.PRODNO AND O.ORDERNO <= 60
ORDER BY TOTAL DESC;
forgot to add the change. the tables have data in them, i've physically verified that.
For starters, you need some join conditions e.g.
Select
r.country_code,
c.cname
From
prema01.country r
inner join
prema01.customer c
on r.country_code = c.country_code
You need to build up the relationships with the other tables in a similar way.
Also, are you sure your tables have any data in them. Does
Select
Count(*)
From
prema01.country
return anything? How about
Select
Count(*)
From
prema01.customer
Where
country_code = 'AUS'
?
Fixed, I was looking for 'Australia' while I have 'AUSTRALIA' in my country table. thanks for the help