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.










share|improve this question



















  • 1




    What date is "31-02-2015" supposed to represent?
    – Gordon Linoff
    Nov 10 at 17:24














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.










share|improve this question



















  • 1




    What date is "31-02-2015" supposed to represent?
    – Gordon Linoff
    Nov 10 at 17:24












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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












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;





share|improve this answer




















  • 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










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%2f53241512%2fquery-subselect-at-least-one%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























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;





share|improve this answer




















  • 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














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;





share|improve this answer




















  • 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












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;





share|improve this answer












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;






share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線