Error in Querying Concatenation of 2 fields in LibreOffice Base SQL - sql

My apologies for wrong terminilogies, I am not actually a programmer, just a Base user confronted with a problem.
I'm having trouble querying a column which is supposed to be a concatenation of two fields of two separate tables. I'm using LibreOffice Base Version 1:3.6.2 and it has the default HSQL engine.
My two tables are as follows:
Table 1 is named "Prefectures" and has the following fields: ID, "Prefecture Name", and "State"
Table 2 is named "Ward" and has the following fields: ID, "Ward Name", and Prefecture ID which is a foreign key referencing table 1.
What I want my query to produce is these two colums: "Ward Name, Prefecture Name" as a concatenation of the Ward.WardName and Prefecture.PrefectureName, and the Ward ID
For example. If I had a prefecture named "Cebu" and a ward named "Lahug" which has a ward ID of 0, I want the query to come up with "Lahug, Cebu" on column 1, and "0" in column 2
The Base tutorial I used seems to have a modified language compared to the actual HSQL language, at least based on my programmer friends reaction. I know that attributes are referred to as Table.Attribute, for example, but in Base, they use "Attribute", or, if I am not mistaken, when one needs to specify the table of origin, "Table"("Attribute"). However, I know that this modified language works because I used it to create the my two tables.
Anyways, hazarding on what I learned from Base the tutorial document, I came up with:
SELECT "Ward Name" || ', ' || "Prefecture Name" AS "Wrd_Pref",
"Ward ID"
FROM "Prefecture" INNER JOIN "Ward" ON "Prefecture" ("Prefecture ID") = "Ward" ("Prefecture ID")
;
And the error message that came up was:
"The data content could not be loaded. Access is denied: PREFECTURE in statement [the whole code above]"
I have a suspicion this is a misread due to wrong syntax on my part. Perhaps my guess on using perentheses in this case is wrong? If so, why is the error message "Access denied"? I checked both the records and the sql codes of the two tables, both were perfectly normal.
EDIT: No, I don't want to just split the two fields. I need them concatenated as I am going to use them as list items in a dropdown list for a form I am making.

The SQL query is this one. You say your tables are called "Prefectures" and "Ward", which are used in the FROM clause.
SELECT "Ward Name" || ', ' || "Prefecture Name" AS "Wrd_Pref", "Ward ID"
FROM "Prefectures" INNER JOIN "Ward" ON "Prefectures"."Prefecture ID" = "Ward"."Prefecture ID";

Related

Snowflake tables with TO, FROM as column names

Looks like we've loaded some snowflake tables via ELT with "TO, FROM" as column names and they are both classic functions in any sql tool
Whenever I run a query for specifically those columns, there's always an error - how do I fix it apart from changing column names? Don't want to change column names as ELT process always happens from mongoDB via log based replication (stitch data)
select * - works perfectly , all other columns work too. Just "to" , "from" is the issue - should that never be used a columns?
select to, from from table limit 10 ; // tested [to, "to", 'to'] - none work
Error: SQL compilation error: error line 1 at position 7 invalid identifier '"to"'
Any ideas how to fix this apart from source column change or snowflake column changes?
Snowflake uses the standard double quotes to escape identifiers. However, when identifiers are escaped, the case of the letters matters, So, these are not the same:
select "to"
select "To"
select "TO"
You need to choose the one that is correct for your column names.
In addition spaces matter, so these are not the same:
select "to "
select " to"
select "to"
That is, what looks like to might be something else. You need to know what that is to escape the name properly.
If you can't figure them out, there is a trick to create a view to give the table reasonable names. Something like this:
create view v_t (to_date, from_date, . . .) as
select *
from t;
You need to be sure to include all the column names in the table in the column name list, in the same order as they are in the table. Then you can use the view with reasonable names.

AWS Athena create table from select query

I am trying to create table using the query below. If I do not create table, but just run the part from SELECT *, the query can be run.
(SELECT *
FROM "MyDatabase"."2007" A
WHERE A."column name a" NOT IN ('U','A+','A','A-')
AND A."column name b" NOT IN ('SHH','CTP')
AND NOT EXISTS
(SELECT *
FROM "MyDatabase"."2008" B
WHERE (B."column name a" = A."column name a"
AND B."column name b" = A."column name b"
AND B."column name c" = A."column name c")))
The error message is "GENERIC_INTERNAL_ERROR: field ended by ';': expected ';' but got 'partOfAColName' at line 1:..."
From google search, space in column names seems to be the problem. But I am not sure. I have space in column names. The column names are automatically detected by Glue Crawler. So I am not sure if I can do anything about it. I have around 20 columns, all having space in the middle though. Could someone suggest a fix? Thanks.
When you execute CREATE TABLE AS...you are telling Athena to create a table with the same column names in your SELECT, but in this case those column names contain spaces, and Athena won't allow you to create a column name with a space. To avoid this you can create the table with column names that adhere to the Athena's specifications then populate that table with INSERT INTO SELECT...FROM

