How to build a select query to specify the record which is already existed in other table - oracle11g

I want to specify that a phone number is already existed in other table or not. If it is existed then it must show me 'yes' otherwise show me 'no'. For exanple:
Select Name, PHONE_NO, (Select 'yes' from cdr_personal_info c , tec_personal_info t where c.phone_no=t.phone_no) CDR_existence from TEC_PERSONAL_INFO;
The above query display 'yes' with all columns of TEC_PERSONAL_INFO table it should only display 'yes' with one column which is existed in CDR_PERSONAL_INFO table.
Note: these two tables does not have any kinds of relationship with each other.

You could use an exists condition around your subquery - though you don't need to refer to the driving table again - within a case expression:
select t.name,
t.phone_no,
case when exists (
select null from cdr_personal_info c where c.phone_no=t.phone_no
) then 'yes' else 'no' end as cdr_existence
from tec_personal_info t
Or you could use an outer join, but you might have to deal with duplicates if the cdrpersonal_info table can have the same phone number more than once:
select t.name,
t.phone_no,
case when c.phone_no is null then 'no' else 'yes' end as cdr_existence
from tec_personal_info t
left join cdr_personal_info c on c.phone_no=t.phone_no
db<>fiddle demo with some made-up data.

Related

How to concatenate a Select query inside a INSTR() in SQLite?

I was trying to order a result set by the order of the values in an IN() clause.
SELECT * FROM CrossReference WHERE cross_reference_id IN (SELECT Id FROM FilteredIds)
So I tried to find a function such as MySql FIELD(). Then I found these answers (answer1, answer2) which explain how to do the exact thing on SQLite using the INSTR().
SELECT *, INSTR(',GDBR10,GDBR5,GDBR30,', ',' || ticker || ',') POS
FROM tbl
WHERE POS>0
ORDER BY POS;
So it's working as expected, but I want to populate the ids dynamically using a select query. I tried many approaches, but nothing seemed to work. Here is the last one I tried. It gave me just one result row (a result related to the first filterId).
SELECT *, INSTR (','||(SELECT id FROM FilteredIds)||',', ',' || cross_reference_id || ',') POS FROM CrossReference WHERE POS>0 ORDER BY POS;
So I guess I'm making some kind of mistake when concatenating the SELECT query with the rest of the code. Because when I manually enter the filtered Ids it works and returns results according to the entered filter ids.

How can I get values values from a table and also not in that table in SQLite?

I have two tables: Player(name, email) and TeamPlayer(PlayerName, Team, Active). Tables Player and TeamPlayer are connected via Player.name = TeamPlayer.PlayerName.
I want to get all the elements of TeamPlayer, but also the elements in Player not in TeamPlayer. The attribute Active is important also. I execute the next command, but it did not work:
SELECT DISTINCT Player.Name, TeamPlayer.Team, TeamPlayer.Active FROM Player LEFT JOIN TeamPlayer ON TeamPlayer.PlayerName=Player.Name ORDER BY TeamPlayer.Active;
Is it possible?
You can use UNION ALL for the table TeamPlayer and the rows from Player that don't exist in TeamPlayer:
SELECT PlayerName AS Name, Team, null AS email, Active
FROM TeamPlayer
UNION ALL
SELECT t.Name, null, t.email, 0
FROM Player t
WHERE NOT EXISTS (
SELECT 1 FROM TeamPlayer
WHERE PlayerName = t.Name
)
ORDER BY Active
Since the 2 tables don't have the same columns, the non existing columns for each case will be NULL or 0 for the column Active.
You can change it as you wish.

Oracle Sql LISTAGG with CASE

