Issue Related to Join in MS Access Database - sql

I am using the following query in an MS Access database:
SELECT SD.RollNo, SD.Name , ED.ExamName, (
SELECT count(*)
FROM (
SELECT DISTINCT innerED.StudentId
FROM ExamDetails innerED
WHERE innerED.StudentId=SD.StudentId
)
) AS StudentId
FROM StudentDetails SD
LEFT OUTER JOIN ExamDetails ED
ON SD.StudentId= ED.StudentId
Whenever I execute this query, a dialog box comes up and asks for the value of the parameter SD.StudentId. Why is it asking for this, and how do I stop it from doing so?

MS Access does not understand the SELECT statement on the Count (*) Aggregate. To Access the SQL Statement looks like this.
SELECT DISTINCT innerED.StudentId
FROM ExamDetails innerED
WHERE innerED.StudentId=SD.StudentId
Because the alias AS STUDENTID comes after the end of the statement, this Select statement doesn't recognize it, so it has no idea what .StudendID is so it assumes it's a parameter.
MS Access, when faced with a parameter that has not been identified in the query itself will prompt the user for a value.
Rewrite the query so that this Select statement can identify all the table sources.

Related

Oracle subquery after like error

I have the following SQL query
SELECT * FROM users u WHERE 1=1 and u.user_name LIKE 'A%'
It works as expected. But the following line results in an error message.
SELECT * FROM users u WHERE 1=1 and u.user_name LIKE (select '%arthur%' from dual)
The error message is the following:
ORA-00933 SQL Command not properly ended
I have tried to close the query with ";" but it is still gives the same error. What could cause this error?
Edit: I need LIKE and can't use IN, lets assume we have only one 'arthur' in the users database.
Your query appears to be correct syntactically. The subquery is a scalar subquery, which returns only one column and at most one row.
In general, you can use LIKE with a subquery using EXISTS:
SELECT u.*
FROM users u
WHERE 1 = 1 AND
EXISTS (SELECT 1
FROM t
WHERE u.user_name LIKE t.col
);
If your actual query is more complicated, then this might solve your actual problem.
i have tried same query as per your,it worked fine.
SELECT * FROM employee e where 1=1 and e.name like (select '%abc%' from dual);

DB2 SELECT EXCEPT with WHERE clause

I'm trying to compare two tables in a DB2 database in z/OS using SPUFI to submit SQL queries.
I'm doing this by using EXCEPT to see the difference between two SELECT queries.
I need to filter the SELECT statement from the first query with a WHERE clause.
SELECT KEY_FIELD_1,LOOKUP_FIELD_1
FROM TABLE_1
WHERE FILTER_FIELD = '1'
EXCEPT
SELECT KEY FIELD_2,LOOKUP_FIELD_2
FROM TABLE_2
I got results back, but it also returned an error -199 Is this because the WHERE clause is not present in the second SELECT statement?
ERROR: ILLEGAL USE OF KEYWORD EXCEPT.
TOKEN <ERR_STMT> <WNG_STMT> GET SQL
SAVEPOINT HOLD FREE ASSOCIATE WAS EXPECTED
Try introducing parentheses e.g.
( SELECT KEY_FIELD_1,LOOKUP_FIELD_1
FROM TABLE_1
WHERE FILTER_FIELD = '1' )
EXCEPT
( SELECT KEY FIELD_2,LOOKUP_FIELD_2
FROM TABLE_2 )

Need SQL with subquery to get distinct values for VBA code

