Updating table in Oracle - oracle11g

I have a table in Oracle call STATISTICS.
COLUMN NAME DATE TYPE
MODEL VARCHAR2(30 BYTE)
NEW_COUNT NUMBER
NEW_DATE DATE
OLD_COUNT NUMBER
OLD_DATE DATE
PRNCT_CHANGE NUMBER
Now I have sql that updates statistics table:
UPDATE STATISTICS
SET
OLD_COUNT = NEW_COUNT,
NEW_COUNT =
( -- semantic table --
SELECT COUNT(*)
FROM TABLE(SEM_MATCH(
'{
?s ?p ?o
}',
SEM_Models(MODEL),NULL,
SEM_ALIASES(SEM_ALIAS('','http://SEMANTIC#')),NULL))
),
OLD_DATE = NEW_DATE,
NEW_DATE = SYSDATE
WHERE MODEL = &MY_MODEL
;
Now, can I do this? Push the date from a new date to an old date before I update the new date?
I am also doing the same thing with the NEW_COUNT and OLD_COUNT...
It sounded logical but is ok to do this?

So I followed my own advice :) and it worked just fine. I am not sure if this is the best practice but it does the trick

Related

List the details of the senior employee belongs to a certain year

I wish to obtain in sqlite :
List the details of the senior employee belongs to 1981
But the
select * from emp where hiredate in (select min(hiredate) from emp
where to_char( hiredate,’YYYY’) = ‘1981’)
not working. I suppose the to_char function is not recognized.
I've tried
select min(hiredate) from table
where hiredate IN ('1981%')
result NULL
I mention the date is in the sqlite supported format ; yyyy-mm-dd
Thank you in advance! :))))
I assume that the hiredate's format is YYYY-MM-DD since this is the only format that is directly comparable and can be manipulated as a date in SQLite (although it is actually TEXT).
If so, then use LIKE operator like this:
select * from emp
where hiredate = (select min(hiredate) from emp where hiredate like '1981%')
If you want the earliest hire date in 1981, just use
SELECT *
FROM emp
WHERE hiredate LIKE '1981-%'
ORDER BY hiredate
LIMIT 1;
which sorts all the rows with 1981 hire dates by that date and only returns the first (minimum) one.
Instead of to_char () you can use the strftime function.
strftime ('% Y', hiredate)
SELECT MIN(hiredate) FROM table WHERE STRFTIME ('% Y', hiredate) = '1981'

Retrive date wise data from SQLITE database kotlin

I have one SQlite database where i store some data with date.
Now i want to get data date wise like:
Month wise - it means i pass the value like Current date is EndDate to this month 1st date.
Year wise - it means i want to get data 1st-april-20..previews to 31-march-20..current
Start and End date wise - hear is which i pass date.
For that i got one solution is HERE for java.
But i have no idea this HOW TO WORK. Anyone please explain me how to work this and How to i get data as i mention above. FOR KOTLIN
TABLE
db.createTable(customerDetail, true,
credit_id to INTEGER + PRIMARY_KEY + AUTOINCREMENT,
credit_type to TEXT,
c_id to TEXT,
credit_amount to TEXT,
credit_date to TEXT,
addFrom to TEXT
)
UPDATE
For Month wise data i'll try below query like:
"SELECT * FROM $customerDetail WHERE $credit_date BETWEEN strftime('%d-%m-%Y', '$startDate') AND strftime('%d-%m-%Y', '$currentDate')"
/*SELECT * FROM CustomerDetail WHERE credit_date BETWEEN strftime('%d-%m-%Y', '01/02/2019') AND strftime('%d-%m-%Y', '23/02/2019')*/
But it's give me arralistSize = 0.
After that i can write new query like:
"SELECT * FROM $customerDetail WHERE $credit_date BETWEEN '$startDate' AND '$currentDate'"
/*SELECT * FROM CustomerDetail WHERE credit_date BETWEEN '01/02/2019' AND '23/02/2019'*/
In this query data will return. But it's return all data without any filtering.
If anyone knows why this work like this please help me.
MY DATA LIST
Thanks in advance.
Solution :
Solution for MONTH wise
I just change date format "dd/mm/yyyy" TO "yyyy/mm/dd" and re insert all data.
AND Fire below QUERY :
"SELECT * FROM $customerDetail WHERE $credit_date BETWEEN '$startDate' AND '$currentDate'"
SQLite does not have a Date data type like other RDBMS's. It treats dates as TEXT.
So when it compares the credit_date column it actually does compare strings.
This would be fine if you stored your dates in the format YYYY-MM-DD and compared against the same format.
But if you store your dates in the format DD/MM/YYYY then you can't compare.
So the best solution would be to change the format of the column credit_date to YYYY-MM-DD.
If this is not possible then you have to transform the string value of the column like this:
substr(credit_date, 7, 4) || '-' || substr(credit_date, 4, 2) || '-' || substr(credit_date, 1, 2)
Now you can use this in a query like:
SELECT * FROM $customerDetail
WHERE substr($credit_date, 7, 4) || '-' || substr($credit_date, 4, 2) || '-' || substr($credit_date, 1, 2)
BETWEEN '$startDate' AND '$currentDate'
But you have to make sure that $startDate and $currentDate are also in the format YYYY-MM-DD