MS Access query with parameter from combobox in form does not return any results

I have created an MS Access database with several tables and queries but the problem described further down is about the following:
tEmployee table contains employee data as the name implies.
tCases table contains data about court cases.
Each case can be assigned to only one employee. I have created a relationship from [tCases]![assignedTo] field to tEmployee.
fSearches is a form to perform searches among the cases. It includes a combobox cEmployee which is populated from [tEmployee].[surname] and a command button to perform the search.
GOAL: to select employee in [fSearches]![cEmployee], hit the button and show all the cases assigned to this employee in another form named fResultsCases.
This is the code for the button where lines in comments are some of the things I've tried:
If cEmploee.ListIndex = -1 Then
MsgBox "You have to select employee to perform search.", title:="Missing value!"
Else
tHidden2.SetFocus
tHidden2.Text = "assigned to " & cEmploee & "."
DoCmd.OpenForm "fResultsCases"
'Forms("fResultsCases").RecordSource = "SELECT * FROM tCases WHERE [tCases]![assignedTo] like [Forms]![fSearches]![cEmploee];"
'Forms("fResultsCases").RecordSource = "SELECT * FROM tCases WHERE tCases .[assignedTo] = [Forms]![fSearches]![cEmploee];"
Forms("fResultsCases").RecordSource = "SELECT * FROM tCases WHERE [assignedTo] = [Forms]![fSearches]![cEmploee];"
Forms("fResultsCases").Recalc
End If
With all the above mentioned I get the following:
[fResultsCases] opens but does not return any case.
I tried to omit the WHERE clause and [fResultsCases] returns all cases as expected.
Then I tried to narrow things down to understand the problem by creating a simple query with one parameter and got exactly the same results.
SELECT tCases.[case number], tCases.subject, tCases.fromDt, tCases.toDt,
tCases.assignedTo, tCases.[date assigned], tCases.[date completed]
FROM tCases
WHERE (((tCases.assignedTo)=[Forms]![fSearches]![cEmploee]));
It seems like I'm missing something about the WHERE clause when it comes to combobox values but I can't figure it out. I am new to MS Access. Any help will be very appreciated.
UPDATE:
cEmploee rowsource property: SELECT [tEmployee].surname FROM tEmployee ORDER BY [surname];
cEmploee bound column property: 1
I cannot recreate your issue replicating your setup with combox that triggers an update of form's recordsource. And because neither an error is raised nor parameter input prompt appears, likely the WHERE clause is returning False (i.e., no matching records) and hence no rows are returned.
The reason can likely be due to the combobox [Forms]![fSearches]![cEmploee] which show Employee's surname but is bound to the unique ID as the default setup of Access' comboboxes (if using wizard). See Data tab of combobox's properties in Design View.
The reason for this hidden bound field is users never know the unique ID of a record but do know the value (i.e., name). So when they select the recognized name they are really selecting the corresponding unique ID. Therefore, consider adjusting WHERE condition accordingly where ID is the unique table ID field which matches the bound field of combobox.
Forms("fResultsCases").RecordSource = "SELECT * FROM tCases WHERE [ID] = [Forms]![fSearches]![cEmploee];"
Forms("fResultsCases").Recalc
Forms("fResultsCases").Form.Requery ' USUALLY THE RECORDSOURCE UPDATE CALL
After about ten days of "struggle" with this brainteaser, I decided to post this question to ask for help. Fortunatelly #Parfait has involved with my problem and with his/her accurate remarks helped me understand my mistakes. It also helped me Ken Sheridan's post here. So I am posting my answer just for future reference. #Parfait, I sincerely thank you for your time.
So the problem in this case was primarily understanding how relationships between tables affect data types. If you want to connect a textfield of tableA to tableB, then there are two cases. If the primary key in tableB is number then textfield becomes number. If the primary key in tableB is text then textfield remains text.
In my case it was the first one. Specifically the field [tCases]![assignedTo] after creating relationship with table [tEmployee], was turned automatically into number (It still shows the surname but contains the corresponding primary key in tEmployee). On the contrary combobox [cEmploee] still contained text. This why this line:
Forms("fResultsCases").RecordSource = "SELECT * FROM tCases WHERE [assignedTo] = [Forms]![fSearches]![cEmploee];"
was not returning any record as #Parfait successfully guessed.
At this point there were two possible solutions:
1) Set surname as primary key to agree with combobox contents.
delete the relationship between [tCases]![assignedTo] and [tEmployee].
set field [tEmployee]![surname] (which is text) as primary key in table [tEmployee].
create a new relationship between [tCases]![assignedTo] and [tEmployee].
I did not try this solution though, as a problem would occur if 2 employees had the same surname. So I suggest following the second.
2) Keep the same primary key and use it to match the records from tableA to the surname from tableB.
To achieve this you have to inform access, that you want the records from tableA that have the ID, while in tableB this ID corresponds to the surname inserted in the combobox (hope this makes sense). In other words create a recordset of both tables (union query). For this purpose I replaced the above refered line of code with the following:
Forms("fResultsCases").RecordSource = "SELECT tCases.* FROM tCases INNER JOIN tEmployee ON tCases.[assignedTo] = tEmployee.[employeeID] " & _
"WHERE tEmployee.[surname]=[Forms]![fSearches]![cEmploee] AND tCases![assignedTo]=[tEmployee]![employeeID];"
Hope this saves some time from other people. Have a nice day!

