I'm trying to use the MERGE function :
see example :
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=3f49d206f5a82d88112387c049d441cb
Update is OK :
1000 150 1 YES
1001 150 1 YES
1003 150 1 YES
But my dream is to get this :
1000 150 1 YES
1001 150 1 YES
1002 150 1 YES
1003 150 1 YES
Why don't I find the 1002 ?
Something wrong on my MERGE probably, but I don't understand the problem.
Could you help me please ? :-)
Thanks,
Abou Ilyès
In your source table the key for 1002 already exists
merge is doing what it should be , updating when condition matches and not doing anything when the key is already present.
SELECT * from STATS_CLIENT_TEST;
below is screenshot from your fiddle, check that id 1002 is already available in the table.
in you update part, you are setting this field "codeaxestat" exactly same as it is before , you have the same value in where clause ?
s.codeaxestat= '150',
s.codeelementstat = '1',
s.valeuraxestatistiqueclient='YES'
WHERE codeaxestat='150'
Related
I have a table with 2 columns (DateKey and StoreCode) and I want to calculate age of each store in each day(AgeOfStore) using DAX. Actually I want to know how many days each store have worked?
For example, in 20210101, Store 1001 did its first work day and then this store in 20210102 did its second day,...
DateKey
StoreCode
AgeOfStore
20210101
1001
1
20210101
1002
1
20210102
1001
2
20210102
1002
2
20210102
1003
1
20210103
1001
3
20210103
1002
3
20210103
1003
2
20210104
1001
4
20210104
1002
4
20210104
1003
3
Thank you in advance.
DAX Measure
AgeOfStore =
RANKX (
FILTER(ALL(tbl),tbl[StoreCode]=max(tbl[StoreCode])),
CALCULATE ( MAX ( 'tbl'[DateKey] ) ),
,
ASC
)
DB version : 11.2.0.4
OS Solaris 5.10
REQUIREMENT : Delete unused records from table only after finding if the record is being accessed or not.
We have a table employee, which has 100,000 records, if anyone select a particular record/records from employee table then, the respective record's STATUS column should get updated to 'ACTIVE' value.
This is required for auditing purpose, 60 days later we will delete all the records from employee table whose STATUS column value is is NULL. how can this be achieved?
My understanding so far, correct me if I am wrong:
1) Trigger can't be used as there is no SELECT event, we only have UPDATE, INSERT, DELETE event.
2) Oracle FGA (Fine grain auditing) may not solve the purpose. or may be I am not aware, is it doable with FGA?
Table:
CREATE TABLE EMPLOYEE
(
EMPID NUMBER,
NAME VARCHAR2(20 BYTE),
SALARY NUMBER,
DEPART VARCHAR2(100 BYTE),
STATUS VARCHAR2(100 BYTE)
)
sample records:
EMPID NAME SALARY DEPART STATUS
---------- --------------- ---------- -------------------- ----------
101 ALFA 1000 IT
102 BETA 2000 CLERK
103 PETER 3000 FINANCE
104 JOHN 4000 IT
105 MESSI 5000 TECH
106 ROMEO 5000 TECH
107 TERI 5000 TECH
108 ROBERT 5000 TECH
Example:
If any one issue below statements
query 1: SELECT * from EMPLOYEE where name='MESSI';
the auditing should update the STATUS='ACTIVE' of empid=105
EMPID NAME SALARY DEPART STATUS
---------- --------------- ---------- -------------------- ----------
101 ALFA 1000 IT
102 BETA 2000 CLERK
103 PETER 3000 FINANCE
104 JOHN 4000 IT
105 MESSI 5000 TECH 'ACTIVE'
106 ROMEO 5000 TECH
107 TERI 5000 TECH
108 ROBERT 5000 TECH
query 2: SELECT * from EMPLOYEE where DEPART='TECH';
The auditing should update the STATUS='ACTIVE' for empid=105,106,107,108
EMPID NAME SALARY DEPART STATUS
---------- --------------- ---------- -------------------- ----------
101 ALFA 1000 IT
102 BETA 2000 CLERK
103 PETER 3000 FINANCE
104 JOHN 4000 IT
105 MESSI 5000 TECH 'ACTIVE'
106 ROMEO 5000 TECH 'ACTIVE'
107 TERI 5000 TECH 'ACTIVE'
108 ROBERT 5000 TECH 'ACTIVE'
The solution suggested by Oracle to achieve what to want is Known as ILM or Information Lifecycle Management.
It allows you to archive, delete, move or do specific actions depending on some criterias (Such as last access).
But be aware, it requires an additional license.
I have a suggestion :
Enable SELECT audit on the target table
Create an AFTER INSERT TRIGGER that will get the inserted line in DBA_AUDIT_TRAIL et extract SQL_TEXT if the OBJECT_NAME was your table (+others checks)
Inside this trigger, do some string operations to get the WHERE CONDITION & use it for update.
Sounds like an ambiguous requirement to me. What happens if one does select * or select count(*)? That under the specification would make all employee records used.
The view v$SQL would contain "all" SQL statements issued, you could always look in there (going through v$plan_table with the object). Then use those to reverse engineer if the record was touched.
You may try FGA ( fine-grained Audit ) using the DBMS_FGA Package. Try Running the code block below in your schema. If it executes successfully , check if audits are lodged periodically in the DBA_COMMON_AUDIT_TRAIL or DBA_AUDIT_STATEMENT table ( check documentation ) while running a select query. Schedule a program that runs periodically to check the audit table and update the status column accordingly. I have never tried anything like this, However!.
BEGIN
dbms_fga.Add_policy(object_schema => 'HR', object_name => 'EMPLOYEE',
policy_name => 'STATUS_UPDATE', audit_condition => 'SELECT',
audit_column => 'NAME ,DEPART', handler_schema => NULL,
handler_module => NULL,
ENABLE => TRUE);
END;
/
I have following data table.
ID salary occupation
1 5000 Engineer
2 6000 Doctor
3 8000 Pilot
4 1000 Army
1 3000 Engineer
2 4000 Teacher
3 2000 Engineer
1 1000 Teacher
3 1000 Engineer
1 5000 Doctor
Now I want to add another column flag to this table so that it looks in the following way.
ID salary occupation Flag
1 5000 Engineer 0
2 6000 Doctor 0
3 8000 Pilot 0
4 1000 Army 0
1 3000 Engineer 1
2 4000 Teacher 1
3 2000 Engineer 1
1 1000 Teacher 2
3 1000 Engineer 2
1 5000 Doctor 3
Now how can I update my original table to the above format using HIVE?
Kindly help me.
Provided that you have data in your files for the additional column you can use Add Column clause for Alter Table.
In your example do something like this:
Alter table Test ADD COLUMNS (flag TINYINT);
Or you can try REPLACE COLUMNS as well:
Alter Table test REPLACE COLUMNS (id int, salary int, occupation String, flag tinyint);
You might need to load(overwrite) your dataset again though(just a speculation!!!).
You can definitely add new columns in HIVE table using alter command as told above
hive>Alter table Test ADD COLUMNS (flag TINYINT);
In Hive 0.13 and earlier releases, column will have NULL values but HIVE 0.14.0 and later release, you can update the column values using UPDATE command
Another way is, after adding column using ALTER command, you can overwrite the existing data with the new data(having Flag column)
hive> LOAD DATA LOCAL INPATH 'flagfile.txt' OVERWRITE INTO TABLE <tablename>;
I'm dealing whit this for a couple of hours and I can't find the way to get the answer.
I've a table with a maximun of 4 records for a product (let's call it that way) for a diferent period (column name with a number). I'm trying to return the ones that DO NOT has a particular type of CONSUMPTION_TYPE_ID. But it doesn't work.
I'll explain it simple. I've a table with these fields (there are more, but these one are just fine)
product_id - CONSUMPTION_TYPE_ID - consumption_period
123 103 1
123 104 1
123 107 1
123 108 1
I need to return the ones that don't has one particular type of consumption, let's say that the type 107 is missing (the row doesn't exists), the select query should show the other 3 or any present. I don't mind doing the same select 4 times, I could also try to do a cursor for it and use loop to check every one. The point is, that the type of query with "not in" or "not exists" doesn't work. It gives me a result like the one given below, but when I query the "consumption_period" it shows me the missing "CONSUMPTION_TYPE_ID" and that's because the "not in" clause it's only hidding the results.
this is what I need.
select * from t1 where CONSUMPTION_TYPE_ID != 108;
product_id - CONSUMPTION_TYPE_ID - consumption_period
123 103 1
123 104 1
123 107 1
I hope you can help me with this. I'm stucked, it maybe simple, but I'm having one of those stucked times. Thanks in advance for any help
You probably should've posted that NOT EXISTS query that doesn't work, because that is the right way to do this.
If I got your requirements right: all products that do not have a record for a specific consumption_type_id.
SELECT DISTINCT product_id
FROM t1 t
WHERE NOT EXISTS
(SELECT 1 FROM t1
WHERE t.product_id = product_id
AND Consumption_Type_ID = ?)
The obvious answer here is to search for CONSUMPTION_TYPE_ID = 108 and have the surrounding code check for a lack of rows, rather than the existence of rows.
If you really need a row return for each consumption_type_id that's not in this table, then you should probably be selecting from the lookup table for consumption_type_id:
select *
from consumption_type ct
where not exists (select *
from t1
where t1.consumption_type_id = ct.consumption_type_id)
and ct.consumption_type_id = 108
currently I am using pl/sql Developer(Oracle). I am told to convert a Row wise arranged data into columns but without the use of PIVOT. Since the Table I am working on dynamically changes, I am not able to use DECODE too.
POLICY SEQS INVDATE SUM(AMT)
-------- ------ ----------- ----------
policA 123 01-JAN-10 40
policA 123 01-FEB-10 50
policA 123 01-MAR-10 60
policA 456 01-JAN-10 360
policA 456 01-FEB-10 450
policA 456 01-MAR-10 540
policA 789 01-FEB-10 1000
polcA 789 01-MAR-10 1000
I have to re-arrange the dates and the sum of amounts column wise. So that the Single Policy and Single SEQS will have the dates and its amount column wise in a line.
"POLICY","SEQS","INST1","INST1SUM","INST2","INST2SUM","INST3","INST3SUM"
"policA","123","01-JAN-10","40","01-FEB-10","50","01-MAR-10","60"
"policA","456","01-JAN-10","360","01-FEB-10","450","01-MAR-10","540"
"policA","789","01-FEB-10","1000","01-MAR-10","1000"
Some Policy might not be starting from Jan, so the INST1 must be from feb, INST2 must be Mar and INST3 and corresponding INSTSUM must be NULL.
Is there any way that this can be done using CROSS JOINS or using xml function?
Can I use xmlagg with alternative data (INST and SUM)?
I have done some research and am not able to solve this out. Can you please help me with this?