Can I use delete clause in Case Clause in teradata - sql

I am newbie to teradata.
I need to delete a row once the case condition is satisfied.
Eg: case condition true delete the selected row.

Maybe I am misinterpreting what you are trying to accomplish with the CASE statement, but based on my understanding you can use the WHERE clause to conditionally remove data from a table:
DELETE
FROM MyDB.MyTable
WHERE Col1 = 31
AND "Desc" = 'xxxxxx';
EDIT:
Based on your comment then you need to apply the CASE logic to each column returned in the SELECT statement that you wish to obscure.
SELECT CASE WHEN Col1 = 31 and "DESC" = 'yyyyy'
THEN NULL
ELSE ColA
END AS ColA_,
/* Repeat for each column you wish to "delete" */
FROM MyDB.MyTable;

Related

Displaying an alternative result when derrived table is empty

I have this sql code where I try to display an alternative value as a result whenever the table is empty or the the single column of the top row when it is not
select top 1 case when count(*)!=0 then derrivedTable.primarykey
else 0 end endCase
from
(
select top 1 m.primarykey
from mytable m
where 0=1
)derrivedTable
The problem is that when I run this, I get the error message "column 'derrivedTable.primarykey' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
But when I put 'derrivedTable.primarykey' in the group by clause, I just get an empty table.
Does anyone hve a solution?
thanks in advance
You can use aggregation:
select coalesce(max(m.primarykey), 0)
from mytable m;
An aggregation query with no group by always returns exactly one row. If the table is empty (or all rows are filtered out), then the aggregation functions -- except for COUNT() -- return NULL -- which can be transformed to a value using COALESCE().
Such a construct makes me worry. If you are using this to set the primary key on an insert, then you should learn about identity columns or sequences. The database will do the work for you.
Can you try this below script-
SELECT
CASE
WHEN COUNT(*) = 1 THEN derrivedTable.primarykey
ELSE 0
END endCase
FROM
(
SELECT TOP 1 m.primarykey
FROM mytable m
WHERE 0 = 1
) derrivedTable
derrivedTable.primarykey;

How to execute two queries based on case condition in sql server

I am trying to execute queries based on condition within single table,here is my query in sql server.
where PATTERN is column name and is set to 0 as default value
I want to display result into only one cell of excel sheet. I linked SQL server and Excel sheet.
IF ([PATTERN] = 1)
BEGIN
SELECT PATTERN,COLOR,SHIFT FROM [DEFECT_RESULTS]
END
ELSE
SELECT MODEL,COLOR FROM [DEFECT_RESULTS]
pattern column is present then still following error is displaying
error: Invalid column name 'PATTERN'.
You need to perform a query in the if condition. SQL doesn't know what table/function you're referring to as it stands.
Something like
if exists (select 1 from DEFECT_RESULTS where PATTERN = 1)
begin
…
Which will do the true side of the if if any row in DEFECT_RESULTS matches. You may need a more specific condition.
declare #pattern int
if (#pattern=1)
begin
SELECT PATTERN,COLOR,SHIFT FROM [DEFECT_RESULTS]
end
else if(#pattern=0)
begin
SELECT MODEL,COLOR FROM [DEFECT_RESULTS]
end
A single query cannot return 2 columns sometimes and 3 columns at other times. Perhaps you just want two queries:
SELECT PATTERN, COLOR, SHIFT
FROM [DEFECT_RESULTS]
WHERE PATTERN = 1;
SELECT MODEL, COLOR
FROM [DEFECT_RESULTS]
WHERE PATTERN = 0;

SQL Select Case Statement With Where Condition In Nested Table

I need to insert a SELECT CASE statement to add a column "acct_profile_ext.acct_pay_terms", to a nested condition table. So the only table that can link with this table is "acct_profile", which is "acct_profile.acct_id = acct_profile_ext.acct_id". Problem is this table "acct_profile" is heavily linked to other table as well, so I need to add this column without effect the result of other table.
So now I've insert the statement at the column part, but I'm not sure how to add the column with WHERE condition inside the SELECT CASE statement.
(SELECT CASE acct_profile_ext.acct_pay_terms
WHEN 'CASH' THEN
(SELECT acct_profile_ext.acct_pay_terms FROM acct_profile_ext, acct_profile
WHERE acct_profile.acct_id = acct_profile_ext.acct_id)
WHEN 'CHEQUE' THEN
(SELECT acct_profile_ext.acct_pay_terms FROM acct_profile_ext, acct_profile
WHERE acct_profile.acct_id = acct_profile_ext.acct_id)
ELSE NULL
END
FROM acct_profile_ext)
Please advise and help. Thanks.

Update columns in same table

I want to update BRCD_NEW column in table branches with the condition applied on another column BRCD in the same table, here is my code, but it returns error
single-row subquery returns more than one row
update branches set brcd_new=(
select
case
when BRCD between '5000' and '5999' then CONCAT('PK001',BRCD)
else CONCAT('PK002',BRCD)
end
from branches);
You don't need the subquery to achieve what you are doing. Use CASE statement to get the value you need and assign in to your column in SET statement:
update branches
set brcd_new =
case
when BRCD between '5000' and '5999' then CONCAT('PK001',BRCD)
else CONCAT('PK002',BRCD)
end
-- WHERE <your filters (if needed)>

Select all tuples of a Table, with a dummy column showing which tuples satisfy a condition

If I add a where condition in query it gives me filtered results. But what i need is a result in which all the rows (filtered + unfiltered) are shown with an additional (dummy) column which tells me by some means(say a boolean value) that a specific row met the "where condition" .
Taking the example where condition:
where col1 = 2
Use a case to add an additional column indicating if the condition is met:
select *, case when col1 = 2
then 'true'
else 'false'
end as `dummy`
from your_table