i have two tables student and trainer. i want to get values from student and trainer table based on some condition

i have student and trainer tables :
student table:
student_id (primary key)
name
email
trainer table:
trainer_id
student_id
amount
output has to:
sid name email amount
22 ram r#g 200
34 sam r#f
i want to get (student_id,name,email) from student table and (amount) from trainer table(imp : trainer_id and student_id should match(like sid = 46,tid =78,amount=500) then only the amount has to display value. otherwise amount will display empty but (student_id,name,email) should display)
in trainer table, student_id and trainer_id has to match...based on that amount will come..i mean if we send the select query as "select amount from trainer where student_id= 20 and trainer_id=36...". that column should match for sid and tid
If you do it this way, It wil not show data if amount is empty :
select st.student_id,
st.name,
st.email,
tt.amount
from student_table st, trainer_table tt
where st.student_id = tt.student_id
NVL function lets you substitute a value when a null value is encountered,
so if you do it this way, It wil show data and show 0 instead of null:
select st.student_id,
st.name,
st.email,
(nvl(select tt.amount
from trainer_table tt
where st.student_id = tt.student_id,0))) amount
from student_table st
This can be accomplished with PLSQL. Sorry it is so extensive but I hope it allows you to see the power of PLSQL if you need to manipulate data based on conditionals.
DECLARE
TYPE result_record IS RECORD
( sid NUMBER
, name VARCHAR2(60)
, email VARCHAR2(60)
, amount NUMBER);
CURSOR c IS select st.student_id sid,
st.name name,
st.email email,
tt.amount amount
from student_table st, trainer_table tt
where st.student_id = tt.student_id;
TYPE results_table IS TABLE OF results_record INDEX BY BINARY_INTEGER;
c_rec c%ROWTYPE;
temp_rec RESULTS_RECORD;
results RESULTS_TABLE;
lv_index NUMBER := 0;
BEGIN
OPEN c;
WHILE lv_index <= c%ROWCOUNT LOOP
FETCH c INTO c_rec;
temp_rec.sid := c_rec.sid;
temp_rec.name := c_rec.name;
temp_rec.email := c_rec.email;
temp_rec.amount := c_rec.amount;
results(lv_index) := temp_rec;
lv_index := lv_index + 1;
END LOOP;
CLOSE c;
-- Now we can access and modify our table from inside PLSQL
SELECT * FROM results;
-- Use PLSQL logic to make the table output pretty with '$' and conditionals
FOR i IN results LOOP
dbms_output.put_line(i.sid||' $'||i.amount); -- example for how to access
-- your code here
END LOOP;
END;
/
As always, I hope this gives you some ideas.
-V

Select query with logical operators as sub query

