MSACESS - JOIN expression not supported error - sql

When I run this on Microsoft Access:
SELECT
'BANNER',
USRAUDT_KEY_VALUE_1,
UCRACCT_PREM_CODE,
CONCAT(CONCAT(USRAUDT_KEY_VALUE_1,'_'),UCRACCT_PREM_CODE) AS "ACCOUNT",
USRAUDT_USER_ID,
' ',
' ',
USRAUDT_ACTIVITY_DATE,
' ',
'bill cycle',
' ',
UCRACCT_STATUS_IND,
USRAUDT_NEW_VALUE,
' ',
UCRACCT_BILL_PRES_TYPE
FROM
USRAUDT
INNER JOIN UCRACCT ON (USRAUDT_KEY_VALUE_1) = UCRACCT_CUST_CODE
WHERE 1=1
AND USRAUDT_NEW_VALUE In ('E','R')
and USRAUDT_USER_ID <> 'UEADMIN'
and USRAUDT_COLUMN_NAME='UCBCUST_PAPERLESS_IND'
and (USRAUDT_ACTIVITY_DATE >= '27-JAN-2020')
and (USRAUDT_ACTIVITY_DATE <= '03-FEB-2020')
and ((USRAUDT_TABLE_NAME)='UCBCUST')
AND UCRACCT_STATUS_IND='A'
and UCRACCT_BILL_PRES_TYPE in ('E','L','U','V')
I get the error "Join expression NOT supported", I've tried using AND but still no avail. Help please.. :(

