Compare page items with string in Oracle APEX SQL - sql

SELECT 'Alle' AS l, 'SAS' AS r FROM DUAL
UNION ALL
--Montage B
SELECT DISTINCT KPI_VIEW_ID_MONAT.OE AS l, KPI_VIEW_ID_MONAT.OE AS r
FROM KPI_VIEW_ID_MONAT
WHERE KPI_VIEW_ID_MONAT.OE LIKE
(SELECT CASE :P46_GEWERKE WHEN 'Montage B' THEN 'N/PN-MA21%'
ELSE 'N%'
END
FROM DUAL
)
I wander what am i doing wrong with comparing the page item "P46_GEWERKE"(Which contains static names) with string 'Montage B'. MY program seems to skip the CASE statement or consider it false all the time and jumps to ELSE statement and executes it.
I am trying to make the program read the first list (GEWERKE) and give it a code according to the string it finds in the list, then pick all the values from the table "KPI_VIEW_ID_MONAT" having the same code and show it in a list called (OE)snapshot of the two lists

Related

Trouble filtering out plural words

I have a table with the most frequent words in the English language which looks like this:
word count
cat 43534889
dog 34584357
hat 4343878
...
hats 44747
I'd like to exclude all the plural words like 'hats' if they already exist in singular form.
So I wrote this query
SELECT
word,
CASE WHEN CONCAT(word,'s') IN (
SELECT freq.word from `words.freq` as freq
WHERE freq.word LIKE '%s' AND LENGTH(freq.word) > 4
)
THEN 'plural'
ELSE 'sing'
END AS plural
FROM `words.freq` LIMIT 1000
My logic is: if the word 'hat' + 's' is found among words ending in 's' (subquery), it means it's just the plural form of that noun. Somehow the function CONCAT doesn't seem just to add 's' to each word, but it changes it so for example when I run this query, words like 'that' are somehow displayed as 'plural' as if they were longer than 4 characters and contained 's' at the end. I am really confused. Can anyone help?
This (in MySQL syntax) should do what you're looking for: as you say, this doesn't capture all the ways that English can make plurals, and it will also get some false positives ("hiss" would be considered as plural because "his" exists).
The idea is to look for words of >=4 characters ending in 's', and check whether the corresponding word with the final 's' removed exists:
SELECT word,
CASE
WHEN CHAR_LENGTH(word) >= 4 AND word LIKE '%s' AND LEFT(word, CHAR_LENGTH(word)-1) IN (SELECT word FROM words) THEN 'plural'
ELSE 'singular'
END AS plurality
FROM words;
I thinks you can sort words in alphabetical descending order and compare a word with next word to check if it's singular form of it.
WITH sample_table AS (
SELECT 'cat' word, 43534889 count UNION ALL
SELECT 'dog', 34584357 UNION ALL
SELECT 'hat', 4343878 UNION ALL
SELECT 'dogs', 38738 UNION ALL
SELECT 'hats', 44747
)
SELECT *,
IF(CONCAT(LEAD(word) OVER (ORDER BY word DESC), 's') = word, 'plural', 'singular') is_plural
FROM sample_table;

Read multiple columns from single table based on our input string

I want to select multiple columns from single table based on my given input string in sql select statement.
Example :
If input="table_medical" i want to select the columns like medi_col1,medi_col2,medi_col2
If input="table_pharmacy" i want to select the columns like medi_phar1,medi_phar2,medi_phar1
sql("select
case when $input="table_medical" then medi_col1) //like this
please help me to complete this.
If you want this in a single query, then the same number of columns is needed -- and have compatible types.
One method uses union all:
select medi_col1, medi_col2, medi_col2
from t
where #input = 'table_medical'
union all
select medi_phar1, medi_phar2, medi_phar1
from t
where #input = 'table_pharmacy';
SET #input="table_medical";
SELECT
CASE WHEN #input="table_medical" THEN medi_col1 ELSE medi_phar1 END as medi_col1,
CASE WHEN #input="table_medical" THEN medi_col2 ELSE medi_phar2 END as medi_col2
CASE WHEN #input="table_medical" THEN medi_col3 ELSE medi_phar3 END as col3
FROM MyTable
Data types for medi_col1 and medi_phar1 needs to be same (and for the rest of columns too)

How to display the whole number only if it starts with two characters otherwise leave it blank. SQL query

I need to display the whole number in a field if it starts with "AB" otherwise do not show/display the number.
Your question is missing code of how you display this (since you wrote you need to display it to the field) so i can't answer you with actual code but here is solution.
If you want to select only rows which column1 starts with AB then use LIKE function. So condition at selecting command is Select * from yourtable where column1 LIKE 'AB%'
If you already selected and displayed data, let's say in datagridview, and you want to fill textbox with string that contains AB then you would go through all rows at specific column and look for it with string.Contains("AB");
So basically you put this command in foreach loop and you have it.
I was wrong. You can use a LIKE, just not in the WHERE clause.
;WITH testdata AS (
SELECT 'aw12354' AS val UNION ALL
SELECT 'a12b344' UNION ALL
SELECT 'AB11111' UNION ALL
SELECT '11AB111' UNION ALL
SELECT '11111AB' UNION ALL
SELECT 'ab22222'
)
SELECT
CASE WHEN val LIKE 'AB%' THEN val ELSE NULL END AS valFull
, CASE WHEN val LIKE 'AB%' THEN SUBSTRING(val,3,len(val)) ELSE NULL END AS valNums
FROM testdata
;
You can also use CLR to build a regex solution, but that is a LOT more involved.

