Get the row before where clause
up vote
0
down vote
favorite
I want to get the values of
I have a table named slot_machine_history, that represents all the movements (sold, new relocation, destroyed,suspended) of a given slot machine.
I want to obtain the row before each sold movement identified by 'V' and the 'V' movement. Becuase I want to obtain the company that it was the owner of the slot, and the new owner of the slot
slot_machine_code movement_history current_company
612134 'NEW' 1
612134 'TRANSPORT' 1
612134 'TRANSPORT' 1
612134 'V' 10
612134 'TRANSPORT' 10
612134 'SUSPENDED' 10
612134 'V' 14
my sql query should return:
612134 'TRANSPORT' 1
612134 'V' 10
612134 'SUSPENDED' 10
612134 'V' 14
sql oracle select
add a comment |
up vote
0
down vote
favorite
I want to get the values of
I have a table named slot_machine_history, that represents all the movements (sold, new relocation, destroyed,suspended) of a given slot machine.
I want to obtain the row before each sold movement identified by 'V' and the 'V' movement. Becuase I want to obtain the company that it was the owner of the slot, and the new owner of the slot
slot_machine_code movement_history current_company
612134 'NEW' 1
612134 'TRANSPORT' 1
612134 'TRANSPORT' 1
612134 'V' 10
612134 'TRANSPORT' 10
612134 'SUSPENDED' 10
612134 'V' 14
my sql query should return:
612134 'TRANSPORT' 1
612134 'V' 10
612134 'SUSPENDED' 10
612134 'V' 14
sql oracle select
1
There is no "row before" in SQL, tables are unordered. You'll need a timestamp or sequence value to decide the "row before"
– jarlh
Nov 7 at 8:29
1
How do you determine the correct order?LAG(current_company) over (partition by slot_machine_code order by whatever)
– dnoeth
Nov 7 at 8:34
also I have a column movement_started_date but I didn't mentioned
– user3671361
Nov 7 at 9:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to get the values of
I have a table named slot_machine_history, that represents all the movements (sold, new relocation, destroyed,suspended) of a given slot machine.
I want to obtain the row before each sold movement identified by 'V' and the 'V' movement. Becuase I want to obtain the company that it was the owner of the slot, and the new owner of the slot
slot_machine_code movement_history current_company
612134 'NEW' 1
612134 'TRANSPORT' 1
612134 'TRANSPORT' 1
612134 'V' 10
612134 'TRANSPORT' 10
612134 'SUSPENDED' 10
612134 'V' 14
my sql query should return:
612134 'TRANSPORT' 1
612134 'V' 10
612134 'SUSPENDED' 10
612134 'V' 14
sql oracle select
I want to get the values of
I have a table named slot_machine_history, that represents all the movements (sold, new relocation, destroyed,suspended) of a given slot machine.
I want to obtain the row before each sold movement identified by 'V' and the 'V' movement. Becuase I want to obtain the company that it was the owner of the slot, and the new owner of the slot
slot_machine_code movement_history current_company
612134 'NEW' 1
612134 'TRANSPORT' 1
612134 'TRANSPORT' 1
612134 'V' 10
612134 'TRANSPORT' 10
612134 'SUSPENDED' 10
612134 'V' 14
my sql query should return:
612134 'TRANSPORT' 1
612134 'V' 10
612134 'SUSPENDED' 10
612134 'V' 14
sql oracle select
sql oracle select
asked Nov 7 at 8:27
user3671361
7310
7310
1
There is no "row before" in SQL, tables are unordered. You'll need a timestamp or sequence value to decide the "row before"
– jarlh
Nov 7 at 8:29
1
How do you determine the correct order?LAG(current_company) over (partition by slot_machine_code order by whatever)
– dnoeth
Nov 7 at 8:34
also I have a column movement_started_date but I didn't mentioned
– user3671361
Nov 7 at 9:57
add a comment |
1
There is no "row before" in SQL, tables are unordered. You'll need a timestamp or sequence value to decide the "row before"
– jarlh
Nov 7 at 8:29
1
How do you determine the correct order?LAG(current_company) over (partition by slot_machine_code order by whatever)
– dnoeth
Nov 7 at 8:34
also I have a column movement_started_date but I didn't mentioned
– user3671361
Nov 7 at 9:57
1
1
There is no "row before" in SQL, tables are unordered. You'll need a timestamp or sequence value to decide the "row before"
– jarlh
Nov 7 at 8:29
There is no "row before" in SQL, tables are unordered. You'll need a timestamp or sequence value to decide the "row before"
– jarlh
Nov 7 at 8:29
1
1
How do you determine the correct order?
LAG(current_company) over (partition by slot_machine_code order by whatever)
– dnoeth
Nov 7 at 8:34
How do you determine the correct order?
LAG(current_company) over (partition by slot_machine_code order by whatever)
– dnoeth
Nov 7 at 8:34
also I have a column movement_started_date but I didn't mentioned
– user3671361
Nov 7 at 9:57
also I have a column movement_started_date but I didn't mentioned
– user3671361
Nov 7 at 9:57
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
You need some sensible way to order the rows for this. Hopefully your table has something that increments like a date or counter for every movement event that a machine undertakes:
movement_event slot_machine_code movement_history current_company
1 612134 'NEW' 1
2 612134 'TRANSPORT' 1
3 612134 'TRANSPORT' 1
4 612134 'V' 10
5 612134 'TRANSPORT' 10
6 612134 'SUSPENDED' 10
7 612134 'V' 14
You can then do a query like:
SELECT slot_machine_code, movement_history, current_company FROM
(
SELECT t.*, LEAD(movement_history) OVER(PARTITION BY slot_machine_code ORDER BY movement_event ASC) as next_Movement_history
FROM your_table t
) z
WHERE
z.movement_history = 'V' OR z.next_movement_history = 'V'
If there is no column that denotes the order that records were written, you're kinda up the creek. Add one.
If your current table just-so happens to return the rows in the order they were inserted then it will be enough to use that to add a rownum or similar, but do it soon, because the order that a database returns rows in is never guaranteed to be the order of insertion and internally data reorganizations will change the order of rows returned by an unordered (having no ORDER BY clause) query
also I have a column started_date but I didn't mentioned. thanks for you answer
– user3671361
Nov 7 at 9:57
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You need some sensible way to order the rows for this. Hopefully your table has something that increments like a date or counter for every movement event that a machine undertakes:
movement_event slot_machine_code movement_history current_company
1 612134 'NEW' 1
2 612134 'TRANSPORT' 1
3 612134 'TRANSPORT' 1
4 612134 'V' 10
5 612134 'TRANSPORT' 10
6 612134 'SUSPENDED' 10
7 612134 'V' 14
You can then do a query like:
SELECT slot_machine_code, movement_history, current_company FROM
(
SELECT t.*, LEAD(movement_history) OVER(PARTITION BY slot_machine_code ORDER BY movement_event ASC) as next_Movement_history
FROM your_table t
) z
WHERE
z.movement_history = 'V' OR z.next_movement_history = 'V'
If there is no column that denotes the order that records were written, you're kinda up the creek. Add one.
If your current table just-so happens to return the rows in the order they were inserted then it will be enough to use that to add a rownum or similar, but do it soon, because the order that a database returns rows in is never guaranteed to be the order of insertion and internally data reorganizations will change the order of rows returned by an unordered (having no ORDER BY clause) query
also I have a column started_date but I didn't mentioned. thanks for you answer
– user3671361
Nov 7 at 9:57
add a comment |
up vote
2
down vote
You need some sensible way to order the rows for this. Hopefully your table has something that increments like a date or counter for every movement event that a machine undertakes:
movement_event slot_machine_code movement_history current_company
1 612134 'NEW' 1
2 612134 'TRANSPORT' 1
3 612134 'TRANSPORT' 1
4 612134 'V' 10
5 612134 'TRANSPORT' 10
6 612134 'SUSPENDED' 10
7 612134 'V' 14
You can then do a query like:
SELECT slot_machine_code, movement_history, current_company FROM
(
SELECT t.*, LEAD(movement_history) OVER(PARTITION BY slot_machine_code ORDER BY movement_event ASC) as next_Movement_history
FROM your_table t
) z
WHERE
z.movement_history = 'V' OR z.next_movement_history = 'V'
If there is no column that denotes the order that records were written, you're kinda up the creek. Add one.
If your current table just-so happens to return the rows in the order they were inserted then it will be enough to use that to add a rownum or similar, but do it soon, because the order that a database returns rows in is never guaranteed to be the order of insertion and internally data reorganizations will change the order of rows returned by an unordered (having no ORDER BY clause) query
also I have a column started_date but I didn't mentioned. thanks for you answer
– user3671361
Nov 7 at 9:57
add a comment |
up vote
2
down vote
up vote
2
down vote
You need some sensible way to order the rows for this. Hopefully your table has something that increments like a date or counter for every movement event that a machine undertakes:
movement_event slot_machine_code movement_history current_company
1 612134 'NEW' 1
2 612134 'TRANSPORT' 1
3 612134 'TRANSPORT' 1
4 612134 'V' 10
5 612134 'TRANSPORT' 10
6 612134 'SUSPENDED' 10
7 612134 'V' 14
You can then do a query like:
SELECT slot_machine_code, movement_history, current_company FROM
(
SELECT t.*, LEAD(movement_history) OVER(PARTITION BY slot_machine_code ORDER BY movement_event ASC) as next_Movement_history
FROM your_table t
) z
WHERE
z.movement_history = 'V' OR z.next_movement_history = 'V'
If there is no column that denotes the order that records were written, you're kinda up the creek. Add one.
If your current table just-so happens to return the rows in the order they were inserted then it will be enough to use that to add a rownum or similar, but do it soon, because the order that a database returns rows in is never guaranteed to be the order of insertion and internally data reorganizations will change the order of rows returned by an unordered (having no ORDER BY clause) query
You need some sensible way to order the rows for this. Hopefully your table has something that increments like a date or counter for every movement event that a machine undertakes:
movement_event slot_machine_code movement_history current_company
1 612134 'NEW' 1
2 612134 'TRANSPORT' 1
3 612134 'TRANSPORT' 1
4 612134 'V' 10
5 612134 'TRANSPORT' 10
6 612134 'SUSPENDED' 10
7 612134 'V' 14
You can then do a query like:
SELECT slot_machine_code, movement_history, current_company FROM
(
SELECT t.*, LEAD(movement_history) OVER(PARTITION BY slot_machine_code ORDER BY movement_event ASC) as next_Movement_history
FROM your_table t
) z
WHERE
z.movement_history = 'V' OR z.next_movement_history = 'V'
If there is no column that denotes the order that records were written, you're kinda up the creek. Add one.
If your current table just-so happens to return the rows in the order they were inserted then it will be enough to use that to add a rownum or similar, but do it soon, because the order that a database returns rows in is never guaranteed to be the order of insertion and internally data reorganizations will change the order of rows returned by an unordered (having no ORDER BY clause) query
answered Nov 7 at 8:39
Caius Jard
7,39611135
7,39611135
also I have a column started_date but I didn't mentioned. thanks for you answer
– user3671361
Nov 7 at 9:57
add a comment |
also I have a column started_date but I didn't mentioned. thanks for you answer
– user3671361
Nov 7 at 9:57
also I have a column started_date but I didn't mentioned. thanks for you answer
– user3671361
Nov 7 at 9:57
also I have a column started_date but I didn't mentioned. thanks for you answer
– user3671361
Nov 7 at 9:57
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185759%2fget-the-row-before-where-clause%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
There is no "row before" in SQL, tables are unordered. You'll need a timestamp or sequence value to decide the "row before"
– jarlh
Nov 7 at 8:29
1
How do you determine the correct order?
LAG(current_company) over (partition by slot_machine_code order by whatever)
– dnoeth
Nov 7 at 8:34
also I have a column movement_started_date but I didn't mentioned
– user3671361
Nov 7 at 9:57