update value of a datacolumn in a datable - datatable

I have a datatable with data as below;
PID TName RName NameID PStatus Status
1 AA AA 1
2 BB DE 0
Now I want to update the datacolumns "NameID" and "Status" for all the rows based on some conditions.
To update NameID:- If columns TName and RName are same, the column NameID should be updated as "Success" else "Failure"
To update Status:- If column "PStatus" has value 1, then Status column should be updated as "yes", and "No" for value 0
So, the after updations, the datatable should be like below:
PID TName RName NameID PStatus Status
1 AA AA Success 1 Yes
2 BB DE Failure 0 No
How to achieve this?

update TableName
set NameID=case
when TName==RName then 'Success'
else 'Failure';
update TableName
set Status=case`enter code here`
when PStatus==1 then 'yes'
else 'no';

Related

Group by on Key columns and get the records having one valid value and zero value

we have a scenario where as per group by key columns i have to get only record having combination of 0 & any valid number for column "ID"
with the below example
TransId,CustomerNm,Date ,Number,Gender,ID
1 ,Surya ,2020-01-01,123456,M ,1234
1 ,Surya ,2020-01-01,123456,M ,0
2 ,Naren ,2020-01-20,123456,M ,3456
2 ,Naren ,2020-01-20,123456,M ,6789
When i try with the below query (key columns: TransId,CustomerNm,Date,Number,Gender)
select TransId,CustomerNm,Date,Number,Gender from INS.Transaction
group by TransId,CustomerNm,Date,Number,Gender having count(*) > 1
i will get both the records
1 ,Surya ,2020-01-01,123456,M
2 ,Naren ,2020-01-20,123456,M
but i am trying to get the records having ID =0 . Expecting output as
1 ,Surya ,2020-01-01,123456,M
Pls suggest if i can add any change in the above query
while doing group by add MIN(ID) in select query and then fetch id 0
select * from (
select TransId,CustomerNm,Date,Number,Gender,MIN(ID) ID from INS.Transaction
group by TransId,CustomerNm,Date,Number,Gender having count(*) > 1 ) where ID = 0
Just add another AND to the having clause to check if min(id) = 0 . I have renamed some columns.
select transid,customernm,transaction_date,some_number,gender
from transaction
group by transid,customernm,transaction_date,some_number,gender
having count(*) > 1 and min(id) = 0;
Note:- Do not use reserved words as column name like date and number

how can I do it

