Why this oracle query returning empty result set? - sql

I have two database tables with some demo data like shown below
Create table demo(uuid int, addressname varchar(50));
insert into demo values(1, 'intersportprofi');
insert into demo values(2, 'intersportprofi');
insert into demo values(3, 'intersportprofi');
insert into demo values(4, 'intersportmarket');
insert into demo values(5, 'intersportmarket');
insert into demo values(6, 'intersportmarket');
create table demo_av(uuid int, testid int, name varchar(50), value varchar(50));
insert into demo_av values(1, 1, 'sport','football basketball cricket');
insert into demo_av values(2, 1, 'brand','reebok addidas nike');
insert into demo_av values(3, 2, 'sport','football basketball ');
insert into demo_av values(4, 2, 'brand','reebok addidas ');
I wrote the following query to get the results from those tables, but oracle returning empty result set.
SELECT d.addressname FROM demo d, demo_av dv
WHERE d.uuid = dv.testid AND d.addressname='intersportprofi'
AND REGEXP_LIKE( dv.value, 'reebok') AND REGEXP_LIKE( dv.value, 'cricket')
Why? where i am doing wrong ? Any help will be greatly appriciated

Change this:
AND REGEXP_LIKE( dv.value, 'reebok') AND REGEXP_LIKE( dv.value, 'cricket')
To this:
AND (REGEXP_LIKE( dv.value, 'reebok') OR REGEXP_LIKE( dv.value, 'cricket'))
Because:
You have no record in the "demo_av" table that matches with "reebok" AND "cricket". The operator you need is "OR" and the parantheses are necessary because of existing of the first condition.
UPDATE
Here is the capture screen of the results:
Cheers

Based on your comments, I think you want a query that will search over multiple rows with same testid. This can be done with joins or like this:
SELECT DISTINCT d.addressname
FROM demo AS d
WHERE d.addressname = 'intersportprofi'
AND EXISTS
( SELECT *
FROM demo_av AS dv
WHERE d.uuid = dv.testid
AND dv.value LIKE '%reebok%'
)
AND EXISTS
( SELECT *
FROM demo_av AS dv
WHERE d.uuid = dv.testid
AND dv.value LIKE '%cricket%'
) ;

Related

INSERT fails with ORA-01400: cannot insert NULL

I am working with ORACLE SQL Developer and I created a table with an id as PK, another FK, id_columnx and a column1 and inserted data into them. Then I added another 2 columns, column 2 and column 3 and when I try to insert data into these new added columns, I get the error:
ORA-01400: cannot insert NULL.
I have to mention that I don't have any triggers on the table and i DO have values in the INSERT statement. There seems to be a conflict with the PK id, but I don't understand why.
So here is the code:
create table mytable(id INT PRIMARY KEY, name varchar2(30));
insert into mytable values (1, 'Mary');
insert into mytable values (2, 'John');
insert into mytable values (3, 'Bill');
alter table mytable
add email VARCHAR2(30);
alter table mytable
add addess VARCHAR2(30);
insert into mytable (email, addess)
values ('mary#gmail.com', 'Street X');
And here is the error I get:
Error starting at line : 12 in command -
insert into mytable (email, addess)
values ('mary#gmail.com', 'Street X')
Error report -
ORA-01400: cannot insert NULL into ("ZAMFIRESCUA_49"."MYTABLE"."ID")
INSERT is for inserting new rows, UPDATE is for altering data in the current rows. As mentioned in the comments, it looks like you want to be updating Mary's row with her email/address:
UPDATE mytable
SET email = 'mary#gmail.com',
address = 'Street X'
WHERE ID = 1 --Mary's ID for example, replace with the ID of the row you want to update
You could also use a subquery to find the right ID so you don't have to always look it up:
UPDATE mytable
SET email = 'mary#gmail.com',
address = 'Street X'
WHERE ID = (SELECT ID FROM mytable WHERE name = 'Mary')
Edit:
I was thinking there were two tables while writing this answer, you could always just use the name field as your filter:
UPDATE mytable
SET email = 'mary#gmail.com',
address = 'Street X'
WHERE name = 'Mary'
You're missing a PK value in your last INSERT after the ALTER statements. Try this:
create table mytable(id INT PRIMARY KEY, name varchar2(30));
insert into mytable values (1, 'Mary');
insert into mytable values (2, 'John');
insert into mytable values (3, 'Bill');
alter table mytable add email VARCHAR2(30);
alter table mytable add addess VARCHAR2(30);
insert into mytable (id, email, addess) values (4, 'mary#gmail.com', 'Street X');

How to insert conditionally in Oracle?

