Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Problem with SQL Group by and having

Former Member
2,581

I Have a Problem with the Following SQL Command:

The following SQL-Command throws a Runtime-Error

"The expression that contains MSPR~PSPNR is not a GROUP-BY expression."!

The Problem is, wehn I put PSPNR into the Group-by Expression, I dont get the sum I needed. I want the sum of prlab obber all PSRNR group by watnr, werks, lgort, charg.

What am I doing wrong? Or is this kind of statement not possible in ABAP?


    SELECT DISTINCT mspr~matnr AS matnr , mspr~werks AS werks,
                    mspr~lgort AS lgort,  mspr~charg AS charg,
             SUM( mspr~prlab ) AS prlab",
      INTO TABLE @et_avail_proj_stock
        FROM mspr
      GROUP BY matnr, werks, lgort, charg

      HAVING mspr~pspnr IN ( SELECT  pspnr 
                              FROM  prps
                              WHERE psphi  = ( SELECT psphi
                                                FROM  prps
                                                WHERE pspnr = @iv_wbs_element ) )
      AND  mspr~sobkz  = @zif_pp_po=>mc_special_stock_project
      AND  prlab       <> @lv_initial_prlab.
6 REPLIES 6
Read only

RaymondGiuseppi
Active Contributor
0 Likes
2,098

The HAVING clause apply to the aggregated or group fields, so move other fields in a WHERE clause. Only aggregated field PRLAB should be in the HAVING clause?

Regards,
Raymond

Read only

Former Member
0 Likes
2,098

I am afraid where and Having in the same statement is not possible ( ? )

When theres a Group by there can only follow a "having" and never a "where" ( ? )

Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
2,098

No, that's not the case.

Read only

Former Member
0 Likes
2,098

I have got it !

You have helped me indeed.

This is the correct statement:

 SELECT mspr~matnr AS matnr, mspr~werks AS werks,
           mspr~lgort AS lgort, mspr~charg AS charg,
           SUM( mspr~prlab ) AS prlab
      INTO TABLE @et_avail_proj_stock
        FROM mspr
      WHERE mspr~pspnr IN ( SELECT  pspnr " ALL WBS Elements of the Project
                              FROM  prps
                              WHERE psphi  = ( SELECT psphi         
                                                FROM  prps
                                                WHERE pspnr = @iv_wbs_element ) )
      AND  mspr~sobkz  = @zif_pp_po=>mc_special_stock_project
      AND  prlab       <> @lv_initial_prlab
      GROUP BY matnr, werks, lgort, charg.
Read only

2,098

This give the correct result, actually the checked prlab is mspr~prlab for each record before aggregation, and correct only because mspr~prlab (some stock qty) is always positive, IMHO you could have written:

SELECT mspr~matnr AS matnr, mspr~werks AS werks,
           mspr~lgort AS lgort, mspr~charg AS charg,
           SUM( mspr~prlab ) AS prlab
      INTO TABLE @et_avail_proj_stock
        FROM mspr
      WHERE mspr~pspnr IN ( SELECT  pspnr " ALL WBS Elements of the Project
                              FROM  prps
                              WHERE psphi  = ( SELECT psphi         
                                                FROM  prps
                                                WHERE pspnr = @iv_wbs_element ) )
      AND  mspr~sobkz  = @zif_pp_po=>mc_special_stock_project
      GROUP BY matnr, werks, lgort, charg
      HAVING  prlab <> @lv_initial_prlab.

Regards,
Raymond