Is using sequence in db2 statements possible?

I am trying to execute following statement in DB2.
This works well.
SELECT NEXT VALUE FOR SCPYMNT.REM_QUERY_NO_SEQ
FROM sysibm.sysdummy1
However, this throws a database error.
SELECT (
CASE WHEN PYMT_SYS = 1 THEN NEXT VALUE FOR SCPYMNT.REM_QUERY_NO_SEQ
WHEN PYMT_SYS = 2 THEN 'dummy'
else 'dummy'
END )
FROM sysibm.sysdummy1
So Db2 gives the error below.
Category Timestamp Message
Statusbar 18.04.2016 11:47:39 DB2 Database Error: ERROR [428F9] [IBM][DB2] SQL0348N "NEXT VALUE FOR SCPYMNT.REM_QUERY_NO_SEQ" cannot be specified in this context. SQLSTATE=428F9
It seems to me there is not a syntax error.Does Db2 not let such queries that consists of case conditions and sequence?
#MichaelTiefenbacher,I put select examples as a demonstration.(What I am really trying to achieve is something like below.
SELECT NAME, QUERYNO
FROM FINAL TABLE (INSERT INTO EMPSAMP (NAME, SALARY, QUERYNO)
VALUES('Mary Smith', 35000.00,
CASE WHEN PYMT_SYS = 1 THEN NEXT VALUE FOR REM_SEQ
CASE WHEN PYMT_SYS = 2 NEXT VALUE FOR EFT_SEQ
));
I think question is more clearer now.
Sequences can be used to generate unique keys or numbers when inserting data into tables.
They are not used to generate unique numbers when selecting data.
For that you could either retrieve the field from the table where you used the sequence at insert time or you can use row_number() in the SELECT.
It also would be helpful to tell a little more what you want to achieve.
I found out that the answer is "No" according to IBM documentation.
NEXT VALUE expressions cannot be specified in the following contexts:
CASE expression
Here is the link
Insert from a union of selects:
SELECT NAME, QUERYNO
FROM FINAL TABLE
(
INSERT INTO EMPSAMP
SELECT
'Mary Smith', 35000.00, NEXT VALUE FOR REM_SEQ
FROM SYSIBM.SYSDUMMY1
WHERE PYMT_SYS = 1
UNION ALL
'Mary Smith', 35000.00, NEXT VALUE FOR EFT_SEQ
FROM SYSIBM.SYSDUMMY1
WHERE PYMT_SYS = 2
)

SSMS - every SELECT statement creates extra empty line - how to suppress?

If I set in SQL Management Studio to produce an output to Text (not to grid or to file) and run following:
SET NOCOUNT ON
SELECT '1'
SELECT '2'
SELECT '1'
I get this:
1
2
1
How to remove extra new line after every line?
I set as default in Tools->Options following:
1. Output format as TAB delimited
2. Excluded column headers in the result set
3. Created a New Query
UPDATE:
Previous query is shown for sake of simplicity. Please take this one as a question:
SELECT 1+2
SELECT 'A FOX'
SELECT 1,2,3
Want to see no extra line between lines, e.g:
3
A FOX
1 2 3
You are getting three separate result sets - one for each select.
In order to get a single result set (with duplicates) you need to UNION ALL the selects:
SET NOCOUNT ON
SELECT '1' UNION ALL
SELECT '2' UNION ALL
SELECT '1'
See UNION (Transact-SQL) on MSDN for details.
Update:
What you want to do is not possible. The types and numbers of the fields are different in each select - you can't UNION them.
You can't have a single result set that has different number of columns per row or with different data types per row.
Well, instead of posting comment, I decided to post answer, since it may help someone.
The output of the stored procedure I need to paste in Excel and apply "copy format" from template.
I resolved this problem this way:
Initial statement I have
SELECT 'Report Header'
SELECT ''
SELECT 'Line of report',1
SELECT 'Line of report',23
SELECT * FROM SUBREPORT
SELECT 'SUBTOTAL:',(select count(*) from another_table)
SELECT ''
SELECT 'REPORT GENERATED AT ', GetDate()
I updated to this:
SELECT 'Report Header'
SELECT '[NEWLINE]'
SELECT 'Line of report',1
SELECT 'Line of report',23
SELECT * FROM SUBREPORT
SELECT 'SUBTOTAL:',(select count(*) from another_table)
SELECT '[NEWLINE]'
SELECT 'REPORT GENERATED AT ', GetDate()
Since extra empty lines breaks formatting, I have to remove them.
I took the text output and pasted in Word.
Replaced "^p^p" with "^p", and then "[NEWLINE]" with "" (nothing).
Copy-Paste to Excel and copy format from template.
Done.
I would appreciate if someone can suggest more elegant solution.