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









share|improve this question


















  • 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

















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









share|improve this question


















  • 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















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









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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
















  • 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














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






share|improve this answer





















  • also I have a column started_date but I didn't mentioned. thanks for you answer
    – user3671361
    Nov 7 at 9:57













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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






share|improve this answer





















  • also I have a column started_date but I didn't mentioned. thanks for you answer
    – user3671361
    Nov 7 at 9:57

















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






share|improve this answer





















  • also I have a column started_date but I didn't mentioned. thanks for you answer
    – user3671361
    Nov 7 at 9:57















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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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




















  • 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




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini