Getting a query result taken from the same data but with temporary var












1














I got a simple thing to do.
Well, maybe not, but someone somewhere surely can help me out : P



I got a simple data structure that contains




  • expedition date


  • delivery date


  • transaction type



I would need to create a query which could
order the rows by a date specific to the transaction type.



(ie : using the expedition date for transaction of type "selling", and delivery date for transaction of type "purchasing")



I was wondering if there was a more efficient way to do this than
by fetching 2 times the same data with different clause where(while adding a column used to order them(tempDate)) and then using another select to encompass these 2 queries to which I would add the order clause on the tempDate.



--> the initial fetching I would do 2 times works on many tables(many, many, many joins)



Basically my current solution is :



Select * from 

(

Select ...

date_exp as dateTemp;

from ...

where conditions* And dateRelatedCondition

UNION

Select ...

date_livraison as dateTemp;

from ...

Where conditions* And NOT(dateRelatedCondition)

) as comboSelect

Order By MIN(comboSelect.dateTemp)
OVER(PARTITION BY(REF_product)),
(REF_product),
comboSelect.dateTemp asc;


*
->Those conditions are the same in both inner Select query



Thank you for your time.










share|improve this question





























    1














    I got a simple thing to do.
    Well, maybe not, but someone somewhere surely can help me out : P



    I got a simple data structure that contains




    • expedition date


    • delivery date


    • transaction type



    I would need to create a query which could
    order the rows by a date specific to the transaction type.



    (ie : using the expedition date for transaction of type "selling", and delivery date for transaction of type "purchasing")



    I was wondering if there was a more efficient way to do this than
    by fetching 2 times the same data with different clause where(while adding a column used to order them(tempDate)) and then using another select to encompass these 2 queries to which I would add the order clause on the tempDate.



    --> the initial fetching I would do 2 times works on many tables(many, many, many joins)



    Basically my current solution is :



    Select * from 

    (

    Select ...

    date_exp as dateTemp;

    from ...

    where conditions* And dateRelatedCondition

    UNION

    Select ...

    date_livraison as dateTemp;

    from ...

    Where conditions* And NOT(dateRelatedCondition)

    ) as comboSelect

    Order By MIN(comboSelect.dateTemp)
    OVER(PARTITION BY(REF_product)),
    (REF_product),
    comboSelect.dateTemp asc;


    *
    ->Those conditions are the same in both inner Select query



    Thank you for your time.










    share|improve this question



























      1












      1








      1







      I got a simple thing to do.
      Well, maybe not, but someone somewhere surely can help me out : P



      I got a simple data structure that contains




      • expedition date


      • delivery date


      • transaction type



      I would need to create a query which could
      order the rows by a date specific to the transaction type.



      (ie : using the expedition date for transaction of type "selling", and delivery date for transaction of type "purchasing")



      I was wondering if there was a more efficient way to do this than
      by fetching 2 times the same data with different clause where(while adding a column used to order them(tempDate)) and then using another select to encompass these 2 queries to which I would add the order clause on the tempDate.



      --> the initial fetching I would do 2 times works on many tables(many, many, many joins)



      Basically my current solution is :



      Select * from 

      (

      Select ...

      date_exp as dateTemp;

      from ...

      where conditions* And dateRelatedCondition

      UNION

      Select ...

      date_livraison as dateTemp;

      from ...

      Where conditions* And NOT(dateRelatedCondition)

      ) as comboSelect

      Order By MIN(comboSelect.dateTemp)
      OVER(PARTITION BY(REF_product)),
      (REF_product),
      comboSelect.dateTemp asc;


      *
      ->Those conditions are the same in both inner Select query



      Thank you for your time.










      share|improve this question















      I got a simple thing to do.
      Well, maybe not, but someone somewhere surely can help me out : P



      I got a simple data structure that contains




      • expedition date


      • delivery date


      • transaction type



      I would need to create a query which could
      order the rows by a date specific to the transaction type.



      (ie : using the expedition date for transaction of type "selling", and delivery date for transaction of type "purchasing")



      I was wondering if there was a more efficient way to do this than
      by fetching 2 times the same data with different clause where(while adding a column used to order them(tempDate)) and then using another select to encompass these 2 queries to which I would add the order clause on the tempDate.



      --> the initial fetching I would do 2 times works on many tables(many, many, many joins)



      Basically my current solution is :



      Select * from 

      (

      Select ...

      date_exp as dateTemp;

      from ...

      where conditions* And dateRelatedCondition

      UNION

      Select ...

      date_livraison as dateTemp;

      from ...

      Where conditions* And NOT(dateRelatedCondition)

      ) as comboSelect

      Order By MIN(comboSelect.dateTemp)
      OVER(PARTITION BY(REF_product)),
      (REF_product),
      comboSelect.dateTemp asc;


      *
      ->Those conditions are the same in both inner Select query



      Thank you for your time.







      sql sql-server tsql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 19:48

























      asked Nov 20 at 17:27









      Master Pain

      104




      104
























          2 Answers
          2






          active

          oldest

          votes


















          0















          • Without the UNION:

            dateRelatedCondition should be removed from WHERE and put to the SELECT like:


          CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END as dateTemp



          • Without the subquery:

            in ORDER BY you need the same expression in the window function:


          Order By MIN(CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END)  
          OVER(PARTITION BY(REF_product)),
          (REF_product),
          dateTemp asc





          share|improve this answer























          • There are some data I've not given. The query ending is ORDER BY Min(tempDate) OVER(PARTITION BY( rowRef_Id )), rowRef_Id, tempDate asc;
            – Master Pain
            Nov 20 at 19:38










          • OK, I update my answer with your expression from the question.
            – Dávid Laczkó
            Nov 20 at 19:49










          • Can't seem to access the dateTemp variable in the Order By clause. Invalid Column Name.
            – Master Pain
            Nov 20 at 20:00










          • You might still need to do a subquery first with the CASE expression and only then do the ordering in the outer query. Or put the CASE expression in place of the dateTemp - but this one won't look very nice and could be hard to be revised later.
            – Dávid Laczkó
            Nov 20 at 20:03












          • Had to keep the first Select clause to be able to use the "comboSelect" to get to the variable for it to work. So something like Select ... from ( Select )) as comboSelect Order By ... Got a better solution ?
            – Master Pain
            Nov 20 at 20:14



















          0














          You mean like this?:



          ORDER BY CASE
          WHEN TransactionType = 'Selling' THEN ExpeditionDate
          WHEN TransactionType = 'purchasing' THEN DeliveryDate
          END





          share|improve this answer





















          • Sorry, had forgot to add some of the Order by clause... Didn't think it would matter. I will need the variable dateTemp to order the rows at the end
            – Master Pain
            Nov 20 at 19:50










          • Ok, so after the code in my answer, add , dateTemp. If I understand you correctly.
            – Tab Alleman
            Nov 20 at 20:01










          • Works only if I end the Order By clause like this ORDER BY MIN( CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END) OVER(PARTITION BY(REF_product)), (REF_product), CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END asc.
            – Master Pain
            Nov 20 at 20:28











          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',
          autoActivateHeartbeat: false,
          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%2f53398388%2fgetting-a-query-result-taken-from-the-same-data-but-with-temporary-var%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0















          • Without the UNION:

            dateRelatedCondition should be removed from WHERE and put to the SELECT like:


          CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END as dateTemp



          • Without the subquery:

            in ORDER BY you need the same expression in the window function:


          Order By MIN(CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END)  
          OVER(PARTITION BY(REF_product)),
          (REF_product),
          dateTemp asc





          share|improve this answer























          • There are some data I've not given. The query ending is ORDER BY Min(tempDate) OVER(PARTITION BY( rowRef_Id )), rowRef_Id, tempDate asc;
            – Master Pain
            Nov 20 at 19:38










          • OK, I update my answer with your expression from the question.
            – Dávid Laczkó
            Nov 20 at 19:49










          • Can't seem to access the dateTemp variable in the Order By clause. Invalid Column Name.
            – Master Pain
            Nov 20 at 20:00










          • You might still need to do a subquery first with the CASE expression and only then do the ordering in the outer query. Or put the CASE expression in place of the dateTemp - but this one won't look very nice and could be hard to be revised later.
            – Dávid Laczkó
            Nov 20 at 20:03












          • Had to keep the first Select clause to be able to use the "comboSelect" to get to the variable for it to work. So something like Select ... from ( Select )) as comboSelect Order By ... Got a better solution ?
            – Master Pain
            Nov 20 at 20:14
















          0















          • Without the UNION:

            dateRelatedCondition should be removed from WHERE and put to the SELECT like:


          CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END as dateTemp



          • Without the subquery:

            in ORDER BY you need the same expression in the window function:


          Order By MIN(CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END)  
          OVER(PARTITION BY(REF_product)),
          (REF_product),
          dateTemp asc





          share|improve this answer























          • There are some data I've not given. The query ending is ORDER BY Min(tempDate) OVER(PARTITION BY( rowRef_Id )), rowRef_Id, tempDate asc;
            – Master Pain
            Nov 20 at 19:38










          • OK, I update my answer with your expression from the question.
            – Dávid Laczkó
            Nov 20 at 19:49










          • Can't seem to access the dateTemp variable in the Order By clause. Invalid Column Name.
            – Master Pain
            Nov 20 at 20:00










          • You might still need to do a subquery first with the CASE expression and only then do the ordering in the outer query. Or put the CASE expression in place of the dateTemp - but this one won't look very nice and could be hard to be revised later.
            – Dávid Laczkó
            Nov 20 at 20:03












          • Had to keep the first Select clause to be able to use the "comboSelect" to get to the variable for it to work. So something like Select ... from ( Select )) as comboSelect Order By ... Got a better solution ?
            – Master Pain
            Nov 20 at 20:14














          0












          0








          0







          • Without the UNION:

            dateRelatedCondition should be removed from WHERE and put to the SELECT like:


          CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END as dateTemp



          • Without the subquery:

            in ORDER BY you need the same expression in the window function:


          Order By MIN(CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END)  
          OVER(PARTITION BY(REF_product)),
          (REF_product),
          dateTemp asc





          share|improve this answer















          • Without the UNION:

            dateRelatedCondition should be removed from WHERE and put to the SELECT like:


          CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END as dateTemp



          • Without the subquery:

            in ORDER BY you need the same expression in the window function:


          Order By MIN(CASE WHEN dateRelatedCondition THEN date_exp ELSE date_livraison END)  
          OVER(PARTITION BY(REF_product)),
          (REF_product),
          dateTemp asc






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 21:10

























          answered Nov 20 at 19:13









          Dávid Laczkó

          404128




          404128












          • There are some data I've not given. The query ending is ORDER BY Min(tempDate) OVER(PARTITION BY( rowRef_Id )), rowRef_Id, tempDate asc;
            – Master Pain
            Nov 20 at 19:38










          • OK, I update my answer with your expression from the question.
            – Dávid Laczkó
            Nov 20 at 19:49










          • Can't seem to access the dateTemp variable in the Order By clause. Invalid Column Name.
            – Master Pain
            Nov 20 at 20:00










          • You might still need to do a subquery first with the CASE expression and only then do the ordering in the outer query. Or put the CASE expression in place of the dateTemp - but this one won't look very nice and could be hard to be revised later.
            – Dávid Laczkó
            Nov 20 at 20:03












          • Had to keep the first Select clause to be able to use the "comboSelect" to get to the variable for it to work. So something like Select ... from ( Select )) as comboSelect Order By ... Got a better solution ?
            – Master Pain
            Nov 20 at 20:14


















          • There are some data I've not given. The query ending is ORDER BY Min(tempDate) OVER(PARTITION BY( rowRef_Id )), rowRef_Id, tempDate asc;
            – Master Pain
            Nov 20 at 19:38










          • OK, I update my answer with your expression from the question.
            – Dávid Laczkó
            Nov 20 at 19:49










          • Can't seem to access the dateTemp variable in the Order By clause. Invalid Column Name.
            – Master Pain
            Nov 20 at 20:00










          • You might still need to do a subquery first with the CASE expression and only then do the ordering in the outer query. Or put the CASE expression in place of the dateTemp - but this one won't look very nice and could be hard to be revised later.
            – Dávid Laczkó
            Nov 20 at 20:03












          • Had to keep the first Select clause to be able to use the "comboSelect" to get to the variable for it to work. So something like Select ... from ( Select )) as comboSelect Order By ... Got a better solution ?
            – Master Pain
            Nov 20 at 20:14
















          There are some data I've not given. The query ending is ORDER BY Min(tempDate) OVER(PARTITION BY( rowRef_Id )), rowRef_Id, tempDate asc;
          – Master Pain
          Nov 20 at 19:38




          There are some data I've not given. The query ending is ORDER BY Min(tempDate) OVER(PARTITION BY( rowRef_Id )), rowRef_Id, tempDate asc;
          – Master Pain
          Nov 20 at 19:38












          OK, I update my answer with your expression from the question.
          – Dávid Laczkó
          Nov 20 at 19:49




          OK, I update my answer with your expression from the question.
          – Dávid Laczkó
          Nov 20 at 19:49












          Can't seem to access the dateTemp variable in the Order By clause. Invalid Column Name.
          – Master Pain
          Nov 20 at 20:00




          Can't seem to access the dateTemp variable in the Order By clause. Invalid Column Name.
          – Master Pain
          Nov 20 at 20:00












          You might still need to do a subquery first with the CASE expression and only then do the ordering in the outer query. Or put the CASE expression in place of the dateTemp - but this one won't look very nice and could be hard to be revised later.
          – Dávid Laczkó
          Nov 20 at 20:03






          You might still need to do a subquery first with the CASE expression and only then do the ordering in the outer query. Or put the CASE expression in place of the dateTemp - but this one won't look very nice and could be hard to be revised later.
          – Dávid Laczkó
          Nov 20 at 20:03














          Had to keep the first Select clause to be able to use the "comboSelect" to get to the variable for it to work. So something like Select ... from ( Select )) as comboSelect Order By ... Got a better solution ?
          – Master Pain
          Nov 20 at 20:14




          Had to keep the first Select clause to be able to use the "comboSelect" to get to the variable for it to work. So something like Select ... from ( Select )) as comboSelect Order By ... Got a better solution ?
          – Master Pain
          Nov 20 at 20:14













          0














          You mean like this?:



          ORDER BY CASE
          WHEN TransactionType = 'Selling' THEN ExpeditionDate
          WHEN TransactionType = 'purchasing' THEN DeliveryDate
          END





          share|improve this answer





















          • Sorry, had forgot to add some of the Order by clause... Didn't think it would matter. I will need the variable dateTemp to order the rows at the end
            – Master Pain
            Nov 20 at 19:50










          • Ok, so after the code in my answer, add , dateTemp. If I understand you correctly.
            – Tab Alleman
            Nov 20 at 20:01










          • Works only if I end the Order By clause like this ORDER BY MIN( CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END) OVER(PARTITION BY(REF_product)), (REF_product), CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END asc.
            – Master Pain
            Nov 20 at 20:28
















          0














          You mean like this?:



          ORDER BY CASE
          WHEN TransactionType = 'Selling' THEN ExpeditionDate
          WHEN TransactionType = 'purchasing' THEN DeliveryDate
          END





          share|improve this answer





















          • Sorry, had forgot to add some of the Order by clause... Didn't think it would matter. I will need the variable dateTemp to order the rows at the end
            – Master Pain
            Nov 20 at 19:50










          • Ok, so after the code in my answer, add , dateTemp. If I understand you correctly.
            – Tab Alleman
            Nov 20 at 20:01










          • Works only if I end the Order By clause like this ORDER BY MIN( CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END) OVER(PARTITION BY(REF_product)), (REF_product), CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END asc.
            – Master Pain
            Nov 20 at 20:28














          0












          0








          0






          You mean like this?:



          ORDER BY CASE
          WHEN TransactionType = 'Selling' THEN ExpeditionDate
          WHEN TransactionType = 'purchasing' THEN DeliveryDate
          END





          share|improve this answer












          You mean like this?:



          ORDER BY CASE
          WHEN TransactionType = 'Selling' THEN ExpeditionDate
          WHEN TransactionType = 'purchasing' THEN DeliveryDate
          END






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 19:09









          Tab Alleman

          25.7k52440




          25.7k52440












          • Sorry, had forgot to add some of the Order by clause... Didn't think it would matter. I will need the variable dateTemp to order the rows at the end
            – Master Pain
            Nov 20 at 19:50










          • Ok, so after the code in my answer, add , dateTemp. If I understand you correctly.
            – Tab Alleman
            Nov 20 at 20:01










          • Works only if I end the Order By clause like this ORDER BY MIN( CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END) OVER(PARTITION BY(REF_product)), (REF_product), CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END asc.
            – Master Pain
            Nov 20 at 20:28


















          • Sorry, had forgot to add some of the Order by clause... Didn't think it would matter. I will need the variable dateTemp to order the rows at the end
            – Master Pain
            Nov 20 at 19:50










          • Ok, so after the code in my answer, add , dateTemp. If I understand you correctly.
            – Tab Alleman
            Nov 20 at 20:01










          • Works only if I end the Order By clause like this ORDER BY MIN( CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END) OVER(PARTITION BY(REF_product)), (REF_product), CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END asc.
            – Master Pain
            Nov 20 at 20:28
















          Sorry, had forgot to add some of the Order by clause... Didn't think it would matter. I will need the variable dateTemp to order the rows at the end
          – Master Pain
          Nov 20 at 19:50




          Sorry, had forgot to add some of the Order by clause... Didn't think it would matter. I will need the variable dateTemp to order the rows at the end
          – Master Pain
          Nov 20 at 19:50












          Ok, so after the code in my answer, add , dateTemp. If I understand you correctly.
          – Tab Alleman
          Nov 20 at 20:01




          Ok, so after the code in my answer, add , dateTemp. If I understand you correctly.
          – Tab Alleman
          Nov 20 at 20:01












          Works only if I end the Order By clause like this ORDER BY MIN( CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END) OVER(PARTITION BY(REF_product)), (REF_product), CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END asc.
          – Master Pain
          Nov 20 at 20:28




          Works only if I end the Order By clause like this ORDER BY MIN( CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END) OVER(PARTITION BY(REF_product)), (REF_product), CASE WHEN TransactionType = 'Selling' THEN ExpeditionDate WHEN TransactionType = 'purchasing' THEN DeliveryDate END asc.
          – Master Pain
          Nov 20 at 20:28


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398388%2fgetting-a-query-result-taken-from-the-same-data-but-with-temporary-var%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

          Costa Masnaga

          Fotorealismo

          Sidney Franklin