SAP Query Example
SAP Query Example
ABAP Query
Contents
Learning the Basics of ABAP Query.....................................................................22 Creating a Report with ABAP Query .....................................................................27 Running the Query................................................................................................226 Understanding the Report List ............................................................................227 Getting the Most from ABAP Query: Tips & Tricks ...........................................227 Review....................................................................................................................233 Where to Learn More ............................................................................................233
In this chapter you will learn how to: Create a functional report in ABAP Query Build a basic functional area using a logical database Maintain a user group Create a report using a functional area and a user group
21
ABAP Query can be used to access just about any data in the database, making it very desirable for users in many areas of a business. Several R/3 application areas provide a predefined logical database for easier information retrieval.
22
ABAP Query Functional Functional Areas Areas Queries Queries User User Groups Groups
Functional Areas
You need functional areas to offer a preselected set of data to the user, depending on the task to be accomplished. A functional area defines the tablesand the fields within those tableswhich can be queried. Functional areas use a logical database, a direct read from a table, or a series of table joins to determine the available tables. Why create functional areas ABAP Query allows you to evaluate data in the R/3 System. However, since the system contains several hundred thousand fields in logical databases, in tables, and sequential datasets, it is simply not practical to offer all these fields to a user for selection when creating queries. Therefore, before starting to create queries (using the component Maintain Queries), you create functional areas (using the component Maintain Functional Areas). Functional areas provide the user with a framework for defining a query quickly and easily. Functional groups When you create a functional area, you select a logical database from an application system. However, since one logical database can contain a vast number of fields, you combine fields together in logical units known as functional groups (see page 25). A functional area not only allows you to restrict the number of fields and group them together in meaningful units, but it also allows you to define auxiliary fields and process them like database fields. You can also read the long texts in additional tables (for example, the long text of an airline carrier in the table SCARR) and perform any necessary preliminary work. This means that you can evaluate sequential datasets just as easily as SAP databases. Starting with Release 4.0, a functional area can have a name of up to 24-characters. Previous releases were limited to four-character names. SAP now delivers functional areas in various applications.
23
User Groups
User groups link users to functional areas and queries and control the functional areas and queries a user can access. Each functional area is assigned to one or more user groups. For access to ABAP Query, a user must be a member of at least one user group. All members of a user group can access the same data as well as the same programs (queries) to create lists. What is a user group? A user group is a collection of users that work with comparable data and accomplish similar tasks. Changes to functional areas and queries are immediately visible to all users. The users assigned to a user group can only access the functional areas assigned to their user group. When creating a query, the user group controls which functional areas can be used. (However, user groups are irrelevant if a user has full authorization to all R/3 data.) Starting with Release 4.0, user groups can have 12-character names. Prior releases were limited to two-character names.
Queries
When you create different lists using ABAP Query, you can save the layout for each list. The element created is called a query. A query is specific to a user group and functional area. When creating a query, you may only select one functional area. Within a query, you must also specify functional groups in the selected functional area. This means the selected area (and the groups) must contain all the fields you wish to include in the query. Using queries If appropriate authorizations are in place, you can execute, modify, copy, and delete queries. Any modifications to queries affect all users of a user group. That is, if one user deletes a query, it is lost for all other users as well. All changes or new creations are at once visible to all users. Starting with Release 4.0, queries can have 12-character names. Prior releases were limited to two-character names. Queries have three different types of reports or lists: [ [ [ Basic list: Sorts and totals selected fields as specified by the user in the query. This is the most common kind of report. Statistical list: Totals, calculates averages, and allocates percentages based on the fields in the query. Ranked list: Ranks fields based on numeric fields, including currency or quantity. A single query may have multiple statistical and ranked lists but only one basic list.
24
Related Terminology
[ [ Logical database: A series of tables that are linked together via a logical database program. Functional groups: Part of a functional area. You create functional groups within a functional area and then assign the fields you want to query to the functional group. Logical database To retrieve data, ABAP provides an extra read program called a logical database. This program reads the required data and provides it to you in the correct order. You only write the statements that process the data. To take advantage of this functionality for your report program, you must declare the logical database in the report attributes. SAP delivers several logical databases in the application areas and gives a user the capability of creating their own. We strongly recommend that a basis/tools expert be involved in the creation of a logical database. To view the delivered SAP logical databases, follow the menu path: Tools ABAP Workbench Development Programming Environment Logical Databases, or use transaction code SE36. Functional groups If a field is not assigned to a functional group, it cannot be included in a query. Functional groups have two-character names and are functional-area dependent. There are no naming restrictions for functional groups.
25
[ [
26
Note: In the example we use Release 4.0B. While this release has a different look-andfeel than prior releases, the underlying concepts and terminology remain the same.
27
The main steps in creating a query are shown in the graphic below.
Create Create a a functional area functional area Assign Assign required required fields fields to to functional functional groups groups Generate Generate the the functional functional area area
Assign Assign the the functional functional area area to to the the user user group group
Guided Tour
28
3. In Functional area, enter a name for the functional area (for example, ZZ_GL_DETAIL) you want to create. 4. Choose Create.
3 4
5. In the Name field enter a description for the functional area. 6. Leave Fixed point arithmetic selected to ensure that the decimal places are correctly set for the calculations.
6
7. In Logical data base, enter the name of the logical database you want to 9 use for the functional area (for example, SDF). 8. To display the tables and fields available in the logical database, choose Display. 9. Select Table join. Note: If you prefer to directly read a specific table or use a table join for multiple tables, then enter the name of the main table in the Table field and select either Direct read or Table join. Only one of two areas (steps 6 and 7 for logical databases or step 8 for specific tables) should be selected. 10. Choose Enter.
10
29
11. Choose the Create button (next to the Functional groups folder) to add two functional groups to the newly created functional area ZZ_GL_DETAIL.
11
12. Create the following two functional groups, A1 and A2 in the Create Functional Groups window: [ [ A1 for Account Master Info A2 for Account Transactions
12
13
210
211
The next step is to add an additional table that does not exist in the logical database. In this example, the G/L account description is not stored in any of the logical database tables; it is stored in table SKAT. We need to add table SKAT to table SKA1 (shown below) because all the key fields of table SKAT are available in table SKA1. 4. Click on the node for SKA1 to select the table in the Change Functional Area ZZ_GL_DETAIL screen. 5. Choose Extras in the toolbar to add an additional table.
6. Choose Create.
7. In Name, enter SKAT, the table to be added to the functional area. Leave Additional table selected. 8. Choose Enter. Note that the selected table is SKA1.
8
212
9. The system defaults to the SELECT statement values it can determine. You must complete the missing entries, if any. 10. Choose Enter twice to return to the Change Functional Area ZZ_GL_DETAIL screen.
10
11. Be sure to add the fields from the additional table to a functional group. The additional tables fields will be listed under the table to which it was attached. In this example, table SKAT is attached to table SKA1.
11
We will now look at the same logical database (SDF), but at a different table (BSEG). The next step is to add an additional fieldin this example, the dollar amount in table BSEG is stored as an absolute value. The online system uses the posting key to determine if the additional field is positive or negative. We need to add an additional field that would contain the amount with the proper sign so that the final totals are correctly calculated.
213
12. Select the line BSEG. 13. Choose the Extras button to add an additional field.
13
12
14
15. In the Name field, enter Correct_Amount, the name of the additional field to be added to the functional area. 16. Select Additional field. 17. Choose Enter.
17
15 16
214
18. In Description, enter an appropriate field description. 19. In Header, enter the column header text. 20. Enter the format of the field by either manually entering the type, length, output length, and decimals. Alternatively, you may specify the name of the field (for example, BSEG-DMBTR) whose formatting this additional field should mirror. 21. To enter the ABAP code for the additional field, choose Editor. 22. Enter the ABAP code that will determine the value of the additional field. In this example, 25 we are checking the debit/credit indicator (BSEG- SHKZG). If we find an 'S' for debit, then the correct amount is equal to the local currency amount (BSEG-DMBTR). If it is not a debit, then it is a credit and the local currency amount must be multiplied by -1.
23 24 20 21 18 19
22
Remember to enter the periods at the end of each line! 23. Choose Save. 24. Choose Back. 25. Choose Enter twice to return to the logical database view of the functional area.
215
26. Double-click on the functional group A2 to select it. 27. To add the additional field (CORRECT_AMOUNT) to a functional group so that it can be used in queries, expand the BSEG table to show all the fields. Scroll down to the bottom and select the CORRECT_AMOUNT field.
26
27
2 1
216
3. In the Users section, enter the user names of the users who should have access to the functional areas and queries of this user group. In this example, we added the user Tbecker to the user group. 4. Choose Assign funct. areas to assign the appropriate functional areas to the user group.
3
217
5. To assign the functional areas to the user group, select the appropriate checkboxes on the left side of the screen. In this example, we selected functional areas ZZGL and ZZ_GL_DETAIL. 6. Choose Save to save the assignments. 7. Choose Back twice to return to the main user group screen. Now that the functional area and user groups have been set up, you are ready to build the query.
5
218
5. You are now prompted to select a functional area. The list of functional areas includes all areas assigned to the user group you are working with. Double-click on the functional area you wish to use. In this example, double-click on ZZ_GL_DETAIL.
6. In Title, enter the title of the query, and in Notes enter any relevant notes. The notes are for reference only and do not appear in the 8 query. 7. In Columns, enter the column size (in characters). In Lines enter the desired number of lines. In this example, the number of lines and columns is 65 and 132 respectively. This should match a print format if the query is to be printed. 8. Choose Next Screen to continue.
219
9. Select the functional groups from which you want to choose fields. Remember that you are working with functional groups, not functional areas, in this step. 10. Choose Next screen to continue.
9
10
11. From each functional group, select the fields to be used in the query. 12. Use Page up or Page Down to scroll through the fields. In this example, the fields you select would be based on the desired report output (as shown in the sample report on page 27). 13. Choose Next Screen to continue.
11
13
12
220
14. From this list, select the fields that should be used as variables when executing the query. Some of these fields may already be variables in the logical database. If so, they will appear twice when executing the query. You may return to this screen and deselect them if they are duplicated. 15. If selection fields straddle multiple pages, use Page up or Page Down to scroll through the fields. 16. Choose Basic List to continue.
16 14
15
221
17. Enter the Line and Sequence for each field in the query. In the output for this sample report, recall that the account number and 23 description appear above the transaction detail. This is because 22 we specified that they would be on line 1. The transaction detail is on line 2. The sequence is the order in which the fields appear across the page. 18. Use Page up or Page Down to scroll through the remaining fields. To view a full page of fields, choose Expand. 19. Select and order the sort fields in the Sort column. 20. The Total button is available on all numeric, currency, and quantity fields. This gives you the option to total the sort fields. 21. The counter field is new in Release 4.0 and allows you to count the number of entries in your query for the selected field. This will also total on the sort fields. 22. The Compressed display checkbox allows detail transactions to be suppressed on the initial screen (and then expanded if necessary). With compression on, you will see the account number and a total for the account. When expanded you will see all the related transaction detail. 23. Choose Next Screen to continue.
17
19
20
21
18
222
This screen helps you set up the sort fields. 24. To get text above each sort break, select Text. 25. To get totals for a sort level, select Total. This option works only if you have also selected Total for a numeric field on the previous screen. 26. To get counter totals for the sort field, select Cnt. (counter). 27. To get a blank line between account numbers, select BlnkLn. 28. To get a page break at each new company code, select NewPg. 29. Choose Next Screen to continue.
29 24 25 26 27 28
30. Each sort field for which you selected Text, Total, or Cnt. above has a separate screen (as shown here). You can specify the text that should appear next to each total and above each sort break. 31. Choose Next Screen to continue. Choose this button through each of the control-level text screens.
31
30
223
32. Each detail line of your query will have a line on the List line output screen. To get a column header for the list line, select Header line. 33. Selecting Ref. checks the referenced line (for example, line 02) which is the detail transaction line for an activity. If there is no activity for line 02, then line 01 is suppressed as well. 34. If the detail lines for an account break across pages, selecting Page Header repeats the first line (with the account number and description) at the top of the next page. 35. Choose Next Screen to continue.
35 32 33 34
36. The query gives the standard length of the field. You now have the option of shortening it. 37. If the Pos fields are left blank, the default is one space between each field. Entries in the Pos column determine where in the line you want the field to start. In our example, we want the account number to start in character position 02 and the text in character position 12. This means there will be 4 blank spaces between the account number and text (12 - 2 6 = 4). 38. Choose Next Screen to continue.
38 36 37
Note: You must remember where the line breaks occur (for example between the G/L account long text and the accounting document number). The long text is on line 1 and the document number starts line 2.
224
39. This is the column header screen. To change a field header, doubleclick on the field header in the Page header section. In this example, we double-click on the Amount field.
39
40. Change the description in the popup box. 41. Choose Enter.
41
40
42. To enter a date, time, page, or name, put your cursor in the white line in the Page header section and type &%DATE, &%TIME, &%page, or the appropriate variable name. To insert additional lines, double42 click on the white line. 43. Choose Save. 44. Congratulations! Your query is now ready to run. Choose Execute to run the query.
43 44
225
3. Enter the variables. In this example, enter: 470000 in G/L Account 3000 in Company code 19.10.1998 in Open items at key date 4. Notice that the G/L account number and Company code fields appear twice because both the logical database and the query use these fields as selection criteria. Be sure to remove this duplication from the query. 5. Dynamic selections are also available because of the logical database. These selections should also be used instead of query selections. 6. Choose Execute to run the query.
5 6 3
3 3
226
227
Chapter 2: ABAP Query Getting the Most from ABAP Query: Tips & Tricks
The following example explains how to customize the variables from within the functional area. 1. From the SAP main menu choose System Services ABAP Query, then Tools ABAP Workbench Utilities ABAP Query Functional Areas. 2. In the Functional area field, enter the name of the functional area to be changed and click the Change button (not shown). 3. In the Change Functional Area screen, choose Selections (not shown). 4. If the functional area was created using a logical database, the database selection criteria will appear. Do not add these fields again or try to change them. If the functional area was created using table joins or direct table reads, this table will be empty which means there are no pre-defined selection criteria included in the functional area. You will need to manually add all the desired fields as selection criteria.
5. Choose Create to add a new field. 6. In the Create Selection window, enter the name of the new selection criteria. Use a name that starts with an alpha character.
6 7
7. Select Selection criterion. Since parameters only allow one value, this selection gives you the most flexibility to enter multiple values and ranges. 8. Choose Enter.
228
Chapter 2: ABAP Query Getting the Most from ABAP Query: Tips & Tricks
9 10 11
12
9. Enter the description and selection text for the field. In this example, Cost Center is added. 10. Enter the name of the field being added in the FOR statement. 11. In the Extras statement, enter any criteria for the field. In this example, the cost center field is made a mandatory field with matchcode capabilities. 12. Choose Enter.
229
Chapter 2: ABAP Query Getting the Most from ABAP Query: Tips & Tricks
3. In the Name field, enter a description for the functional area. 4. In the Table field, enter the name of the table (for example, MARD) with which you want to begin the table join. For best results, a high percentage of fields from the table you choose (for example, MARD) must also be present in the other table (for example, MARC). 5. Select Table join. 6. Choose Enter.
3
7. Enter the names of the other tables that are needed in the functional area. 8. Choose Define condition to define the link between the two selected tables.
230
Chapter 2: ABAP Query Getting the Most from ABAP Query: Tips & Tricks
9. The tables appear in the Join conditions (right side of the screen). 10. Choose Specify conditions to define the join.
10
11. You are prompted for proposals for the table join. Choose Yes and the system will propose the join conditions.
13
12
12. The proposed join between MARD and MARC is the material number and the plant. These are the two key fields on MARC. If both key fields can be matched (as in this case), then the system will always bring back one unique recordif a record is found.
231
Chapter 2: ABAP Query Getting the Most from ABAP Query: Tips & Tricks
If the proposal did not create the intended join conditions, you may want to define additional join conditions. To do this, enter matching numeric values in the two-character space next to each field. For example, if Ind.: Flag material on MARD should match the Indicator: Flag material on MARC, enter a 02 in the two-character space next to each of these fields. ABAP Query only allows join conditions between fields of a similar type. 13. Choose Back to go back one screen. 14. Choose Functional group on the next screen to begin adding fields to the functional groups from the adjoined tables.
232
[ [
While ABAP Query is an easy tool to work with, it is important to recognize that experience with the tool is valuable toward producing an efficient query. It is important to control the usage of the tool; not every user in a company should be given permission to create, change, or access queries. Controlled access to ABAP Query is recommended for optimal system performance and report organization. A reporting team should be formed to create and change queries. This team should be responsible for naming conventions, report creation, and testing. The team should ensure that duplicate functional areas, queries, and user groups are not created and that the standards are adhered to. A report specification form is a valuable tool. A sample form is included in the Fundamentals of Reporting guidebook and on the companion CD supplied with this guidebook set. This form serves as the key channel of communication between the reporting team and the end users that need reports.
Review
ABAP Query is an easy-to-use tool for extracting data from the R/3 System. It can be used with most tables, both standard and user-defined. With ABAP Query you can list data, total, and sort from almost any table or combination of tables. ABAP Query can create basic, statistical, and ranked lists. It works with logical databases, tables joins, or individual tables. Queries can be printed, downloaded, used with graphics, saved as extracts, or simply viewed on the screen. The following steps are needed to create a report with ABAP Query: 1. Create a functional area. 2. Assign required fields to functional groups. 3. Generate the functional area. 4. Create a user group. 5. Assign the functional area to the user group. 6. Create the query.
233
234