Access Data Type Mismatch on my SQL Code

I have a query that pulls the phone extension and the full name of a person from my phone system import, and splits the full name into a First and Last name. It is working great for spliting the name up but if I try to sort by either name I get a Data Tyep Mismatch Error.
Here is the SQL Code.
SELECT ExportG3R.Extension,
Left([ExportG3R.name],InStr([ExportG3R.name],",")-1) AS LastName,
Trim(Mid([ExportG3R.name],InStr([ExportG3R.name],",")+1,Len([ExportG3R.name])-InStr([ExportG3R.name],","))) AS FirstName
FROM ExportG3R
ORDER BY ExportG3R.Extension;
Any ideas on how to get this working?
I created a ExportG3R table with text fields for Extension and name. Your query worked with name values like "Litzner, Mike". However, a name value which doesn't include a comma, such as "Litzner Mike", gave me #Error for LastName and "Litzner Mike" for FirstName. And if name is Null, it gives me #Error for both. Neither of those was a deal-breaker for SELECT until I tried to sort on LastName and/or FirstName. When attempting those sorts, the Access 2003 db engine responded "Invalid procedure call", which is not the same error message you're getting.
So, although I'm not certain my situation exactly matches yours, I'll suggest you try your query with a WHERE clause to return only rows which have non-Null name values and also include a comma.
SELECT
e.Extension,
Left(e.[name],InStr(e.[name],",")-1) AS LastName,
Trim(Mid(e.[name],InStr(e.[name],",")+1,Len(e.[name])-InStr(e.[name],","))) AS FirstName
FROM ExportG3R AS e
WHERE e.name Like "*,*"
ORDER BY ORDER BY 2,3;
Another concern is that name is a reserved word. When creating that table in design view with Access 2007, gave me a warning about that field name. Try creating a throw-away table in Design View and read the help it offers when it gives you a warning about name. If at all possible, change it to something which isn't a reserved word ... FullName perhaps.
Finally I think this would be simpler if the name field were split into two fields in the table itself. Storing names as "Last, First" combines 2 attributes in one field. So when you need either of them, you must split them out. It's easier to store them separately, then concatenate them whenever you need them as "Last, First":
SELECT LastName & ", " & FirstName AS FullName
you could wrap it in a sub query;
SELECT extension,lastname,firstname
FROM (
SELECT ExportG3R.Extension,
Left([ExportG3R.name],InStr([ExportG3R.name],",")-1) AS LastName,
Trim(Mid([ExportG3R.name],InStr([ExportG3R.name],",")+1,
Len([ExportG3R.name])-InStr([ExportG3R.name],","))) AS FirstName
FROM ExportG3R
) order by firstname

error with a sql query because of ambiguous column name

I'm trying to create a sql query, but there is this error:
Ambiguous column name 'description'.
Its because this column occurs in both tables.
if I remove the description from the query, it works.
I tried to rename the description-field "AS description_pointer", but the error still occurs.
SELECT TOP 1000 [activityid]
,[activitytypecodename]
,[subject]
,[regardingobjectid]
,[contactid]
,[new_crmid]
,[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as I
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact]
ON I.[regardingobjectid] = [crmtestext_MSCRM].[dbo].[FilteredContact].[contactid]
WHERE new_crmid not like '%Null%' AND activitytypecodename like '%E-mail%'
Both tables coming into play in the query have a column named description. You RDBMS cannot guess which column table you actually want.
You need to prefix the column name with the table name (or table alias) to disambiguate it.
Bottom line, it is a good practice to always prefix column names with table names or aliases as soon as several tables come into play in a query. This avoids the issue that you are seeing here and make the queries easier to understand for the poor souls that have no knowledge of the underlying schema.
Here is an updated version of your query with table aliases and column prefixes. Obviously you need to review each column to put the correct alias:
SELECT TOP 1000
i.[activityid]
,i.[activitytypecodename]
,i.[subject]
,c.[regardingobjectid]
,c.[contactid]
,c.[new_crmid]
,c.[description] AS description_pointer
FROM [crmtestext_MSCRM].[dbo].[FilteredActivityPointer] as i
Left JOIN [crmtestext_MSCRM].[dbo].[FilteredContact] as c
ON i.[regardingobjectid] = c.[contactid]
WHERE i.new_crmid not like '%Null%' AND i.activitytypecodename like '%E-mail%'