I have a table BAR_DATA with two fields: LongDate, Time. Both are long integers. No Access Date/Time involved here.
For each distinct LongDate value there are hundreds of records, each with Time value which may be distinct or duplicate within that LongDate.
I need to create an SQL statement that will group by LongDate and give me a count of distinct Times within each LongDate.
The following SQL statement, (built by an Acess query) does NOT work (some LongDates are omitted):
Query A
SELECT DISTINCT BAR_DATA.LongDate, Count(BAR_DATA.Time) AS CountOfTime
FROM BAR_DATA
GROUP BY BAR_DATA.LongDate
HAVING (((Count(BAR_DATA.Time))<>390 And (Count(BAR_DATA.Time))<>210));
However, if I use Query B to reference Query DistinctDateTime, it does work:
Query B
SELECT DistinctDateTime.LongDate, Count(DistinctDateTime.Time) AS CountOfTime
FROM DistinctDateTime
GROUP BY DistinctDateTime.LongDate
HAVING (((Count(DistinctDateTime.Time))<>390 And (Count(DistinctDateTime.Time))<>210));
Query DistinctDateTime
SELECT DISTINCT BAR_DATA.LongDate, BAR_DATA.Time
FROM BAR_DATA;
My problem:
I need to get Query B and Query DistinctDateTime wrapped into a single SQL statement so I can paste it into a VBA function. I presume there
is some subquery techniques, but I have failed at every attempt, and find no pertinent example.
Any help will be greatly appreciated. Thanks!
Subquery your distinct table inside and perform your aggregates outside until you get the desired result:
SELECT DistinctDateTime.LongDate, Count(DistinctDateTime.Time) AS CountOfTime
FROM
(
SELECT DISTINCT BAR_DATA.LongDate, BAR_DATA.Time
FROM BAR_DATA
) AS DistinctDateTime
GROUP BY DistinctDateTime.LongDate
HAVING (((Count(DistinctDateTime.Time))<>390 And (Count(DistinctDateTime.Time))<>210));

SQL Query to select one field order by other field

I want to select a field in select statement and order by with another field but sql server doesn't allows this as it says order by item must appear in the select statement if select distinct is specified.
This is what I tried :
select DISTINCT format_type
from Labels_Add_Label
where external_group_id= 2826
order by group_sequence
What changes are required to do in this query?
Please provide the changed query
You can rewrite your query this way (equivalent to distinct):
SELECT format_type
FROM Labels_Add_Label
WHERE external_group_id= 2826
GROUP BY format_type;
and you can't use ORDER BY group_sequence here. There may be more than one row with same format_type but different group_sequence. SQL server doesn't know which one should be used for the ordering.
You can however use aggregate functions with a GROUP BY query:
SELECT format_type
FROM Labels_Add_Label
WHERE external_group_id= 2826
GROUP BY format_type;
ORDER BY MIN(group_sequence) ; -- or MAX(group_sequence)
I just took this question to test my knowledge and have come with the following solution (using CTE in MS SQL Server), please correct me if I'm wrong - Using the NORTHWIND Database (Employees Table) on MS SQL Server, I have written this query - This could be one other option that could be used, if there be a need to!
WITH CTE_Employees(FirstName, LastName, BirthDate)
AS
(
SELECT FirstName, LastName, BirthDate
FROM Employees
WHERE Region IS NOT NULL
)
SELECT FirstName FROM CTE_Employees ORDER BY BirthDate DESC
As mentioned above, there can be a same Employee FirstName but with a different LastName, hence SQL Server imposes a condition where we can't use DISTINCT in conjunction with ORDER BY...
Hope this helps!

CROSS JOIN of query and single-row table

I have a big query and a table with a single row (I store some constants in it).
What is the best way to join the row of the table with every row of the query considering that Access doesn't support cross joins with queries?
SELECT * from (subquery), table -- Invalid in Access
Access will accept a cross join between a query named some_query and a table named some_table like this ...
SELECT *
FROM some_query, some_table;
With your names, try it this way ...
SELECT * from [some query], [table]
IOW, get rid of the parentheses, and enclose the data source names in square brackets because of the space in some query and because table is a reserved word.
OTOH, if you meant some query to be a placeholder for the text of a SQL statement instead of the name of a saved query, consider this example.
SELECT *
FROM
(SELECT * FROM agents) AS sub, Dual;
According to Microsoft and this previous question, cross joins are legal. You say is it invalid, but did you get an error message when you tried?