I would like to write SQL which deletes records based on a few conditions.
I have four records
In these records, ID is unique and can change. Name is used to delete the records.
1) A Name should have at least one OK type.
2) IF Name has an ID that doesn't contain DUMMY in its name, then delete all other records where ID contains DUMMY.
How can I do this in SQL?
Try this:
delete from tablename t
where t."id" like '%DUMMY%'
and t."name" = (
select "name" from tablename
where "name" = '?'
group by "name"
having sum(case when "type" = 'OK' then 1 else 0 end) > 0
and sum(case when "id" not like '%DUMMY%' then 1 else 0 end) > 0
)
The subquery after the IN clause returns the names that have at least 1 type = 'OK' and at least 1 id not containing 'DUMMY'.
Edit:
delete from tablename t
where
t."name" = '?'
and (
(t."id" like '%DUMMY%' and t."type" <> 'OK')
or (
t."type" = 'OK'
and exists (
select 1 from tablename
where "name" = t."name" and "type" = t."type" and "id" > t."id"
)
)
)
Related
I want to run a multi-column correlated update of this kind:
UPDATE t1 t1_alias
SET (table_name, tablespace_name) = (
SELECT table_name, tablespace_name
FROM t2 t2_alias
WHERE t1_alias.table_name = t2_alias.table_name
);
But my attempt:
update customer up
set (customer_name, account, active) = (
select tmp.name, tmp.account, case when tmp.active = 'Yes' then 1 else 0 end
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
);
... throws:
ORA-01407: cannot update ("FOO"."CUSTOMER"."CUSTOMER_NAME") to NULL
The source table customer_temp has no null values so I must be getting matches wrong. What is my error or misconception?
Presumably, there are some rows in the target table that have no match in the subquery.
You can avoid this with by adding an exists condition that filters out "unmatched" rows:
update customer up
set (customer_name, account, active) = (
select tmp.name, tmp.account, case when tmp.active = 'Yes' then 1 else 0 end
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
)
where exists (
select 1
from customer_temp tmp
where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
);
I have an Oracle table and I would like to create a check condition like this :
ALTER TABLE MyTable
ADD CONSTRAINT MyTable_CHK2 CHECK (
case Dimension
When 1 then
nvl(dimensiontype1,-1)<>-1
when 2 then
nvl(dimensiontype1,-1)<>-1 and nvl(dimensiontype2,-1)<>-1
when 3 then
nvl(dimensiontype1,-1)<>-1 and nvl(dimensiontype2,-1)<>-1 and nvl(dimensiontype3,-1)<>-1
else
true
end
)
disable
The query is not working. I'm having the error : Missing Keyword.
Anyone know how to solve that please ?
Thanks.
You probably want an AND / OR expression
ALTER TABLE MyTable
ADD CONSTRAINT MyTable_CHK2 CHECK
(
( Dimension = 1 and nvl(dimensiontype1,-1) <> - 1 ) OR
( Dimension = 2 and (nvl(dimensiontype1,-1) <> - 1 and nvl(dimensiontype2,-1)<> -1 ) ) OR
( Dimension = 3 and (nvl(dimensiontype1,-1) <> -1 and nvl(dimensiontype2,-1)<> -1 and nvl(dimensiontype3,-1) <> -1))
) disable ;
Check constraint should be:
(dimension=1 and dimensiontype1 is not null)
or (dimension=2 and dimensiontype1 is not null and dimensiontype2 is not null)
or (dimension=3 and dimensiontyp1 is not null and dimensiontype2 is not null and dimensionType 3 is not null)
I'm trying to check if a specific ID exists in a table named "Products" in my sqlite database.
def existsCheck( db, id )
temp = db.execute( "select exists(
select 1
from Products
where promoID = ?
) ", [id] )
end
that's my current code but that returns an array which means I have to deal with conversion before I can use it as a boolean. Any ideas how I can change it so that it returns an int with the value 1?
There is no need to use a subquery:
def existsCheck( db, id )
db.execute( "select 1
from Products
where promoID = ?",
[id] ).length > 0
end
This returns a boolean result.
Change it to:
def existsCheck( db, id )
temp = db.execute( "select 1 where exists(
select 1
from Products
where promoID = ?
) ", [id] ).any?
end
SQL query returns 1 when condition is met or empty result set if it's not. Whole function returns boolean depending on result set.
Is it possible to create a conditional index on two columns?
CREATE UNIQUE INDEX idx_dup_wfc
ON WF_WORKFLOW_CLASS (CASE WHEN is_active = 1
THEN (NAME, DEPT_OWNER)
ELSE NULL
END)
returns ORA-00906: missing left parenthesis however the following works
CREATE UNIQUE INDEX idx_dup_wfc
ON WF_WORKFLOW_CLASS (CASE WHEN is_active = 1
THEN NAME
ELSE NULL
END)
Yes, but you have to perform a CASE per column:
CREATE UNIQUE INDEX idx_dup_wfc
ON WF_WORKFLOW_CLASS
(CASE WHEN is_active = 1 THEN NAME END
,CASE WHEN is_active = 1 THEN DEPT_OWNER END
)
(The ELSE in your code is superfluous).
I'm trying to check if a specific ID exists in a table named "Products" in my sqlite database.
def existsCheck( db, id )
temp = db.execute( "select exists(
select 1
from Products
where promoID = ?
) ", [id] )
end
that's my current code but that returns an array which means I have to deal with conversion before I can use it as a boolean. Any ideas how I can change it so that it returns an int with the value 1?
There is no need to use a subquery:
def existsCheck( db, id )
db.execute( "select 1
from Products
where promoID = ?",
[id] ).length > 0
end
This returns a boolean result.
Change it to:
def existsCheck( db, id )
temp = db.execute( "select 1 where exists(
select 1
from Products
where promoID = ?
) ", [id] ).any?
end
SQL query returns 1 when condition is met or empty result set if it's not. Whole function returns boolean depending on result set.