I would like to run a conditional select query on a table of sewer structures (S_Structures) in SQLite. The table contains the following columns: struct_type (structure type) and Year (construction year).
The selection will be based on the structure type as well as structure age, I think I've sorted out the type selection bit and with regards to the age, I intend to deduct the year from the local time. This is all fine and well, but how do I go about defining a logical operator as sub query for the specified age range of: 2 < age <= 5.
Select [Year],[Struc_Type]
FROM [S_Structures]
WHERE [Struc_Type] NOT IN
("Manhole", "Rodding Eye", "Dummy",
"End Manhole", "T-Piece", "Sub- Catchment", "Top End");
AND WHERE (strftime('%Y','now') - Year) > '2'
AND (strftime('%Y','now') - Year); <= '5';
A subquery would involve an additional SELECT in parentheses.
Only a single WHERE clause is allowed.
Just use the BETWEEN operator:
SELECT [Year], [Struc_Type]
FROM [S_Structures]
WHERE [Struc_Type] NOT IN ('Manhole', 'Rodding Eye', 'Dummy', 'End Manhole',
'T-Piece', 'Sub-Catchment', 'Top End')
AND (strftime('%Y','now') - Year) BETWEEN 3 AND 5;

I need to calculate the date / time difference between one date time column

Details.
I have the notes table having the following columns.
ID - INT(3)
Date - DateTime
Note - VARCHAR(100)
Tile - Varchar(100)
UserName - Varchar(100)
Now this table will be having NOTES along with the Titles entered by UserName on the specified date / time.
I need to calculate the DateTimeDifference between the TWO ROWS in the SAME COLUMN
For example the above table has this peice of information in the table.
64, '2010-03-26 18:16:13', 'Action History', 'sending to Level 2.', 'Salman Khwaja'
65, '2010-03-26 18:19:48', 'Assigned By', 'This is note one for the assignment of RF.', 'Salman Khwaja'
66, '2010-03-27 19:19:48', 'Assigned By', 'This is note one for the assignment of CRF.', 'Salman Khwaja'
Now I need to have the following resultset in query reports using MYSQL.
TASK - TIME Taken
ACTION History - 2010-03-26 18:16:13
Assigned By - 00:03:35
Assigned By - 25:00:00
More smarter approach would be
TASK - TIME Taken
ACTION History - 2010-03-26 18:16:13
Assigned By - 3 minutes 35 seconds
Assigned By - 1 day, 1 hour.
I would appreciate if one could give me the PLAIN QUERY along with PHP code to embed it too.
<?php
$start = new DateTime('2009-01-01 00:00:00'); // 31 days
$time_span = $start->diff(new DateTime('2009-02-01 00:00:00'));
var_dump($time_span); // returns '1 month'
$start = new DateTime('2009-02-01 00:00:00'); //28 days
$time_span = $start->diff(new DateTime('2009-03-01 00:00:01'));
var_dump($time_span); // returns '1 month'
?>
DATEDIFF()
It looks like you want to group by case number.
Using your schema and sample data, I think that this is exactly what you wanted:
SELECT t1.ID, t1.title AS task, t1.username,
IFNULL(CONCAT(TIMESTAMPDIFF(MINUTE, t2.currentDate, t1.currentDate)), t1.currentdate) AS time_taken
FROM tps_trans_support_notes t1
LEFT JOIN tps_trans_support_notes t2
ON t2.currentdate < t1.currentdate AND
t2.ID <> t1.ID AND
t2.casenumber = t1.casenumber
LEFT JOIN tps_trans_support_notes t3
ON t3.casenumber = t1.casenumber AND
t3.ID <> t1.ID AND t3.ID <> t2.ID AND
t3.currentdate > t2.currentdate AND
t3.currentdate < t1.currentdate
WHERE t3.ID IS NULL AND
t1.casenumber = '21'
ORDER BY t1.ID
First, the query gets the begin time and end time into the same row, excluding rows where there are times that occur between the two, then it displays the difference.
The query only shows the difference in minutes, but you can use the other DateTime functions to expand that.

Resources