I'm trying to create a query using the SQL SELECT statement but I keep getting a error with my code
This is my code:
SELECT *
FROM tblRegistration
WHERE (tblRegistration.InstanceID IN (
SELECT InstanceID FROM tblRegistration
SELECT tblCourse.InstanceID, tblCourse.HoursPerWeek
FROM tblCourse
WHERE (((tblCourse.HoursPerWeek)=40));
Access throws in far more parentheses in WHERE clause than are needed. Simplify by removing most of them then test. Access will eventually throw them back in if you switch builder between datasheet and design view and save the object. Always make sure brackets, parens, quote marks, and apostrophes are evenly paired.
Also have too many SELECT clauses.
Subquery for IN() must return only one field.
Consider:
SELECT *
FROM tblRegistration
WHERE tblRegistration.InstanceID IN (
SELECT tblCourse.InstanceID
FROM tblCourse
WHERE tblCourse.HoursPerWeek=40);
If you don't want to remove field from subquery, try:
SELECT *
FROM tblRegistration
WHERE EXISTS (
SELECT tblCourse.InstanceID, tblCourse.HoursPerWeek
FROM tblCourse
WHERE tblCourse.HoursPerWeek=40);
Related
I have over 40 tables I want to append in BigQuery using standard SQL. I have already formatted them to have the exact same schema. When I try to use the '*' wildcard at the end of table name in my FROM clause, I get the following error:
Syntax error: Expected end of input but got "*" at [95:48]
I ended up manually doing a UNION DISTINCT on all my tables. Is this the best way to do this? Any help would be appreciated. Thank you!
CREATE TABLE capstone-320521.Trips.Divvy_Trips_All AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_*;
--JOIN all 2020-21 trips data
CREATE TABLE capstone-320521.Trips.Divvy_Trips_Raw_2020_2021 AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_04
UNION DISTINCT
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_05
UNION DISTINCT
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_06
UNION DISTINCT
Syntax error: Expected end of input but got "*"
I think the problem is in missing ticks around the table references. Try below
CREATE TABLE `capstone-320521.Trips.Divvy_Trips_All` AS
SELECT * FROM `capstone-320521.Trips.Divvy_Trips_*`
Note: The wildcard table name contains the special character (*), which means that you must enclose the wildcard table name in backtick (`) characters. See more at Enclose table names with wildcards in backticks
I am unaware of any such UNION DISTINCT syntax. If your intention is to do a union of the 3 tables and remove any duplicate records in the process, then just using UNION should suffice:
CREATE TABLE capstone-320521.Trips.Divvy_Trips_Raw_2020_2021 AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_04
UNION
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_05
UNION
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_06;
Note that in general it is bad practice to use SELECT * with union queries. The reason has to do with that a union between two (or more) tables is generally only valid if the two queries involved have the number and types of columns. Using SELECT * obfuscates what columns are actually being selected, and so it is preferable to always explicitly list out the columns.
I have had an issue with some DB2 SQL and could really use some help.
In short, people want me to create a view. I wrote a query that works and returns what we wanted... but when I wrap it with a 'CREATE VIEW' statement, the view throws errors when I query it with anything.
The query is (names changed obviously):
SELECT DISTINCT "QTable"."Add" "Q_Add", "QTable"."Approved" "Q_Approved", "QTable"."Link" "Q_Link"
FROM ((
SELECT * FROM db.schema.VTable)
"QTable" LEFT OUTER JOIN (SELECT * FROM db.schema.ETable)
"QStat" on "QTable"."Status" = "QStat"."ETable")
Each time I run this I get 10 record back. Awesome, that's what I want. When I wrap it as a View, which I do by entering:
CREATE VIEW TestSchema.TestTable AS
SELECT * FROM ( *query I just wrote above*)
It runs (very quickly) but then I run a basic SELECT * FROM viewname I always get the same error of:
SQL0206N "QTable.Status" is not valid in the context where it is used. SQLSTATE=42703
I have been stuck for a while now. I am assuming I am creating the view wrong. Any ideas? Tips?
Edit: DB2 11.1
I would get rid of just about ll the brackets and quotes. I suspect they are not necessary, unless DB2 is very non-standard sql. I've left the quotes in although I would take all of those out as well unless they are reserved words.
There is no reason to do something like from (select * from table)
The query should just be:
SELECT DISTINCT "QTable"."Add" "Q_Add", "QTable"."Approved" "Q_Approved", "QTable"."Link" "Q_Link"
FROM db.schema.VTable QTable
LEFT OUTER JOIN db.schema.ETable "QStat" on "QTable"."Status" = "QStat"."ETable"
Your view then should also not be create view as select * from (...) it should just be:
create view myView as
SELECT DISTINCT "QTable"."Add" "Q_Add", "QTable"."Approved" "Q_Approved", "QTable"."Link" "Q_Link"
FROM db.schema.VTable QTable
LEFT OUTER JOIN db.schema.ETable "QStat" on "QTable"."Status" = "QStat"."ETable"
You may have a different problem. I just tried it in DB2 10.5 and it works well:
create table vtable (
"Add" int,
"Approved" int,
"Link" varchar(20),
"Status" int
);
create table etable (
"ETable" int
);
create view my_view1 as
select
*
from
(
SELECT
DISTINCT "QTable"."Add" "Q_Add",
"QTable"."Approved" "Q_Approved",
"QTable"."Link" "Q_Link"
FROM
(
( SELECT * FROM VTable) "QTable"
LEFT OUTER JOIN
(
SELECT
*
FROM ETable
)
"QStat" on "QTable"."Status" = "QStat"."ETable"
)
)
Anyway, you have excessive parenthesis. Remove the ones you don't need.
And QTable need to be in double quotes always, as in "QTable".
I'm using Access 2003 (forced to do it due to retrocompatibility) to modify a 10 years old project, not made by me.
I encounter errors in executing this query:
INSERT INTO ClientiContratto ( ID, CLIENTE, DATA, PERIODO, IMPORTO, FATTURATO )
SELECT [Forms]![InserisciContratto]![Cliente] AS Espr1, (SELECT Nome from TAnagrafica WHERE TAnagrafica.IDAnagr = [Forms]![InserisciContratto]![Cliente]) AS Espr2, [Forms]![InserisciContratto]![Data] AS Espr3, [Forms]![InserisciContratto]![Periodo] AS Espr4, [Forms]![InserisciContratto]![Importo] AS Espr5, False AS Espr6;
That returns errors due to
(SELECT Nome from TAnagrafica WHERE TAnagrafica.IDAnagr = [Forms]![InserisciContratto]![Cliente]) AS Espr2
If I execute this query standalone, it works like a charm but when it comes to inserting the query into the INSERT INTO...SELECT statement, it returns (translated from italian):
Runtime error '3000': Reserved error (-3025): there are no messages
for this error.
The aim is to insert in a table some new values based on values found in the active form, and the part of code which isn't working should search into a table a value linked to the [InserisciContratto]![Cliente] actual value.
What am I doing wrong? Maybe is that because I cant execute a SELECT subquery in a previous SELECT query?
Any help would be appreciated.
You can work around the problem using a DLookUp instead of a subquery:
DLookUp("Nome", "TAnagrafica", "TAnagrafica.IDAnagr = [Forms]![InserisciContratto]![Cliente]")
Note that you can either use the DLookUp on a form control, or in a query. Both are valid. In the query, it'd look like this:
INSERT INTO ClientiContratto ( ID, CLIENTE, DATA, PERIODO, IMPORTO, FATTURATO )
SELECT [Forms]![InserisciContratto]![Cliente] AS Espr1, DLookUp("Nome", "TAnagrafica", "TAnagrafica.IDAnagr = [Forms]![InserisciContratto]![Cliente]") AS Espr2, [Forms]![InserisciContratto]![Data] AS Espr3, [Forms]![InserisciContratto]![Periodo] AS Espr4, [Forms]![InserisciContratto]![Importo] AS Espr5, False AS Espr6;
An alternate, common source of these kind of errors is that Access behaves finicky when using subqueries and not querying from a real table. You can easily work around that by using the subquery as the main query. Note that this does require the subquery to always return a result, else no row will be inserted:
INSERT INTO ClientiContratto ( ID, CLIENTE, DATA, PERIODO, IMPORTO, FATTURATO )
SELECT [Forms]![InserisciContratto]![Cliente] AS Espr1, Nome AS Espr2, [Forms]![InserisciContratto]![Data] AS Espr3, [Forms]![InserisciContratto]![Periodo] AS Espr4, [Forms]![InserisciContratto]![Importo] AS Espr5, False AS Espr6
FROM TAnagrafica
WHERE TAnagrafica.IDAnagr = [Forms]![InserisciContratto]![Cliente]
select * into #transacTbl from tmpTrans
insert
select
(case when tmpT.TranStatus = 10
then(
select ID, 'Returned')
else(
select ID, 'GoodSale')
end)
from
(
select * from MainTransacSource
) as tmpT
I want to be able to insert the details of a transaction into a different table with a label if it is a returned or good sale/transaction. I did this to avoid the cursor so please avoid giving a solution using a cursor.
I know the code looks good but what I'm experiencing is that, the case statement only returns one value via subquery.
This is a simplified version of the code; I have at least 6 types of cases and should be able to insert by ROW. I hate to think that I have to repeat each case per column because the actual number of columns is about 38.
You may suggest another work-around if this doesn't fit the logic. Of course, without a cursor.
Without access to your tables and not knowing more about what precisely you want to acheive, try something like this:
select * into #transacTbl from tmpTrans
insert
select tmpT.ID,
(case when tmpT.TranStatus = 10
then 'Returned'
else 'GoodSale'
end)
from
(select * from MainTransacSource) as tmpT <OR simply MainTransacSource tmpT (maybe)>
Cheers.
Debugging an app which queries SQL Server 05, can't change the query but need to optimise things.
Running all the selects seperately are quick <1sec, eg: select * from acscard, select id from employee... When joined together it takes 50 seconds.
Is it better to set uninteresting accesscardid fields to null or to '' when using EXISTS?
SELECT * FROM ACSCard
WHERE NOT EXISTS
( SELECT Id FROM Employee
WHERE Employee.AccessCardId = ACSCard.acs_card_number )
AND NOT EXISTS
( SELECT Id FROM Visit
WHERE Visit.AccessCardId = ACSCard.acs_card_number )
ORDER by acs_card_id
Do you have indexes on Employee.AccessCardId, Visit.AccessCardId, and ACSCard.acs_card_number?
The SELECT clause is not evaluated in an EXISTS clause. This:
WHERE EXISTS(SELECT 1/0
FROM EMPLOYEE)
...should raise an error for dividing by zero, but it won't. But you need to put something in the SELECT clause for it to be a valid query - it doesn't matter if it's NULL or a zero length string.
In SQL Server, NOT EXISTS (and NOT IN) are better than the LEFT JOIN/IS NULL approach if the columns being compared are not nullable (the values on either side can not be NULL). The columns compared should be indexed, if they aren't already.