No two SQL dialects are exactly the same including MS Access SQL and others from various DBMS's. Specifically, your attempted query includes several issues:
Join: ON clauses in MS Access requires table or table alias qualifiers with period notations. Generally in SQL, the best practice is to qualify every column reference in SELECT, ON, WHERE, GROUP BY and other clauses whenever you join more than one table. This facilitates code maintainability and readability.
FROM USRAUDT d
INNER JOIN UCRACCT c
ON d.USRAUDT_KEY_VALUE_1 = d.UCRACCT_CUST_CODE
Functions: There is no built-in CONCAT function. Use & instead or build your own via VBA function (accessible through queries only in MS Access GUI and not backend via ODBC/OLEDB): d.USRAUDT_KEY_VALUE_1 & '_' & c.UCRACCT_PREM_CODE
Quotes: Double even single quotes used in column aliases will render such symbols (", ') directly in query column headers. Similarly, if you use quotes for table aliases, you will then need to reference table with such symbols. For example, SELECT "d".* FROM USRAUDT "d" or SELECT 'd'.* FROM USRAUDT 'd' works but fails if you use no quotes: SELECT d.* .... To escape special characters or spaces in MS Access, use square brackets or backticks. Column and table names are case insensitive in Access regardless of symbols.
Aliases: Any columns without column names or aliases will render as Expr#### where number follows 1000 + column integer (zero-index). To avoid this result, provide actual column aliases even for constants like '_' unless SELECT is used in insert-select query which does not matter. Again, generally in SQL, the best practice is to always provide unique column aliases as some other engines will raise exceptions.
Dates: Dates in MS Access cannot be compared to its string representation but need to be casted to dates with CDate or enclosed with # and must adhere to representative forms of dates such as 'MM/DD/YYYY' or 'DD/MM/YYYY'. Usually, the best format though is ISO system date format: 'YYYY-MM-DD':
and (d.USRAUDT_ACTIVITY_DATE >= #2020-01-27#)
and (d.USRAUDT_ACTIVITY_DATE <= CDate('02/03/2020')

Related

WHERE clause returning no results

I have the following query:
SELECT *
FROM public."Matches"
WHERE 'Matches.Id' = '24e81894-2f1e-4654-bf50-b75e584ed3eb'
I'm certain there is an existing match with this Id (tried it on other Ids as well), but it returns 0 rows. I'm new to querying with PgAdmin so it's probably just a simple error, but I've read the docs up and down and can't seem to find why this is returning nothing.
Single quotes are only used for strings in SQL. So 'Matches.Id' is a string constant and obviously not the same as '24e81894-2f1e-4654-bf50-b75e584ed3eb' thus the WHERE condition is always false (it's like writing where 1 = 0)
You need to use double quotes for identifiers, the same way you did in the FROM clause.
WHERE "Matches"."Id" = ...
In general the use of quoted identifiers is strongly discouraged.

SELECT JOIN +Column

I have the following query :
SELECT A.*, B.* FROM Employee AS A
LEFT JOIN EmployeeHistory AS B ON +B.EmployeeId = CASE
WHEN DeptId=1 THEN SUBSTRING(FunctionRef,2,3)
WHEN DeptId=2 THEN SUBSTRING(FunctionRef,2,2) END
I want to understand the + before the +B.EmployeeId, since SQL Server isn't throwing an error
The SQL you have posted is not valid T-SQL. This is because after you define the alias for Employee as A you have an ON ("A" is for Employee? What A? A is for Apple, E is for Employee. Bad Habits to Kick : Using table aliases like (a, b, c) or (t1, t2, t3)). The first table in a FROM can't be followed by an ON, ON is for tables that you are joining to.
If the first ON is meant to be a JOIN, it does work: db<>fiddle. But all the + is doing is being a leading plus or concatenation operator (which depends on the data type of EmployeeID). SELECT +1, + ''; is perfectly valid (though odd) syntax. The + is basically doing nothing.
Disclaimer: The opening paragraph is based on the original SQL the OP posted, which they stated they had copy and pasted from a working environment.
The + is a unary plus sign. It is analogous to then unary minus (-) but it doesn't do anything. If the second argument is a string, then it is a unary string concatenator, once again doing nothing.
I'm not sure what the purpose is.
You can check this out to get an idea:
select ++++1, + '', +'abc'
Maybe you forgot to include the second line JOIN table2 as B. If you included that line the query could become valid.
The +B.EmployeeId expression can mean 0+B.EmployeeId and thus the plus sign would not have any effect.

Find duplicates in case-sensitive query in MS Access

I have a table containing Japanese text, in which I believe that there are some duplicate rows. I want to write a SELECT query that returns all duplicate rows. So I tried running the following query based on an answer from this site (I wasn't able to relocate the source):
SELECT [KeywordID], [Keyword]
FROM Keyword
WHERE [Keyword] IN (SELECT [Keyword]
FROM [Keyword] GROUP BY [Keyword] HAVING COUNT(*) > 1);
The problem is that Access' equality operator treats the two Japanese writing systems - hiragana and katakana - as the same thing, where they should be treated as distinct. Both writing systems have the same phonetic value, although the written characters used to represent the sound are different - e.g. あ (hiragana) and ア (katakana) both represent the sound 'a'.
When I run the above query, however, both of these characters will appear, as according to Access, they're the same character and therefore a duplicate. Essentially it's a case-insensitive search where I need a case-sensitive one.
I got around this issue when doing a simple SELECT to find a Keyword using StrComp to perform a binary comparison, because this method correctly treats hiragana and katakana as distinct. I don't know how I can adapt the query above to use StrComp, though, because it's not directly evaluating one string against another as in the linked question.
Basically what I'm asking is: how can I do a query that will return all duplicates in a table, case-sensitive?
You can use exists instead:
SELECT [KeywordID], [Keyword]
FROM Keyword as k
WHERE EXISTS (SELECT 1
FROM Keyword as k2
WHERE STRCOMP(k2.Keyword, k.KeyWord, 0) = 0 AND
k.KeywordID <> k2.KeywordID
);
Try with a self join:
SELECT k1.[KeywordID], k1.[Keyword], k2.[KeywordID], k2.[Keyword]
FROM Keyword AS k1 INNER JOIN Keyword AS k2
ON k1.[KeywordID] < k2.[KeywordID] AND STRCOMP(k1.[Keyword], k2.[Keyword], 0) = 0

SQL query ignores "not between"

I have a data source like following
If I ran the following sql query it removes all the records with "Seg Type" MOD and ignores the Fnn range given.
select * from NpsoQueue
where SegmentType not in ('MOD')
and Fnn not between 0888452158 and 0888452158
I want the query to consider both conditions. So, if I ran the query it should remove only the first record
The logic in your where clause is incorrect
Use
select * from NpsoQueue
where NOT (
SegmentType = 'MOD'
and Fnn between '0888452158' and '0888452158'
)
Also, a number with a leading zero is a string literal so you need to put single quotes around it to preserve the leading zero and stop implicit casts happening
As mentioned by #TriV you could also use OR. These are fundamental boolean logic concepts, i.e. not related to SQL Server or databases

Pentaho Dynamic SQL queries

I have a Pentaho CDE project in development and i wanted to display a chart wich depends on several parameters (like month, year, precise date, country, etc). But when i want to "add" another parameter to my query, it doesn't work anymore... So i'm sure i'm doing something wrong but what ? Please take a look for the parameter month for example :
Select_months_query : (this is for my checkbox)
SELECT
"All" AS MONTH(TransactionDate)
UNION
SELECT DISTINCT MONTH(TransactionDate) FROM order ORDER BY MONTH(TransactionDate);
Select_barchart_query : (this is for my chart, don't mind the other tables)
SELECT pginit.Family, SUM(order.AmountEUR) AS SALES
FROM pginit INNER JOIN statg ON pginit.PG = statg.PGInit INNER JOIN order ON statg.StatGroup = order.StatGroup
WHERE (MONTH(order.TransactionDate) IN (${month}) OR "All" IN (${month}) OR ${month} IS NULL) AND
/*/* Apply the same pattern for another parameter (like year for example) *\*\
GROUP BY pginit.Family
ORDER BY SALES;
(Here, ${month} is a parameter in CDE)
Any ideas on how to do it ?
I read something there that said to use CASE clauses... But how ?
http://forums.pentaho.com/showthread.php?136969-Parametrized-SQL-clause-in-CDE&highlight=dynamic
Thank you for your help !
Try simplifying that query until it runs and returns something and work from there.
Here are some things I would look into as possible causes:
I think you need single quotes around ${parameter} expressions if they're strings;
"All" should probably be 'All' (single quotes instead of double quotes);
Avoid multi-line comments. I don't think you can have multi-line comments in CDE SQL queries, although -- for single line comments usually works.
Be careful with multi-valued parameters; they are passed as arrays, which CDA will convert into comma separated lists. Try with a single valued parameter, using = instead of IN.