Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Fetch unique records from table in SAP ABAP
When working with ABAP and SQL, you typically use OpenSQL for your SQL activities. To fetch unique records from a table, you can eliminate duplicate rows using the DISTINCT operator.
Using DISTINCT Operator
The DISTINCT keyword ensures that only unique records are returned from your query. This is particularly useful when you have duplicate entries in your table and need to retrieve only distinct values ?
SELECT DISTINCT field1, field2 FROM table_name WHERE condition.
Example
Here's a practical example showing how to select unique customer names from a customer table ?
DATA: lt_customers TYPE TABLE OF string,
lv_name TYPE string.
SELECT DISTINCT name
FROM zcustomer_table
INTO TABLE lt_customers
WHERE city = 'New York'.
LOOP AT lt_customers INTO lv_name.
WRITE: / lv_name.
ENDLOOP.
Multiple Fields with DISTINCT
You can also use DISTINCT with multiple fields. The combination of all specified fields must be unique ?
SELECT DISTINCT name, city, country FROM zcustomer_table INTO TABLE @DATA(lt_unique_customers) WHERE active = 'X'.
This query returns unique combinations of name, city, and country fields where the customer is active.
Conclusion
The DISTINCT operator in SAP ABAP is an efficient way to retrieve unique records from database tables, helping you eliminate duplicates and work with clean, distinct data sets.