`
SELECT TXNID,
TOTAL_AMOUNT,
ACCOUNT_CATEGORY,
DEBIT
FROM ACCOUNTING_BOOK
WHERE TXNID like 'M003%' ;
TXNID:M003 TOTAL_AMT:25 ACCOUNT_CATEGORY:Revenue Debit:null Credit:null
TXNID:M003 TOTAL_AMT:25 ACCOUNT_CATEGORY:Asset Debit:null Credit:null
Rules for populating debit column:[Total-Amount if account-category=Expense or if account-category=Asset for corresponding Revenue item matched by TxnId; 0 otherwise]
Want to validate the result inside this table into debit column.How can I do it?
Rules for populating credit column:[Total-Amount if account-category=Revenue or if account-category=Asset for corresponding Expense item matched by TxnId will return 1 ; 0 otherwise]
Want to validate the result inside this table into credit column.How can I do it?
If I understood rules correctly:
update accounting_book ab
set debit = case when account_category in ('Expense', 'Asset')
then (select sum(total_amt)
from accounting_book
where account_category = 'Revenue'
and txnid = ab.txnid)
else 0
end
Edit:
Rules for populating credit column:[Total-Amount if
account-category=Revenue or if account-category=Asset for
corresponding Expense item matched by TxnId will return 1 ; 0
otherwise]
Similar to previous update:
update accounting_book ab
set credit = case when account_category in ('Revenue', 'Asset')
and exists (select 1
from accounting_book
where account_category = 'Expense'
and txnid = ab.txnid)
then 1
else 0
end
CASE WHEN A.ACCOUNTCATEGORY IN('Revenue','Asset')
AND EXISTS (SELECT TOTAL_AMOUNT
FROM ACCOUNTING_BOOK
WHERE ACCOUNTCATEGORY='Expense'
AND TXNID=A.TXNID)
THEN A.TOTAL_AMOUNT
ELSE 0
END,
CASE WHEN A.ACCOUNTCATEGORY IN('Expense','Asset')
AND EXISTS (SELECT TOTAL_AMOUNT
FROM ACCOUNTING_BOOK
WHERE ACCOUNTCATEGORY='Revenue'
AND TXNID=A.TXNID)
THEN A.TOTAL_AMOUNT
ELSE 0
end);
END LOOP;
END;
/

hive insert overwrite table with inner sub query of count of columns as result

Hi I have below Source table "status table"
date status name
2017-06-22 true 1.tar
2017-06-22 true 2.tar
2017-06-22 false 3.tar
2017-06-22 true 4.tar
2017-06-22 false 5.tar
2017-06-21 false 6.tar
2017-06-21 false 6.tar
2017-06-21 false 6.tar
2017-06-21 true 6.tar
I have below destination table columns with expected data
True False Total Date
3 2 5 2017-06-22
1 3 4 2017-06-21
I wrote below query to load the data from source table to destination table, but it says
Expression not in GROUP BY key
SET hive.exec.dynamic.partition.mode=nonstrict;
SET hive.auto.convert.join=true;
INSERT OVERWRITE TABLE destination PARTITION(date_time)
SELECT
count(status=true) AS success,
count(status=false) AS fail,
success + fail
FROM
status;
Please help me with the missing link. Thanks in advance.
Hive does not support aliases references in the SELECT clause (success + fail)
COUNT counts everything that is not NULL. FALSE is not NULL.
Date is a reserved word. use `Date` or even better, find another name.
You haven't grouped by Date.
When using dynamic partitioning, the partition column(s) should be selected, last.
select count (case when status = true then 1 end) as success
,count (case when status = false then 1 end) as fail
,count (status) as total
,`date`
from status
group by `date`
thanks for answering, however I found out my mistake, I was using count function instead of sum. Here is the code below.
SELECT
sum(case when status ='true' then 1 else 0 end),
sum(case when status ='false' then 1 else 0 end),
sum(case when status ='true' then 1 else 0 end) + sum(case when status='false' then 1 else 0 end)
from status where date='2017-06-21';

How to use If condition inside a Case statement?

Below is my query. Is this righ?
SQL> select case when value in (1000) then null
2 when user in ('ABC') then user
3 when area in ('DENVER') then
4 if value = 2000 then 'Service1'
5 else value = 3000 then 'Service2'
6 end if
7 else null
8 end as num_code from service_usoc_ref;
if prin = 2000 then 'Omaha'
*
ERROR at line 4:
ORA-00905: missing keyword
Please help me.
You can either put another case or use decode (as #madhu suggested):
select case
when value in (1000) then null
when user in ('ABC') then user
when area in ('DENVER') then
case when value = 2000 then 'Service1'
when value = 3000 then 'Service2'
end
else null
end as num_code
from service_usoc_ref;
This could help you
select case when value in (1000) then null
when user in ('ABC') then user
when area in ('DENVER') then
decode( value, 2000 , 'Service1',3000 , 'Service2', NULL) num_code
from service_usoc_ref;

Oracle - additional column check with min function and group by

My Oracle table has the columns: message_id, status, status_date
I would like to return message_id where when grouping by message_id the record with the mininum value for status_date has a value of 'PC' in the status column.
In other words do not return the record if the record that is returned with the minimum value of status_date when grouped by mess_id does not have a value of 'PC' in the status column.
Thanks,
Brian
I'm guessing this is what the source data may look like:
message_id status status_date
---------- ------ -----------
1 PC 01-JAN-12
1 QC 02-JAN-12
1 RC 03-JAN-12
2 AA 04-JAN-12
2 PC 05-JAN-12
2 CC 06-JAN-12
3 PC 07-JAN-12
3 PC 08-JAN-12
3 PC 09-JAN-12
And that the expected output would be something like this:
message_id
----------
1
3
The reason that these two records are returned are because for all the records where message_id=1, the one with the minimum status_date has a value of 01-JAN-12. The record with message_id=1 and status_date of 01-JAN-12 has a status of 'PC', and similarly with message_id=3. Since the minimum status_date of the records with message_id=2 is 04-JAN-12, and it's status is NOT 'PC'.
We can build this query using inline views, but there's probably an easier way using analytic functions.
SELECT A.MESSAGE_ID
FROM MY_TABLE A,
( SELECT B.MESSAGE_ID, MIN(B.STATUS_DATE) MIN_DATE
FROM MY_TABLE B
GROUP BY B.MESSAGE_ID ) C
WHERE A.STATUS = 'PC'
AND A.STATUS_DATE = C.STATUS_DATE
AND A.MESSAGE_ID = C.MESSAGE_ID;

Resources