Here is my query:
SELECT
t2.*
FROM
(
SELECT
FullName
FROM
pr
GROUP BY
FullName
HAVING
COUNT(*)>=2
) T1
JOIN
pr T2 ON T1.FullName = T2.FullName;
I used this answer to build this: SQL Return only duplicate rows.
Although they didn't specify which DBMS they were using, I can assume it was not MS Access since it ran properly for them and not me.
Whenever I try to save this query, it says:
Syntax error in FROM clause
Not sure where this is wrong. I know that access requires some weird brackets during joins but the problem is apparently with one of the FROM statements. Any ideas here?
I would expect an MS Access query to look like this:
SELECT t2.*
FROM (SELECT FullName
FROM pr
GROUP BY FullName
HAVING COUNT(*) >= 2
) AS T1 INNER JOIN
pr as T2
ON T1.FullName = T2.FullName;
Note the ass and INNER.
Related
For example
select *
from t1
inner join (select * from t2 where t2.id not in (select ID from t2 where city="Paris"))
I tried searching Google. There a lot of examples but none of them uses not in. Plus there are no restrictions specified for an in line view.
Oracle calls subqueries in the FROM clause "inline views".
These are generic SELECT queries. They can contain NOT IN with subqueries. The problem with your query is a lack of ON clause and the use of double quotes for a string constant:
select *
from t1 inner join
(select *
from t2
where t2.id not in (select ID from t2 where city = 'Paris')
---------------------------------------------------------^ single quotes
) t2
on t1.? = t2.?
-----^ on clause
Note: I would discourage you from using NOT IN with subqueries, because they do not work as expected if any returned values are NULL. (If that is the case, then no rows are returned.)
I advise using NOT EXISTS instead.
For example, given a table Cust, containing a field name, values of name might be "aabb", "bb", "cc". I would like to make a query, which in this instance would return the records for the first two. What I've tried is:
SELECT DISTINCT AA.*
FROM (SELECT A.*,IIF(B.ID="","","Dup") DUP
FROM Cust A LEFT JOIN Cust B ON a.ID <> b.ID
AND IF(LENGTH(A.name)>LENGTH(B.name),
INSTR(A.name,B.name)>0,
INSTR(B.name,A.name)>0)
) AS AA;
I haven't quite got the hang of SQL yet so I'm struggling to make this work... The error I get at the moment is Syntax Error (missing operator) in query expression 'IIF(B.ID = "", "", "Dup") DUP'
Change
IIF(B.ID="","","Dup") DUP
to
IIF(B.ID="","","Dup") as DUP
Access SQL requires the AS keyword when you alias a field.
Also, it looks like you're spelling IIF as IF in your WHERE clause. That will also result in a syntax error.
Perhaps you should be using LIKE:
select t1.name, t2.name
from table1 as t1
inner join table1 as t2
ON t1.name like '%'+t2.name+'%'
AND t1.name <> t2.name
See example: http://sqlfiddle.com/#!6/3af5b/4
I need to output data from one table but only if that customers name has shown up in another table and I'm not quite sure how get this to work. Thanks
You can use an INNER JOIN which will return all rows that appear in both tables:
select t1.*
from table1 t1
inner join table2 t2
on t1.name = t2.name
If you need help learning JOIN syntax, then here is a great visual explanation of joins
This is a basic SQL query:
SELECT *
FROM t
WHERE t.name IN (SELECT name FROM t2);
There are other ways to express this. Are you new to SQL?
Please bear with me new to SQL- I am trying to write an SQL command with a join in a PROGRESS db. I would like to then select only the first matching record from the join. I thought to use LIMIT but PROGRESS does not support that. MIN or TOP would also work I think but having trouble with the syntax.
Something like this?-
SELECT table1.field 1, table2.field 2
FROM table2
INNER JOIN table2
ON table1.field3=table2.field3
WHERE table1.field4 in (SELECT min(table1.field4) FROM table1)
BUt it appears I can't use MIN there as saying can't do an aggregate there.
Any help would be huge.
try:
SELECT
t1.field1, t2.field2
FROM table1 t1
INNER JOIN table2 t2 ON t1.field3=t2.field3
WHERE t1.field4=(SELECT min(t.field4) FROM table1 t WHERE t1.field4=t.field4)
I have the following query which works fine with MySQL but refuses to work with SQL server:
SELECT table1.someField AS theField,
COUNT(table2.someField) / (SELECT COUNT(someField) FROM table1 WHERE someField = theField),
FROM table1 LEFT JOIN table2 ON table1.someField = table2.someField
SQL Server doesn't seem to like the alias in the subquery. I've been told I need to use a CTE but I've never used them before. Is this correct?
The problem might well be in the confusion in the sub-query
SELECT COUNT(someField) FROM table1 WHERE someField = theField
the someField in the condition will be local - but you can get to table1.someField just the same.
How about
SELECT COUNT(t3.someField) FROM table1 t3 WHERE t3.someField = table1.someField
?