SQL Full_Text Setup and Query - sql

I've been struggling for a while trying to get FULL-TEXT INDEX and CONTAINS/FREETEXT query working on a MySQL database. I attempted to set it up and test it using phpMyAdmin.
I created a new database (FULL_TEXT_DB) new table (FULLTEXT_TABLE) with 2 columns (AI & TEXT_COL). AI is type 'INT', index is 'PRIMARY' with the auto increment box checked. TEXT_COL is type 'VARCHAR' with the index as 'FULLTEXT' and Index Name is 'id'
I made 2 test rows with text = 'red apple raw' and 'red apple organic'
INSERT INTO `fulltext_table` (`ai`, `text_col`) VALUES ('1', 'red apple raw'), ('2', 'red apple organic');
I attempted to test it with CONTAINS and FREETEXT.
SELECT * FROM fulltext_table WHERE CONTAINS(text_col, 'red apple raw');
SELECT * FROM testing_fulltext WHERE FREETEXT(text_col, 'red');
CONTAINS produced no results and FREETEXT returned an error (Error #1305 "FUNCTION FULL_TEXT_DB.FREETEXT does not exist)
Any help would be greatly appreciated. Perhaps my problem is using phpMyAdmin, but unfortunately that's all I've ever used to create tables and then run them using web PHP code. I am a SQL novice. Thanks!

Related

How to use unnest array with dynamic Postgresql query across two tables

I am relatively new to SQL and having some difficulty understanding how to run a conditional query. For some context, this query is being run with Node/Express using Postgresql.
Situation
If a user selects 'Team A' the query (in the backend) will take the data from one table, while if the user selects 'Team B' it will run the query against a different table. To render the table correctly I need to transpose the data and understand I can achieve this with UNNEST(array[]).
I can run the query successfully with the following code:
SELECT "TeamA" AS "Player",
UNNEST(array['Type1', 'Type2', 'Type2']) AS "Type",
UNNEST(array["Column1", "Column2", "Column3"]) AS "Type2",
UNNEST(array["Column4", "Column5", "Column6"]) AS "Type3",
UNNEST(array["Column7", "Column8", "Column9"]) AS "Type4",
UNNEST(array["Column10", "Column11", "Column12"]) AS "Type5"
FROM sportingdb."TeamAData"
WHERE "TeamA" = '${player}'
However this needs to be dynamic and query a different table if the user selects 'Team A' or 'Team B'.
I have attempted to use the same query with a ternary, however was given a syntax error and am unable to identify where I have gone wrong. I also broke it down as an if else statement as follows:
DO
BEGIN
IF '${TeamA}' = "TeamA" THEN
SELECT "TeamA" AS "Player",
UNNEST(array['Type1', 'Type2', 'Type2']) AS "Type",
UNNEST(array["Column1", "Column2", "Column3"]) AS "Type2",
UNNEST(array["Column4", "Column5", "Column6"]) AS "Type3",
UNNEST(array["Column7", "Column8", "Column9"]) AS "Type4",
UNNEST(array["Column10", "Column11", "Column12"]) AS "Type5"
FROM sportingdb."TeamAData"
WHERE "TeamA" = '${player}'
ELSE
SELECT "TeamB" AS "Player",
UNNEST(array['Type1', 'Type2', 'Type2']) AS "Type",
UNNEST(array["Column1", "Column2", "Column3"]) AS "Type2",
UNNEST(array["Column4", "Column5", "Column6"]) AS "Type3",
UNNEST(array["Column7", "Column8", "Column9"]) AS "Type4",
UNNEST(array["Column10", "Column11", "Column12"]) AS "Type5"
FROM sportingdb."TeamBData"
WHERE "TeamB" = '${player}'
END IF
END
I found this link was somewhat helpful, which I followed to write the above code but arrived with an SQL error [42601]: ERROR: syntax error at or near "BEGIN". I also tried adding the dollar signs as per the example as well as a DECLARE line but this didn't seem to work.
I would truly appreciate any assistance on how I can get this to work. Thanking you in advance!

Convert SQL to REGEX to Data Studio

I am new to working in Google Data Studio. I am trying to do a case when on some data but all I have is the SQL.
Here are the examples of what I am working with. I know this is not the best way to create this example. I normally use work with making queries so these examples are new to me.
CREATE TABLE #botdata(botstatus varchar(255))
insert into #botdata (botstatus)
values ('0|3900000:3900037:3900999:BOT-ANOMALY-HEADER|undefined|false')
insert into #botdata (botstatus)
values ('1|3900037|undefined|false')
insert into #botdata (botstatus)
values ('0|3900005:3900024|undefined|')
I am making the above into groups in SQL using a query but I need this same query to be formatted in Google Data Studio and I am not sure how
select case when botstatus like '1|%' OR (botstatus like '0|%' and botstatus like '%BOT-%') then 'BOT'
when botstatus like '0|%' then 'Regular Visit' else 'Regular Visit NO BOT STATUS' end as updatedbotstatus, botstatus
from #botdata
Having issues since the '|' and '-' are special charters in data studio.
Tried this but not getting the results I need.
CASE
WHEN REGEXP_MATCH( Bot Status(11) ,'0\\|*') and REGEXP_MATCH(Bot Status(11),'*BOT\\-*') then 'Bot'
ELSE 'Other'
END
Please test it out and lemme know if it needs any revision.
CASE WHEN REGEXP_CONTAINS(regex,'1\\|') OR (REGEXP_CONTAINS(regex,'0\\|') AND REGEXP_CONTAINS(regex,'BOT-')) THEN 'BOT' WHEN REGEXP_CONTAINS(regex,'0\\|') THEN 'REGULAR VISIT' END
-

Regex comparison in Oracle between 2 varchar columns (from different tables)

I am trying to find a way to capture relevant errors from oracle alertlog. I have one table (ORA_BLACKLIST) with column values as below (these are the values which I want to ignore from
V$DIAG_ALERT_EXT)
Below are sample data in ORA_BLACKLIST table. This table can grow based on additional error to ignore from alertlog.
ORA-07445%[kkqctdrvJPPD
ORA-07445%[kxsPurgeCursor
ORA-01013%
ORA-27037%
ORA-01110
ORA-2154
V$DIAG_ALERT_EXT contains a MESSAGE_TEXT column which contains sample text like below.
ORA-01013: user requested cancel of current operation
ORA-07445: exception encountered: core dump [kxtogboh()+22] [SIGSEGV] [ADDR:0x87] [PC:0x12292A56]
ORA-07445: exception encountered: core dump [java_util_HashMap__get()] [SIGSEGV]
ORA-00600: internal error code arguments: [qercoRopRowsets:anumrows]
I want to write a query something like below to ignore the black listed errors and only capture relevant info like below.
select
dae.instance_id,
dae.container_name,
err_count,
dae.message_level
from
ORA_BLACKLIST ob,
V$DIAG_ALERT_EXT dae
where
group by .....;
Can someone suggest a way or sample code to achieve it?
I should have provided the exact contents of blacklist table. It currently contains some regex (perl) and I want to convert it to oracle like regex and compare with v$diag_alert_ext message_text column. Below are sample perl regex in my blacklist table.
ORA-0(,|$| )
ORA-48913
ORA-00060
ORA-609(,|$| )
ORA-65011
ORA-65020 ORA-31(,|$| )
ORA-7452 ORA-959(,|$| )
ORA-3136(,|)|$| )
ORA-07445.[kkqctdrvJPPD
ORA-07445.[kxsPurgeCursor –
Your blacklist table looks like like patterns, not regular expressions.
You can write a query like this:
select dae.* -- or whatever columns you want
from V$DIAG_ALERT_EXT dae
where not exists (select 1
from ORA_BLACKLIST ob
where dae.message_text like ob.<column name>
);
This will not have particularly good performance if the tables are large.

Error: "Column name or number of supplied values does not match table definition" Despite column match

I was hoping someone could point me in the right direction for troubleshooting.
I work for a small company and we are building a new application. I have the insert statement below (with selected static values for testing) that was working until this morning when we started getting error messages from it (we changed nothing about this table).
INSERT INTO t_el_powertrain
(
[pt_id]
,[el_id]
,[segment_id]
,[continuous_hp]
,[intermittent_hp]
,[torque]
,[torque_rpm]
,[eop]
,[checked_out]
,[create_date]
,[created_by]
,[update_date]
,[updated_by]
,[captive]
)
SELECT 1, --pt_id
100000, --el_id
5 , --segment_id
5.00, --continuous_hp
5.00, --intermittent_hp
5.00, --torque
5, --torque_rpm
5, --eop
0, --checked_out
GetDate(), --create_date
5 , --created_by
GetDate(), --update_date
5, --updated_by
10
Error message:
Here is a screenshot of the table structure (I refreshed the connection right before)
It seems pretty clear to me that there should be no issues with the insert statement below. Does anyone have any idea why I might be getting an error here?
Thanks for the help!
EDIT: added picture of the error message
UPDATE:
Thanks to a quick response from Martin Smith, I realized that I totally overlooked what DB object was throwing the error.
I forgot to update our history mirror table when I added a column a while back.
Solved. Thanks again.

Derby DB Insert Error SQL state 21000

I am attempting to insert data into a table in my database. I am using an Oracle Apache Derby DB. I have the following code-
Insert into P2K_DBA.ODS_CNTRL
(ODS_LOAD_ID, ODS_STATUS, USR_WWID, USR_FIRST_NM,
USR_LAST_NM, USR_DISPLAY_NM, USR_NT_ID,TOT_AMT,
TOT_RCD_CNT, TOT_QTY, LAST_UPD_DT, ODS_ADJ_TYP,
ODS_ADJ_DESC, APRV_WWID, APRV_FIRST_NM,APRV_LAST_NM,
APRV_DISPLAY_NM, APRV_NT_ID, APRV_DT
)
values
(6,'avail','64300339', 'Travis',
'Taylor', 'TT', '3339', 33,
15, 40, '7/10/2012', 'test',
'test', '64300337', 'Travis',
'Taylor', 'TT', '3339', '2/06/2013');
I ran this SQL command and received the following error-
"Error code -1, SQL state 21000: Scalar subquery is only allowed to return a single row.
Line 1, column 1"
I have ran this code successfully a few days ago. On top of that I have tried to manually enter in data in this table (using NetBeans) and have it auto generate the code, which resulted in the same error.
What is causing this error and how can I solve/bypass it?
One way in which you could run into this would be to do something like
CREATE FUNCTION F(...) ...
F((SELECT COL FROM T))
But you could instead write
... (SELECT F(COL) FROM T) provided the new context permits a subquery, that is.