BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
user24feb
Barite | Level 11

I am trying to summarize data using proc sql. If I use this subquery, it gives me the desired result:

Proc SQL;

  Create Table Daten.Query_1_Test_C As

  Select Plant_Combined,

         KeyDate_Month_Key,

         MaterialClass_key,

         Sum(Quantity_OpenOrder_t_IsBillingNo) As Qty_OpenOrder_t Format=Commax15.3

  From

    (Select * From sqlserv.vFact_SalesOrder_DP 

     Where KeyDate_Month_Key in (201401 201402 201403) AND

           MaterialClass_key="JGG" AND

           Substr(Plant_Combined,1,6) in ("[0031]" "[0032]" "[0034]" "[0332]") AND

           IsFutureDeliveryDate eq 1 AND

     Ultimo_Filter_Reporting eq 1) 

  Group By Plant_Combined,

           MaterialClass_key,

           KeyDate_Month_Key  

  Order By KeyDate_Month_Key,          

           Plant_Combined,

           MaterialClass_key

   ;

Quit;

What I cannot understand though, is why this is not the same as:

Proc SQL;

  Create Table Daten.Query_1_Test_B As

  Select Distinct Plant_Combined,

         KeyDate_Month_Key,

         MaterialClass_key,

         Sum(Quantity_OpenOrder_t_IsBillingNo) As Qty_OpenOrder_t Format=Commax15.3

  From sqlserv.vFact_SalesOrder_DP 

  Where IsFutureDeliveryDate eq 1 AND

         Ultimo_Filter_Reporting eq 1

  Group By Plant_Combined,

           MaterialClass_key,

           KeyDate_Month_Key

  Having KeyDate_Month_Key in (201401 201402 201403) AND

         MaterialClass_key="JGG" AND

         Substr(Plant_Combined,1,6) in ("[0031]" "[0032]" "[0034]" "[0332]")        

  Order By KeyDate_Month_Key,          

        Plant_Combined,

        MaterialClass_key

   ;

Quit;

Shouldn't "where" and "having" do the job of a subquery?

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

What you are seeing is trying to do filters on data which is being grouped.  The order by is probably messing with things as well -> so you are trying to sequence the data by something other than the grouping info.  Put simply:

select ...

from          (get your data with your filters)

group by KeyDate_Month_Key,          /* This will be ordered like this also */

               Plant_Combined,

               MatierialClass_Key;

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, the simple answer is you can't use where clauses with aggregate functions.  Hence Having was added to cover that hole.

user24feb
Barite | Level 11

Thanks for your quick reply. Smiley Happy

.. if I put the where part to the having part (and remove where completely) it still gives a weird result. Any ideas what is wrong with this code?

Proc SQL;

  Create Table Daten.Query_1_Test_B As

  Select Distinct Plant_Combined,

         KeyDate_Month_Key,

         MaterialClass_key,

         Sum(Quantity_OpenOrder_t_IsBillingNo) As Qty_OpenOrder_t Format=Commax15.3

  From sqlserv.vFact_SalesOrder_DP 

  Group By Plant_Combined,

           MaterialClass_key,

           KeyDate_Month_Key

  Having KeyDate_Month_Key in (201401 201402 201403) AND

         MaterialClass_key="JGG" AND

         Substr(Plant_Combined,1,6) in ("[0031]" "[0032]" "[0034]" "[0332]") AND       

         IsFutureDeliveryDate eq 1 AND

          Ultimo_Filter_Reporting eq 1       

  Order By KeyDate_Month_Key,          

        Plant_Combined,

        MaterialClass_key

   ;

Quit;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

What you are seeing is trying to do filters on data which is being grouped.  The order by is probably messing with things as well -> so you are trying to sequence the data by something other than the grouping info.  Put simply:

select ...

from          (get your data with your filters)

group by KeyDate_Month_Key,          /* This will be ordered like this also */

               Plant_Combined,

               MatierialClass_Key;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 746 views
  • 3 likes
  • 2 in conversation