Query Update where higher data in same table Oracle - sql

i have table data like this, namely DATA table:
DATA
-------------------------------
NIK TIME ACTION
-------------------------------
1500671 07:30:00 0
1500671 15:37:00 0
1600005 07:25:00 0
1600005 16:29:00 0
1600006 07:16:00 0
1600006 17:15:00 0
in that table i wanna update data set ACTION=1 where time is higher in same NIK. Anyone can help me?

Please describe the problem appropriately when asking for assistance in technical forums. eg: ddl, test data etc. Anyway, I hope the below helps you.
UPDATE DATA
SET ACTION = 1
WHERE TIME IN (
SELECT MAX(TIME) FROM DATA GROUP BY NIK)

Related

SELECT from 50 columns

I have a table that has many columns around 50 columns that have datetime data that represent steps user takes when he/she do a procedure
SELECT UserID, Intro_Req_DateTime, Intro_Onset_DateTime, Intro_Comp_DateTime, Info_Req_DateTime, Info_Onset_DateTime, Info_Comp_DateTime,
Start_Req_DateTime, Start_Onset_DateTime, Start_Comp_DateTime,
Check_Req_DateTime, Check_Onset_DateTime, Check_Comp_DateTime,
Validate_Req_DateTime, Validate_Onset_DateTime, Validate_Comp_DateTime,
....
FROM MyTable
I want to find the Step the user did after certain datetime
example I want to find user ABC what the first step he did after 2 May 2019 17:25:36
I cannot use case to check this will take ages to code
is there an easier way to do that?
P.S. Thanks for everyone suggested redesigning the database.. not all databases can be redesigned, this database is for one of the big systems we have and it is been used for more than 20 years. redesigning is out of the equation.
You can use CROSS APPLY to unpivot the values. The syntax for UNPIVOT is rather cumbersome.
The actual query text should be rather manageable. No need for complicated CASE statements. Yes, you will have to explicitly list all 50 column names in the query text, you can't avoid that, but it will be only once.
SELECT TOP(1)
A.StepName
,A.dt
FROM
MyTable
CROSS APPLY
(
VALUES
('Intro_Req', Intro_Req_DateTime)
,('Intro_Onset', Intro_Onset_DateTime)
,('Intro_Comp', Intro_Comp_DateTime)
.........
) AS A (StepName, dt)
WHERE
MyTable.UserID = 'ABC'
AND A.dt > '2019-05-02T17:25:36'
ORDER BY dt DESC;
See also How to unpivot columns using CROSS APPLY in SQL Server 2012
The best way is to design your table with your action type and datetime that action was done. Then you can use a simple where clause to find what you want. The table should be like the table below:
ID ActionType ActionDatetime
----------- ----------- -------------------
1492 1 2019-05-13 10:10:10
1494 2 2019-05-13 11:10:10
1496 3 2019-05-13 12:10:10
1498 4 2019-05-13 13:10:10
1500 5 2019-05-13 14:10:10
But in your current solution, you should use UNPIVOT to get what you want. You can find more information in this LINK.

select a number of strings inside a clob

sorry I know you expect code examples but I have absolutly no idear how to start with that issue.
I have a database with about 100000 entries of that structure:
ID | LONGARG
0 ECLONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
ECLONG_TEXT_INSIDE_THIS2|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
1 ECLONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
2 ECLONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
ECLONG_TEXT_INSIDE_THIS2|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
ECLONG_TEXT_INSIDE_THIS3|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
3 ECLONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
ECLONG_TEXT_INSIDE_THIS2|LONG_TEXT_INSIDE_THIS|LONG_TEXT_INSIDE_THIS
Longarg is of type CLOB.
My question is, is there a possibility to select all the text that is between EC and the first | to get a result like that without using a StoredProcedure and for all datarows?
Result:
LONG_TEXT_INSIDE_THIS
LONG_TEXT_INSIDE_THIS2
LONG_TEXT_INSIDE_THIS
LONG_TEXT_INSIDE_THIS
LONG_TEXT_INSIDE_THIS2
LONG_TEXT_INSIDE_THIS3
LONG_TEXT_INSIDE_THIS
LONG_TEXT_INSIDE_THIS2
Thanks in advance for your help
Stefan

How to increment a SQL entry during a SQL Update in SQLIite

I feel like this is a pretty basic SQL question but I am having trouble searching for the answer.
Essentially the logic I want to write in my SQL statement is this.
When doing an update on a row instead of just blanking out the data there add it together.
so if I have a row "9/19/13" | 0 | 1 | 0 and now I want to update that row with this entry "9/19/13" | 0 | 1 | 0 I get "9/19/13" | 0 | 2 | 0.
My current update command looks like so.
UPDATE entries(Date, John, Mark, Casey) SET (#Date, #John, #Mark, #Casey) WHERE(Date = #Date);
I could easily do it in my actual code where I would retrieve the entry increment it then just do a regular update, but I feel like it should be possible to to do it with straight SQL, and would be cleaner.
Is this what you mean?
-- #John, #Mark, #Casey are set in advance
UPDATE entries(Date, John, Mark, Casey)
SET (#Date, John + #John, Mark + #Mark, Casey + #Casey)
WHERE (Date = #Date);
calling update is just setting the values in the db to be the values you pass in, not the existing value added (in the case of numbers) to the value you're updating with.
it would be much simpler to just increment the value before update as you already have it.

Self join/update of table with data in same table

Due to combining old and new system data in a table I have a list of data like this:
Work no Work name
========= =========
123456 James
123456 James, (123456)
And I want to update to:
Work_no Work_name
========= =========
123456 James
123456 James
I tried building an update statement, wasn't too confident in it so ran it as an equivalent select statement to see what returned and it seems to be running in an infinite loop (there's about 200k records and when I stopped it it was at somewhere in 2 Million returned!) although what it was returning at the start looked fine it just seemed to be duplicating or something:
UPDATE c1
set c1.Work_name = c.Work_name
FROM table c1
INNER JOIN table c ON c1.Work_no = c.Work_no
where charindex(',',c1.Work_name) > 0
Only got experience doing the simplest update statements - a bit stuck with this one if anyone could suggest what I am doing wrong and best way to rectify it?
Thanks
Andrew
Do you have an index built on work_no?
Did you try:
UPDATE #table set Work_name = (select top 1 c.Work_name FROM #table c where #table.Work_no = c.Work_no and charindex(',',c.Work_name) = 0) where charindex(',',c.Work_name) > 0

SQL - convert data to a date/source/value "grid"

I have data in an MYSQL database that looks like this:
Project Date Time
A 2009-01-01 15
A 2009-01-02 10
B 2009-01-02 30
A 2009-01-09 15
C 2009-01-07 5
I would like to produce output from this data like this:
Date Project A Time Project B Time Project C Time
2009-01-01 15 0 0
2009-01-02 10 30 0
2009-01-07 15 0 5
Can this be done with an SQL query, or do I need to write an external script to itterate through the DB and organize the output?
(Also, if someone has a better suggestion for a subject line let me know and I'll edit the question; I'm not sure of the proper terms to describe the current and desired formats, which makes searching for this information difficult)
You're looking for pivot / crosstab support. Here is a good link.
http://en.wikibooks.org/wiki/MySQL/Pivot_table
I believe this is called Pivot table. Just google for it.
I've been looking for something like this and found a MySQL stored procedure that works perfectly:
http://forums.mysql.com/read.php?98,7000,250306#msg-250306
The result you're looking for could be obtained from the following simple call:
call pivotwizard('date','project','time','from_table','where_clause')