Teradata CASE WHEN attribute IN ( Select... from) - teradatasql

I have selected a bunch of IDs and I try to use CASE WHEN to get a column when the IDs which I really want to select are in the IDs I selected earlier.
But it shows Select Failed .3771:Illegal expression in WHEN clause of Case expression.
Any suggestion to get it right?
Please help.
p.s my goal is to select the IDs shown for the first time.

Related

Do not fail on missing column in a SQLLite query

I have a simple query like this:
SELECT * FROM CUSTOMERS WHERE CUSTID LIKE '~' AND BANKNO LIKE '~'
The problem is, the customers-table might or might not contain the BANKNO column depending on circumstances I've no control over. If however BANKNO is not a column in CUSTOMERS, this query fails.
So my question is: it is possible to test if the BANKNO column exists and if so, to include it in the query and if not to exclude this column?
The query really has to be flexible.
A non-existent column in a SELECT to sqlite3 will always fail.
One option might be to put the "full" sql in a try block, and if it errors, execute the other sql.
Or, you could query PRAGMA table_info('CUSTOMERS') and interrogate the result to see if a column in question is in the database. Find the sqlite doc here https://www.sqlite.org/pragma.html#pragma_table_info.
I'm sure there are other options, but the bottom line is you need to know before the sql is executed that it contains only valid column names.

Can I loop over the results of an SQL query to use each value in another query all at once in SQL Developer?

What I want to achieve (if it is possible from SQL Developer) is that when I execute the script it do the following:
Run a SELECT statement that will return a list of IDs. Approx 270 records.
I need to use each of those IDs individually in another SELECT statement (in the WHERE clause) which will return some records. A few of this could result in over 17,000 records and some can be one.
Then each result from the second SELECT I want it to be exported to an excel or csv file into a folder at my pc.
I have both 'Select' ready but I don't know how to loop over the results of the first to grab each ID and use it in the second one. Also I don't know how to export automatically from the code.
You can use GROUP BY clause.
read more here:
http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj32654.html
If the first SELECT returns you IDs only, and that's all you need for your second SELECT, just use IN clause providing your first SELECT query for the second one.
Example:
-- Your second select
SELECT
col1
,col2
,col3
,col4
FROM
second_table
WHERE
some_foreign_id IN (
-- Your first select
SELECT
id
FROM
first_table
WHERE
some_conditions...
)
In my opinion don't use PLSQL for this. In PLSQL the only way you can get an output of this by using a REFCURSOR (unless you dont use UTIL File package to do it). A simple SELECT WITH JOIN condition will suffice your requirement.
Test illustration
SELECT A.* FROM TABLE_A A, TABLE_B
WHERE A.COMMON_COLUMN = B.COMMON_COLUMN;
Hope this helped you in same way

SQL to find last sibling in a linked list

I've got a simple table that has a previous field for each row so as to create a linked list. I am trying to find a query that will find the id of the last element in the list. My first thought was that perhaps I could use some sort of self join, but I'm not sure how to express that what I am looking for is the id of the element which does not appear as the "previous" field in some other row.
My second thought was perhaps a query that uses "not in" and the list of all ids (e.g. SELECT id) but I'm not sure how to attach that a particular field (e.g. previous). In other words, I would want something like:
SELECT * FROM test WHERE (SELECT id) NOT IN previous
But the SQL code is expecting previous to be a table, whereas I want to find where non of the ids are in the previous value.
If I try the reverse, for some reason it matches all rows:
SELECT * FROM test WHERE previous NOT IN (SELECT id)
As for the second thought:
SELECT id FROM foo f1 WHERE id NOT IN (SELECT previous FROM foo f2)

Was it mandatory to put a "distinct" field as the first field in a query?

Just out of curiosity, looks like a distinct field must be placed ahead of any other fields, am I wrong?
See this example in SQLite,
sqlite> select ip, distinct code from parser; # syntax error?
Error: near "distinct": syntax error
sqlite> select distinct code, ip from parser; # works
Why is that? Do I really have a syntax error?
There is no such thing as a "distinct field".
distinct applies to all fields in the query and therefore must appear immediately after select.
In other words, select distinct code, ip is really
select distinct
code,
ip
rather than
select
distinct code,
ip
It selects all distinct pairs of (code, ip). Thus the result set could include repeated values of code (each with a different value of ip).
It is not possible to apply distinct to a single field in the way you're trying to (group by might be a useful alternative, but we need to understand what it is exactly that you're trying to achieve).

ERROR:-528 MEssage: [Informix .NET provider][Informix]Maximum output rowsize (32767) exceeded

I face the following exception when i try to get data from table with the following structure:
ERROR:-528 MEssage: [Informix .NET provider][Informix]Maximum output
rowsize (32767) exceeded.
CREATE TABLE dr66req
(
req_ser SERIAL PRIMARY KEY,
req_desc LVarChar(32739),
);
Ref:
The total number of bytes that this statement selects exceeds the
maximum that can be passed between the database server and the program.
Try following-
1) Make sure that the columns selected are the ones that you intended.
2) Check that you have not named some very wide character column by
mistake, neglected to specify a substring, or specified too long a
substring. If the selection is what you require, rewrite this SELECT
statement into two or more statements, each of which selects only some
of the fields.
3) If it is a join of several tables, you might best select
all desired data INTO TEMP; then select individual columns of the
temporary table.
4)If this is a fetch via a cursor in a program, you
might revise the program as follows.
First, change the cursor to select only the ROWID of the desired row.
Second, augment the FETCH statement with a series of SELECT statements, each of which selects one or a few columns WHERE ROWID = the saved row ID.

Resources