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

Select Group By problem.

Former Member
0 Likes
606

Hi;

1) QUESTION.

I have the next select and i get 2 lines of the same document number.

this example is an extract of all the data i get in each line of the select:

AVBELN BFKIMG BKZWI1 EVERPR CPERNR CKUNNR

0090000023 2.000 1586.20 608.10 00000000 0010000287

0090000023 2.000 1586.20 608.10 00000015

All the field are equal the only diference are CPERNR CKUNNR, because of them is that i have 2 lines instead of one.

This is the actual SELECT:

DATA: BEGIN OF itab OCCURS 0,

sociedad LIKE vbrk-bukrs,

centro LIKE vbrp-werks,

provee LIKE mara-extwg,

material LIKE vbrp-matnr,

descri LIKE vbrp-arktx,

unidad LIKE vbrp-vrkme,

codigo LIKE vbrk-kunag,

codcli LIKE kna1-kunnr,

nomcli LIKE kna1-name1,

direc LIKE kna1-ort01,

docum LIKE vbrk-vbeln,

cantidad LIKE vbrp-fkimg,

monto LIKE vbrp-kzwi1,

costo LIKE mbew-verpr,

vendedor LIKE vbpa-pernr,

destina LIKE vbpa-kunnr,

zona LIKE vbrp-bzirk_auft,

szona LIKE vbrp-kdgrp_auft,

pais LIKE vbrp-lland_auft,

depto LIKE vbrp-regio_auft,

orgvtas LIKE vbrp-vkorg_auft,

canal LIKE vbrp-vtweg_auft,

sector LIKE vbrp-spart,

oficina LIKE vbrp-vkbur,

tipodoc LIKE vbrk-vbtyp,

fecha LIKE vbrk-fkdat,

catego LIKE mara-matkl,

matprov LIKE eina-idnlf.

DATA: END OF itab.

SELECT abukrs bwerks dextwg bmatnr barktx bvrkme a~kunag

gkunnr gname1 gort01 avbeln bfkimg bkzwi1 everpr cpernr

ckunnr bbzirk_auft bkdgrp_auft blland_auft b~regio_auft

bvkorg_auft bvtweg_auft bspart bvkbur avbtyp afkdat dmatkl fidnlf

INTO TABLE itab FROM

( ( ( ( ( ( vbrk AS a

INNER JOIN vbrp AS b ON avbeln = bvbeln )

INNER JOIN vbpa AS c ON bvbeln = cvbeln )

INNER JOIN mara AS d ON bmatnr = dmatnr )

INNER JOIN mbew AS e ON bmatnr = ematnr )

INNER JOIN eina AS f ON bmatnr = fmatnr )

INNER JOIN kna1 AS g ON akunag = gkunnr )

WHERE a~bukrs = psocie AND

a~erdat IN pfecha AND

b~werks = pcentro AND

e~bwkey = pcentro AND

e~vmvpr = 'VE' AND

c~parvw NOT IN ('AG','RE','RG').

SORT itab BY material docum.

What i want is to GROUP this data to get one single line for each document number.

i tryied this but it doesn´t work:

SELECT abukrs bwerks dextwg bmatnr barktx bvrkme a~kunag

gkunnr gname1 gort01 avbeln bfkimg bkzwi1 everpr cpernr

ckunnr bbzirk_auft bkdgrp_auft blland_auft b~regio_auft

bvkorg_auft bvtweg_auft bspart bvkbur avbtyp afkdat dmatkl fidnlf

INTO TABLE itab FROM

( ( ( ( ( ( vbrk AS a

INNER JOIN vbrp AS b ON avbeln = bvbeln )

INNER JOIN vbpa AS c ON bvbeln = cvbeln )

INNER JOIN mara AS d ON bmatnr = dmatnr )

INNER JOIN mbew AS e ON bmatnr = ematnr )

INNER JOIN eina AS f ON bmatnr = fmatnr )

INNER JOIN kna1 AS g ON akunag = gkunnr )

WHERE a~bukrs = psocie AND

a~erdat IN pfecha AND

b~werks = pcentro AND

e~bwkey = pcentro AND

e~vmvpr = 'VE' AND

c~parvw NOT IN ('AG','RE','RG') GROUP BY

abukrs bwerks dextwg bmatnr barktx bvrkme a~kunag

gkunnr gname1 gort01 avbeln bfkimg bkzwi1 everpr cpernr

ckunnr bbzirk_auft bkdgrp_auft blland_auft b~regio_auft

bvkorg_auft bvtweg_auft bspart bvkbur avbtyp afkdat dmatkl fidnlf.

It doesn't group the lines in one.

2) QUESTION

Why i can´t do a SELECT over an Internal Table as the one i declared in the QUESTION # 1...?

Thaks on advance.

David Fúnez
Tegucigalpa, Honduras
Corp. Mandofer

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
546

Here is one simple suggestion :

I will not write any select query with more than 3 tables join,even if you want to write,take 3 tables as one join and remain would be for all entries .

as you mentioned group by

here i am writing group by in one of my program :

select matnr sum( vsolm ) from zrfltap into table i_tsold

where matnr in so_matnr

and vdatu in so_vdatu

and werks = p_werks

group by matnr.

sort i_tsold by matnr.

I hope you got it.

Thanks

Seshu

3 REPLIES 3
Read only

Former Member
0 Likes
546

Hi David,

1. Try to reduce the tables in the JOIN so that the code is more maintainable.

Include all the key fields from all the tables in the select.

2. You can't do SELECT on internal table, instead you can use READ or LOOP on the internal table.

Regards,

Atish

Read only

Former Member
0 Likes
546

"It doesn't group the lines in one" ???

You are expecting one line for each document number knowing that you are selecting MATNR from VBRP???

An invoice can (and usually) will have multiple line items. How are expecting to condense multiple, unique line items (by MATNR) into one line?

Please clarify your business requirement(s).

Read only

Former Member
0 Likes
547

Here is one simple suggestion :

I will not write any select query with more than 3 tables join,even if you want to write,take 3 tables as one join and remain would be for all entries .

as you mentioned group by

here i am writing group by in one of my program :

select matnr sum( vsolm ) from zrfltap into table i_tsold

where matnr in so_matnr

and vdatu in so_vdatu

and werks = p_werks

group by matnr.

sort i_tsold by matnr.

I hope you got it.

Thanks

Seshu