I've read here that the syntax looks like this:
INSERT
WHEN ([Condition]) THEN
INTO [TableName] ([ColumnName])
VALUES ([VALUES])
ELSE
INTO [TableName] ([ColumnName])
VALUES ([VALUES])
SELECT [ColumnName] FROM [TableName];
But I don't want to provide values from another table. I just want to type them, so I've got:
INSERT
WHEN EXISTS (SELECT 1 FROM FOO WHERE NAME = 'JOE')
THEN
INTO BAR (NAME, AGE)
VALUES ('JOE', 50)
and this produces exception: ORA-00928: missing SELECT keyword.
I want to perform an insert if given value is found in another table.
Using with select works. Your query wasn't working because there is a problem with values keyword when inserting conditionally.
INSERT
WHEN EXISTS (SELECT 1 FROM FOO WHERE NAME = 'JOE')
THEN
INTO BAR (NAME, AGE)
SELECT 'JOE', 50 FROM DUAL
So, I've found an indirect way here and solution for my question would be:
INSERT INTO BAR (NAME, AGE)
SELECT 'JOE', 50
FROM DUAL
WHERE EXISTS (SELECT 1 FROM FOO WHERE NAME = 'JOE')
but it doesn't explain why I have to use SELECT statement in INSERT WHEN

Inserting data into Oracle table (SQL)

I already have a table built in oracle.
Im trying to insert some data like this:
INSERT INTO movies_actor('name','id')
VALUES ('Nuno','2'), ('Pedro','3'), ('Jose','1');
select * from movies_actor;
I always get this error
ORA-00928: missing SELECT keyword
What am I doing wrong?
I don't think you need the single quote around your field names.
You need to do:
INSERT INTO TableName(Column1, Column2)
VALUES('Nuno', '2');
In your example, it would be:
INSERT INTO movies_actor(name, id)
VALUES ('Nuno','2');
INSERT INTO movies_actor(name, id)
VALUES ('Pedro','3');
INSERT INTO movies_actor(name, id)
VALUES ('Jose','1');
select * from movies_actor;
Another way.
insert into table
(field1, field2)
select value1, value2
from dual
union
select value3, value4
from dual
etc
You cannot insert multiple records in one statement using VALUES. You can either use Tenzin's solution or use INSERT ALL :
INSERT ALL
INTO movies_actor(name, id) VALUES ('Nuno', '2')
INTO movies_actor(name, id) VALUES ('Pedro', '3')
INTO movies_actor(name, id) VALUES ('Jose', '1')
SELECT * FROM dual;

Show row number column from result set in oracle sql view

I want to add an extra column so if I get, let's say, 4 rows in the result this column will have values 1,2,3,4.
I've tried ROWNUM, but since this is a view it shows the actual row number in the whole view, and that's not what I want.
Here is a sample schema:
CREATE TABLE TEST (RID NUMBER, RVAL VARCHAR2(100 BYTE));
INSERT INTO TEST (RID, RVAL) VALUES (1, 'ONE');
INSERT INTO TEST (RID, RVAL) VALUES (2, 'TWO');
INSERT INTO TEST (RID, RVAL) VALUES (3, 'THREE');
INSERT INTO TEST (RID, RVAL) VALUES (4, 'FOUR');
CREATE OR REPLACE VIEW VTEST AS
SELECT ROWNUM AS NUMROW, RID, RVAL FROM TEST;
Here are two sample queries. The first shows the result I want. The second how I want to get it (with a simple select against the view)
SELECT ROWNUM,RID,RVAL FROM TEST WHERE RID = 3 OR RID = 4;
SELECT * FROM VTEST WHERE RID = 3 OR RID = 4;
Here is the fiddle: http://sqlfiddle.com/#!4/4e816/3
in oracle use ROW_NUMBER oracle docs

Query to display output horizontally

I need to display a query output in a horizontal manner. I have some example data
create table TestTable (id number, name varchar2(10))
insert into TestTable values (1, 'John')
insert into TestTable values (2, 'Mckensy')
insert into TestTable values (3, 'Valneech')
insert into TestTable values (4, 'Zeebra')
commit
select * from TestTable
This gets the output in a vertical view.
ID Name
==========
1 John
2 Mckensy
3 Valneech
4 Zeebra
However, I need to display it horizontally.
ID 1 2 3 4
Name John Mckensy Valneech Zeebra
How can one do this?
To pivot, you should use the pivot clause of the select statement:
select *
from testtable
pivot ( max(name)
for id in (1,2,3,4)
)
This is not particularly pretty to do in SQL, so you should consider carefully whether this is what you want to do. I normally use Oracle Base for pivoting examples but there are many out there.
Here's a little SQL Fiddle to demonstrate.
Maybe it will help you:
select 'id', LISTAGG(id, ' ') WITHIN GROUP (ORDER BY name)
from testtable
union
select 'name', LISTAGG(name, ' ') WITHIN GROUP (ORDER BY name)
from testtable
EDIT:
or with pivot:
create table TestTable2 (id varchar2(30), name varchar2(10));
insert into TestTable2 values ('id', 'name');
insert into TestTable2
select cast(id as varchar2(30)) as id , name
from testtable
select *
from testtable2
pivot ( max(name)
for id in ('id',1,2,3,4)
)
PIVOT operator is what you are looking for.