I am trying to add a LISTAGG function to the below query so that the output can be a single value, separated by commas.Can someone advise how to add the LISTAGG function to it.
SELECT CASE A.ATTRIBUTE_NAME
WHEN 'EA' THEN 'EMAIL ADDRESS'
WHEN 'CP' THEN 'Company'
WHEN 'OG' THEN 'Organization'
END AS USER_DETAILS
FROM USERS A LEFT OUTER JOIN DETAILS B
ON A.ID = B.USER_ID
WHERE USER_NAME LIKE '%John%'
From your description you simply want to put the case expression inside the listagg() call:
SELECT LISTAGG(
CASE A.ATTRIBUTE_NAME
WHEN 'EA' THEN 'EMAIL ADDRESS'
WHEN 'CP' THEN 'Company'
WHEN 'OG' THEN 'Organization'
END,
',') WITHIN GROUP (ORDER BY A.ATTRIBUTE_NAME) AS USER_DETAILS
FROM USERS A LEFT OUTER JOIN DETAILS B
ON A.ID = B.USER_ID
WHERE USER_NAME LIKE '%John%'
Although it looks like it should be referring to B.ATTRIBUTE_NAME; perhaps you got that wrong modifying the code for posting. And you might want to order the items in the list differently.

Update row with value from next row sqlite

I have the following columns in a SQLite DB.
id,ts,origin,product,bid,ask,nextts
1,2016-10-18 20:20:54.733,SourceA,Dow,1.09812,1.0982,
2,2016-10-18 20:20:55.093,SourceB,Oil,7010.5,7011.5,
3,2016-10-18 20:20:55.149,SourceA,Dow,18159.0,18161.0,
How can I populate the 'next timestamp' column (nextts) with the next timestamp for the same product (ts), from the same source? I've been trying the following, but I can't seem to put a subquery in an UPDATE statement.
UPDATE TEST a SET nextts = (select ts
from TEST b
where b.id> a.id and a.origin = b.origin and a.product = b.product
order by id asc limit 1);
If I call this, I can display it, but I haven't found a way of updating the value yet.
select a.*,
(select ts
from TEST b
where b.id> a.id and a.origin = b.origin and a.product = b.product
order by id asc limit 1) as nextts
from TEST a
order by origin, a.id;
The problem is that you're using table alias for table in UPDATE statement, which is not allowed. You can skip alias from there and use unaliased (but table-name prefixed) reference to its columns (while keeping aliased references for the SELECT), like this:
UPDATE TEST
SET nextts = (
SELECT b.ts
FROM TEST b
WHERE b.id > TEST.id AND
TEST.origin = b.origin AND
TEST.product = b.product
ORDER BY b.id ASC
LIMIT 1
);
Prefixing unaliased column references with the table name is necessary for SQLite to identify that you're referencing to unaliased table. Otherwise the id column whould be understood as the id from the closest[*] possible data source, in which case it's the aliased table (as b alias), while we're interested in the unaliased table, therefore we need to explicitly tell SQLite that.
[*] Closest data source is the one listed in the same query, or parent query, or parent's parent query, etc. SQLite is looking for the first data source (going from inner part to the outside) in the query hierarchy that defines this column.

Case when no record then error (postgresql)

Struggling with a simple problem and find no solution.
https://ideone.com/Hisu8H (sqlfiddle mini) Should be two rows, but only one OK appears.
If there is a record there should be an OK result. If no records there should be ERROR result.
SELECT
CASE WHEN (SELECT count(*) from code)=0 THEN 'ERROR' else 'OK' end
FROM code
WHERE "CODE_ID"='EXISTS'
The problem is that if the CODE_ID exists, there will be a line OK but if the record is missing there will be no line fetched at all.
In a simple query this works. But not as a condition!
SELECT COALESCE(count(*), 0) FROM code
WHERE "CODE_ID"='ERROR'
Thanks in advance for any clue!
If there is no row that matches the condition then nothing is returned and you can't count it.
To return information about values that are used in a condition but do not exist in the database you typically use an outer join on the list of values:
select t.id,
code.code_id
from (
values ('error'), ('test')
) as t (id)
left join code on code.code_id = t.id;
This essentially a where code_id in ('error', 'test') query that also returns those values from the in list that do not exist in the table.
This can then be used to identify the missing values:
select t.id,
case when code.code_id is null then 'Error' else 'OK' end as status
from (
values ('error'), ('test')
) as t (id)
left join code on code.code_id = t.id;

Resources