Completing the where clause in sql server - sql

I have a table kyc3 where there are walletno, status and rank columns present. rank columns are currently filled with 0. While status column has following data: accepted, rejected, registered and scanned. I want to put value for each status in rank column where
accepted = 1, rejected = 2, registered = 3 and scanned = 4
I wrote following query but do not understand how to complete it:
INSERT INTO kyc3([rank]) SELECT status_ FROM kyc3
I understand I need to put a where clause that will indicate my logic for data population. But what should I write?

If the table is populated and you want to change the rank field, you want to use an UPDATE statement, as INSERT is for adding new rows to your table:
UPDATE kyc3
SET rank = CASE WHEN status = 'accepted' THEN 1
WHEN status = 'rejected' THEN 2
WHEN status = 'registered' THEN 3
WHEN status = 'scanned' THEN 4
END

You can use update to fill a cell of an existing row.
update kyc3
set rank = CASE WHEN status = 'accepted' THEN 1
WHEN status = 'rejected' THEN 2
WHEN status = 'registered' THEN 3
WHEN status = 'scanned' THEN 4
END
Use insert only for creating new rows.

Related

Oracle SQL/PLSQL: Return Boolean value when multiple criterion met across many rows of data

I have a query that currently returns multiple rows of data. I need to analyse this data using PL/SQL and/or SQL to determine whether it meets the business requirements - returning either TRUE or FALSE.
Sample Data 1 Sample Data 2 Sample Data 3
TYPE STATUS TYPE STATUS TYPE STATUS
Red Open Red Open Red Open
Blue Open Blue Open Blue Open
Yellow Open Yellow Open Yellow Open
Red Closed Yellow Closed
Red Pending
Requirements - if met return TRUE, otherwise FALSE:
The STATUS for each TYPE must be 'Open'.
If there is more than 1 record for the same TYPE then the STATUS on at least 1 record must be 'Open', the STATUS on the remaining records must be 'Closed'.
Results based on requirements above:
Sample Data 1 = FALSE
Sample Data 2 = TRUE
Sample Data 3 = TRUE
Any and all help much appreciated, apologies if this question is a duplicate.
Oracle SQL doesn't have boolean types, so let's use 0 and 1:
select type,
(case when count(distinct case when status = 'open' then type end) = count(distinct type) and
count(case when status = 'open' then type end) = count(distinct type) and
sum(case when status not in ('open', 'closed') then 1 else 0 end) = 0
then 1 else 0
end) as flag
from t
group by type;
The logic:
The first condition says that each type has at least one "open".
The second condition (combined with the first) says that each type has exactly one "open".
The third condition says that the only status are "open" and "closed".

sql server query for update rows which are multiple of 2

I've a table with many rows. I've to meet the requirement that those rows which are multiple of 2 should only be updated. e.g.
update [DBO].[ZZZ_FKP_FEMALE_FULLNAME_TBL]
set remarks = 'multiple of TWO'
--- update only those rows which are multiple of 2.
--- where ID = MULTIPLE OF 2
here ID column is primary key with auto increment
How can I solve this?
You can use modulo as #jarlh said, here is the code:
UPDATE T SET T.remarks = 'multiple of TWO'
FROM [DBO].[ZZZ_FKP_FEMALE_FULLNAME_TBL] AS T
WHERE ID % 2 = 0
update [DBO].[ZZZ_FKP_FEMALE_FULLNAME_TBL] set remarks = 'multiple of TWO'where ID % 2 = 0

Update a column based on its current value

Is there any way to change field value based on its current value with one query?
Like I have tbl.team and if it's value = 1, change it to 2. And vice versa, tbl.team = 2 => 1.
You can use a case expression to update the column conditionally:
update the_table
set team = case
when team = 1 then 2
else 1
end
where team in (1,2);

Modifying column value according to status, and adding extra rows. (sql)

In my table I have a "name" column and a "status" column.
the status is either true or false.
And another table contains a number which is a total amount.
The result that I want to get is a table with two columns:
name | status
and and example of a data:
a | available
a | available
a | not available
a | not available
a | available
when "a" is in the name column and the availability is the status column.
The total amount from the second table indicates the total number of "a" rows that i need to have, and the status depends on the true/false from the status column in the original table.
If the status is "true" I need to write "available" and when "false" then "not available".
If the total amount value is bigger than the data I have in the first table, I need to add rows according to the total amount with the status "available".
For example, If I have 3 records of "a", when one has the status "true" and the other two have the status "false", and the total amount is 4, In the result I need to get 4 rows with the name "a", 2 of them "available" and 2 "not available" (the given 3 rows, plus one row to make it 4).
My question is, how can I change the value according to the data in the table? (Write available/ not available)
And how can I add a certain amount of rows with preset values (same name as before, and "available" status)?
"...how can I change the value according to the data in the table?"
You can use CASE() to test for the value of the column.
SELECT name,
CASE WHEN status = 'true'
THEN 'available'
ELSE 'not available'
END status
FROM tableName
For future answer-seekers:
To get the 'fake' rows, one way is to use are recursive CTE:
WITH Expanded_Data as (SELECT Counted_Data.name,
CAST('true' as VARCHAR(5)) as status,
Counted_Data.count + 1 as count,
Total.count as limit
FROM (SELECT name, COUNT(*) as count
FROM Data
GROUP BY name) Counted_Data
JOIN Total
ON Counted_Data.name = Total.name
AND Counted_Data.count < Total.count
UNION ALL
SELECT name, status, count + 1, limit
FROM Expanded_Data
WHERE count < limit)
SELECT name, CASE WHEN status = 'true'
THEN 'available'
ELSE 'not available' END
FROM (SELECT name, status
FROM Data
UNION ALL
SELECT name, status
FROM Expanded_Data) d;
(have a working SQL Fiddle example.)
I'm a little worried about the initial duplication in the source data though; I can only hope there is more 'unique' information as well.

Get specific "version" of a column

I have an app that uses SQLite to store data locally. There may be more than one file storing the data. All the files contain a list of items, but only one of them has the "correct" status for the current user (basically, there is a "user" db in $HOME and a "system-wide" one in /etc).
Usually, both files will contain the same list of items, and only the status column will differ. If, however, either contains items not in both, I want that data as well.
SQLite does not have FULL OUTER JOIN.
A solution I have come up with is this:
SELECT item, group_concat(status) AS status FROM (SELECT item,status FROM items UNION SELECT item,status FROM otherdb.items) GROUP BY item;
And then parsing the comma-separated "status" output to get the "right" status. I would like a pure SQL solution, however.
The values I want for status are:
If any = 1, status = 1
elif any = -1, status = -1
elif any = 2, status = 2
elif any = -2, status = -2
else status = 0 or NULL
status may only (in the db) be -2,-1,0,NULL,1,2 so this covers all data.
If there is a solution that only gives whichever one is non-zero and non-null, that could work too, although I would prefer the above.
I would sugest you one approach:
1. create a temp table, one adittional column for a flag "otherbd";
2. throw everything from the 2 tables in there;
3. delete the lines you don't want;
Create a status priority table with the following values
status priority
1 5
-1 4
2 3
-2 2
0 1
NULL 0
The concept is to join your two tables against this StatusPriorty table, Group the records and use MAX(Priority) to get your results