SQL Query. Converted records seen in a row - sql

If I have the following data layout seen below how could I return a query to switch the 'InforceInd' records displaying as '0' to '1'? (As long as 1 'policynumber' record contains a 'InforceInd' record of '1' switch all remaining rows from '0' to '1'?)
COLUMNS - PolicyNumber, InforceInd
ROW - XYZ, 1
ROW - XYZ, 0
ROW - XYZ, 0
Thank you in advance.

Try:
UPDATE MyTable
SET InforceInd = 1
WHERE
InforceInd = 0
AND
PolicyNumber IN (
SELECT PolicyNumber FROM MyTable WHERE InforceInd = 1);

Related

SQL QUERY regarding single row as output

I want a query in which it only returns only one output row based on the values in a column.
What I need is
Table A has values
A
----|----
1 |
2 |
3 |
required output
A
----
yes
so I need a query in which if table a has 3 in its column then the output I need to get is yes or else no.
my code:-
WITH
number_tb AS (select * from t1),
out_put as (select case when a = 3 then 'yes' else 'no' end a_case from number_tb)
select * from out_put
output:-
a_case
-----
no
no
yes
I only need single row output. if 3 is present then yes or no, I don't need it for each row.
Is it possible to do so??
If a proper boolean true/false value is also acceptable, you can use
select exists (select * from t1 where a = 3) as a;
If you want a string with yes/no instead you can use a CASE expression to turn the boolean value into a string.
select case
when exists (select * from t1 where a = 3) then 'Yes'
else 'No'
end as a;

oracle query to get count of added, updated and deleted records based on the date filter

I have a table structure like below
ID OLDID col1 status
1 0 txt1 A - inserted record
2 0 txt1 I - deleted record
3 0 txt1 I - updated record
4 3 txt1 I - updated record
5 4 txt1 I - updated record
6 5 txt1 I - deleted record
7 0 txtx I - updated record
8 7 txt1 A - updated record
I wanted to count the inserted, updated and deleted record in this table, please help me with the oracle query to fetch the count of the records, below are the logic
The logic for new record is ID is new ID and old id is 0 and status is A.
The updated record's status is I, the id of this record will be inserted into the new record's old id column.
the deleted record status with I with old id 0, and the second condition is status is I and ID will not be in OLD id column.
I have tried my best to explain the case, please let me know if any more details are needed. help me with the oracle query.
below query seems to be working, but if no deleted record then I wanted to have 0 for it but in the below query only returns records which have >0 counts, can any expert optimize it, please
SELECT sub.status, COUNT(*)
FROM (SELECT CASE
WHEN yt.oldid = 0 AND yt.status = 'A' THEN 'Insert'
WHEN yt.status = 'I' and QID not in (select oldid from FROM your_table where TO_CHAR (record_date,'MM/YYYY')='04/2020' ) THEN 'Delete'
ELSE 'Update' END AS status
FROM your_table yt where TO_CHAR (record_date,'MM/YYYY')='04/2020')
GROUP BY sub.status;
Neep experts help here, please.
You can use CASE..WHEN and self LEFT JOIN as follows:
SELECT T.*,
CASE WHEN T.OLDID = 0 AND T.STATUS = 'A' THEN 'Insert'
WHEN T.STATUS = 'I' AND T1.ID IS NOT NULL THEN 'Update'
WHEN T.STATUS = 'I' AND T.OLDID = 0 AND T1.ID IS NULL THEN 'Delete'
END AS RECORD_OP
FROM YOUR_TABLE T
LEFT JOIN YOUR_TABLE T1 ON T.ID = T1.OLDID

Compare values in column, return a value based on what is in the columns

I have a table that has 3 columns, ID, Record and signed.... ID is unique but RecordID duplicates because each row is identifying if someone signed off on something in the record. The 3rd column is if they signed off on it or not.
Below is an example of what the table could look like.
What I'm trying to do is return a 1 if the record is completely signed off on or or a 0 if the record isn't completely signed off on....
Here is an example of the table
My output should be like this 5099 =0, 5100 = 0, 5101 = 1, 5102 = 0
I've been racking my head on it
I've tried the following code and it seems to work on items that have more than two rows. But if they have two rows and one signed is yes and the other no I'm getting a 1
select
(case
when not exists(
select *
From Table
Where signed<> '' no) then 0
Else '1'
end);
my output should be like this 5099 =0, 5100 = 0, 5101 = 1, 5102 = 0
SELECT RecordID, CASE WHEN n_signed=n THEN 1 ELSE 0 END FROM
(
SELECT RecordID, SUM(CASE WHEN signed='yes' THEN 1 ELSE 0 END) as n_signed, COUNT(*) as n
FROM table
GROUP BY RecordID
) a
You probably need something like this
select
recordid,
floor(avg(case when signed='yes' then 1 else 0 end)) as signed_status
from your_table
group by recordid;
Demo

Is it possible to force reutrn value from query when no rows found? [duplicate]

This question already has answers here:
Return a value if no record is found
(6 answers)
Get 0 value from a count with no rows
(6 answers)
Closed 7 years ago.
I have a simple query:
Select qty from X where id=....;
this query always return 0 or 1 row.
when it return 1 row everything works.
but if it return 0 rows my query fails as qty is used in calculations. (This is acatually a Sub query in Select statment).
I need somehow to make sure the query always return 1 row.
I tried:
Select coalesce(qty,0) from X where id=....;
but it doesn't help as if there are no rows the coalesce is useless.
if no row found it should give 0
How can I fix it?
You can do this:
SELECT COALESCE( (SELECT qty from X where id=....), 0)
if nothing is returned from the inner SELECT statement, COALESCE will give you 0 in the outer SELECT statement.
Select *.a from (
Select qty, 0 as priority from X where id=....
Union
Select 0, 1 as priority
) a
Order By a.priority Asc
Limit 1
This select basically ensures that at least one row is returned by adding an additional row to the end and by adding the limit statement we just return the first row.
So now there is the case where at least one row is found and the additional row is added to the end. In this case the first row found (see ->) will be returned due to the ascending order by priority:
qty priority
-> 1 0
3 0
4 0
. .
0 1
And then there is the case where no row is found but the additional row is returned:
qty priority
-> 0 1
try this.
SELECT COALESCE((SELECT qty
FROM x
WHERE id = #MyIdVar), 1)
If no records found then its return 'No rows found'
if not exists(Select * from table where condition=...)
Begin
Select * from table where condition=...
End
Else
Begin
Select 'No rows found'
End
Whenever you use this you will get record else no record found message.
Without using COALESCE()
select qty from X where id=1 UNION SELECT 0 order by qty desc limit 1
Demo

SQL - Counting a column twice

I have a table that has a column called 'status'. This can be set to 0 or 1
Is it possible for me to count both the 0's and 1's in a single query?
Thanks in advance
James
Yes, just group on the value of status:
SELECT status, COUNT(*)
FROM yourtable
GROUP BY status
That will give you exactly two rows since the value can only be 0 or 1, and the COUNT(*) column will be the number of times each status value appears in the table.
SELECT SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) AS 'number of zeroes',
SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) AS 'number of ones'
FROM yourtable;