Query subselect at least one
up vote
0
down vote
favorite
I have a table named movement_history (that represenents movements of cars (register, sell, suspended, repaired, destroyed)). I want to get the information of a company
that has sold each cars and to who.
This is my table (select *)
code_car date_movement type_movement current_company_code
6000 01/01/2010 NEW 5
6000 01/01/2012 REPARATION 5
6000 01/11/2015 SOLD 8
6000 01/01/2017 DESTROYED 8
4444 01/05/2000 NEW 10
4444 01/05/2000 SUSPENDED 10
4444 01/05/2015 SOLD 5
4444 01/07/2015 RENOVATION 5
4444 09/12/2015 SOLD 18
.... ... ... ...
So if i want all the sales of cars from the company 5 in a determinated time (01-01-2015 to 31-02-2015) this would be the result:
code_car date_movement type_movement current_company_code
6000 01/11/2015 SOLD 8
4444 09/12/2015 SOLD 18
That's my query. First I obtain all the cars that someday have been from the company 5. Then I obtain all the 'SOLD' movements and I want to obtain for each car sold if some day has been part of the company 5.
select
code_car,date_movement,type_movement,current_company_code
from
movement_history where code_car in (
(select code_car from movement_history where current_company_code = 5)) and
code_car IN
(select code_car from movement_history where type_movement =
'SOLD' and code_car <> 5 and date_movement > to_Date('01-01-2015','dd/mm/yyyy') and date_movement < to_Date('31-12-2015','dd/mm/yyyy'));
I guess am doing badly the part to obtain if each car sold some day has been part of the company 5 al least one time.
Any suggestion? Thanks a lot.
sql oracle subquery
add a comment |
up vote
0
down vote
favorite
I have a table named movement_history (that represenents movements of cars (register, sell, suspended, repaired, destroyed)). I want to get the information of a company
that has sold each cars and to who.
This is my table (select *)
code_car date_movement type_movement current_company_code
6000 01/01/2010 NEW 5
6000 01/01/2012 REPARATION 5
6000 01/11/2015 SOLD 8
6000 01/01/2017 DESTROYED 8
4444 01/05/2000 NEW 10
4444 01/05/2000 SUSPENDED 10
4444 01/05/2015 SOLD 5
4444 01/07/2015 RENOVATION 5
4444 09/12/2015 SOLD 18
.... ... ... ...
So if i want all the sales of cars from the company 5 in a determinated time (01-01-2015 to 31-02-2015) this would be the result:
code_car date_movement type_movement current_company_code
6000 01/11/2015 SOLD 8
4444 09/12/2015 SOLD 18
That's my query. First I obtain all the cars that someday have been from the company 5. Then I obtain all the 'SOLD' movements and I want to obtain for each car sold if some day has been part of the company 5.
select
code_car,date_movement,type_movement,current_company_code
from
movement_history where code_car in (
(select code_car from movement_history where current_company_code = 5)) and
code_car IN
(select code_car from movement_history where type_movement =
'SOLD' and code_car <> 5 and date_movement > to_Date('01-01-2015','dd/mm/yyyy') and date_movement < to_Date('31-12-2015','dd/mm/yyyy'));
I guess am doing badly the part to obtain if each car sold some day has been part of the company 5 al least one time.
Any suggestion? Thanks a lot.
sql oracle subquery
1
What date is "31-02-2015" supposed to represent?
– Gordon Linoff
Nov 10 at 17:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a table named movement_history (that represenents movements of cars (register, sell, suspended, repaired, destroyed)). I want to get the information of a company
that has sold each cars and to who.
This is my table (select *)
code_car date_movement type_movement current_company_code
6000 01/01/2010 NEW 5
6000 01/01/2012 REPARATION 5
6000 01/11/2015 SOLD 8
6000 01/01/2017 DESTROYED 8
4444 01/05/2000 NEW 10
4444 01/05/2000 SUSPENDED 10
4444 01/05/2015 SOLD 5
4444 01/07/2015 RENOVATION 5
4444 09/12/2015 SOLD 18
.... ... ... ...
So if i want all the sales of cars from the company 5 in a determinated time (01-01-2015 to 31-02-2015) this would be the result:
code_car date_movement type_movement current_company_code
6000 01/11/2015 SOLD 8
4444 09/12/2015 SOLD 18
That's my query. First I obtain all the cars that someday have been from the company 5. Then I obtain all the 'SOLD' movements and I want to obtain for each car sold if some day has been part of the company 5.
select
code_car,date_movement,type_movement,current_company_code
from
movement_history where code_car in (
(select code_car from movement_history where current_company_code = 5)) and
code_car IN
(select code_car from movement_history where type_movement =
'SOLD' and code_car <> 5 and date_movement > to_Date('01-01-2015','dd/mm/yyyy') and date_movement < to_Date('31-12-2015','dd/mm/yyyy'));
I guess am doing badly the part to obtain if each car sold some day has been part of the company 5 al least one time.
Any suggestion? Thanks a lot.
sql oracle subquery
I have a table named movement_history (that represenents movements of cars (register, sell, suspended, repaired, destroyed)). I want to get the information of a company
that has sold each cars and to who.
This is my table (select *)
code_car date_movement type_movement current_company_code
6000 01/01/2010 NEW 5
6000 01/01/2012 REPARATION 5
6000 01/11/2015 SOLD 8
6000 01/01/2017 DESTROYED 8
4444 01/05/2000 NEW 10
4444 01/05/2000 SUSPENDED 10
4444 01/05/2015 SOLD 5
4444 01/07/2015 RENOVATION 5
4444 09/12/2015 SOLD 18
.... ... ... ...
So if i want all the sales of cars from the company 5 in a determinated time (01-01-2015 to 31-02-2015) this would be the result:
code_car date_movement type_movement current_company_code
6000 01/11/2015 SOLD 8
4444 09/12/2015 SOLD 18
That's my query. First I obtain all the cars that someday have been from the company 5. Then I obtain all the 'SOLD' movements and I want to obtain for each car sold if some day has been part of the company 5.
select
code_car,date_movement,type_movement,current_company_code
from
movement_history where code_car in (
(select code_car from movement_history where current_company_code = 5)) and
code_car IN
(select code_car from movement_history where type_movement =
'SOLD' and code_car <> 5 and date_movement > to_Date('01-01-2015','dd/mm/yyyy') and date_movement < to_Date('31-12-2015','dd/mm/yyyy'));
I guess am doing badly the part to obtain if each car sold some day has been part of the company 5 al least one time.
Any suggestion? Thanks a lot.
sql oracle subquery
sql oracle subquery
edited Nov 10 at 18:20
a_horse_with_no_name
286k45429526
286k45429526
asked Nov 10 at 17:22
artyui berwaf
1
1
1
What date is "31-02-2015" supposed to represent?
– Gordon Linoff
Nov 10 at 17:24
add a comment |
1
What date is "31-02-2015" supposed to represent?
– Gordon Linoff
Nov 10 at 17:24
1
1
What date is "31-02-2015" supposed to represent?
– Gordon Linoff
Nov 10 at 17:24
What date is "31-02-2015" supposed to represent?
– Gordon Linoff
Nov 10 at 17:24
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
I see. The current_company_code
is the company that got the car. So, you want to use LAG()
:
select mh.*
from (select mh.*,
lag(mh.current_company_code) over (partition by mh.code_car order by date_movement) as prev_ccc
from movement_history mh
) mh
where mh.type_movement = 'SOLD' and
mh.date_movement >= date '2015-01-01' and
mh.date_movement < date '2016-01-01' and
mh.prev_ccc = 5;
Without using lag is ther any possibility?
– artyui berwaf
Nov 10 at 17:29
1
@artyuiberwaf . . .lag()
is the simplest solution and should have the best performance. I see no reason not to use it.
– Gordon Linoff
Nov 10 at 17:38
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I see. The current_company_code
is the company that got the car. So, you want to use LAG()
:
select mh.*
from (select mh.*,
lag(mh.current_company_code) over (partition by mh.code_car order by date_movement) as prev_ccc
from movement_history mh
) mh
where mh.type_movement = 'SOLD' and
mh.date_movement >= date '2015-01-01' and
mh.date_movement < date '2016-01-01' and
mh.prev_ccc = 5;
Without using lag is ther any possibility?
– artyui berwaf
Nov 10 at 17:29
1
@artyuiberwaf . . .lag()
is the simplest solution and should have the best performance. I see no reason not to use it.
– Gordon Linoff
Nov 10 at 17:38
add a comment |
up vote
1
down vote
I see. The current_company_code
is the company that got the car. So, you want to use LAG()
:
select mh.*
from (select mh.*,
lag(mh.current_company_code) over (partition by mh.code_car order by date_movement) as prev_ccc
from movement_history mh
) mh
where mh.type_movement = 'SOLD' and
mh.date_movement >= date '2015-01-01' and
mh.date_movement < date '2016-01-01' and
mh.prev_ccc = 5;
Without using lag is ther any possibility?
– artyui berwaf
Nov 10 at 17:29
1
@artyuiberwaf . . .lag()
is the simplest solution and should have the best performance. I see no reason not to use it.
– Gordon Linoff
Nov 10 at 17:38
add a comment |
up vote
1
down vote
up vote
1
down vote
I see. The current_company_code
is the company that got the car. So, you want to use LAG()
:
select mh.*
from (select mh.*,
lag(mh.current_company_code) over (partition by mh.code_car order by date_movement) as prev_ccc
from movement_history mh
) mh
where mh.type_movement = 'SOLD' and
mh.date_movement >= date '2015-01-01' and
mh.date_movement < date '2016-01-01' and
mh.prev_ccc = 5;
I see. The current_company_code
is the company that got the car. So, you want to use LAG()
:
select mh.*
from (select mh.*,
lag(mh.current_company_code) over (partition by mh.code_car order by date_movement) as prev_ccc
from movement_history mh
) mh
where mh.type_movement = 'SOLD' and
mh.date_movement >= date '2015-01-01' and
mh.date_movement < date '2016-01-01' and
mh.prev_ccc = 5;
answered Nov 10 at 17:25
Gordon Linoff
744k32285390
744k32285390
Without using lag is ther any possibility?
– artyui berwaf
Nov 10 at 17:29
1
@artyuiberwaf . . .lag()
is the simplest solution and should have the best performance. I see no reason not to use it.
– Gordon Linoff
Nov 10 at 17:38
add a comment |
Without using lag is ther any possibility?
– artyui berwaf
Nov 10 at 17:29
1
@artyuiberwaf . . .lag()
is the simplest solution and should have the best performance. I see no reason not to use it.
– Gordon Linoff
Nov 10 at 17:38
Without using lag is ther any possibility?
– artyui berwaf
Nov 10 at 17:29
Without using lag is ther any possibility?
– artyui berwaf
Nov 10 at 17:29
1
1
@artyuiberwaf . . .
lag()
is the simplest solution and should have the best performance. I see no reason not to use it.– Gordon Linoff
Nov 10 at 17:38
@artyuiberwaf . . .
lag()
is the simplest solution and should have the best performance. I see no reason not to use it.– Gordon Linoff
Nov 10 at 17:38
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241512%2fquery-subselect-at-least-one%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What date is "31-02-2015" supposed to represent?
– Gordon Linoff
Nov 10 at 17:24