Hi All ,
we require to have query to show the data with groupwise data in the query. can anyone suggest with sample query .
MYQUUERY: select * from da5.SwitchPrice as S inner join da5.SwitchPriceGroup as G on S.PSGID=G.PSGID;
Result we got :
Barcode SwitchPrice PSGID PSGroup
______________________________________________
A01 Price2 1 LUNCH
A02 Price3 1 LUNCH
A03 Price4 1 LUNCH
A04 Price3 2 EVENING
Result we desire to get :
Barcode SwitchPrice PSGID
________________________________________________
PSGROUP :LUNCH
A01 Price2 1
A02 Price3 1
A03 Price4 1
PSGROUP :EVENING
A04 Price3 2
Thanks in advance

Tuhin PaulPosted Feb 28, 2025, 12:15 PM
If you need the exact hierarchical structure (with group names as headers), it's better to handle this in your application code.
Tuhin PaulPosted Feb 28, 2025, 12:15 PM
The query will produce the following result:
Header
Barcode
SwitchPrice
PSGID
PSGROUP :LUNCH
A01
Price2
1
A02
Price3
1
A03
Price4
1
PSGROUP :EVENING
A04
Price3
2
PSGroup), the first row will have the group name in theHeadercolumn.Headercolumn.ORDER BYclause ensures that the rows are grouped and sorted correctly.Limitations:
Formatting :
Headercolumn contains the group name, and subsequent rows leave it blank.Application-Level Formatting :
Tuhin PaulPosted Feb 28, 2025, 12:14 PM
To achieve the desired output where the
PSGROUPname acts as a header followed by its corresponding rows, SQL alone cannot directly produce hierarchical or formatted outputs like this. However, you can simulate this behavior using SQL by introducing a "header row" for each group.ROW_NUMBER() Function :
ROW_NUMBER()function assigns a unique number to each row within a group (PARTITION BY G.PSGroup) based on the specified order (ORDER BY S.Barcode).ROW_NUMBER() = 1), we display the group name (PSGROUP :). Otherwise, we leave theHeadercolumn blank.CASE Statement :
CASEstatement checks if the current row is the first row in its group. If it is, it displays the group name (PSGROUP :). Otherwise, it leaves theHeadercolumn empty.Concatenation :
CONCATfunction is used to prependPSGROUP :to the group name.Ordering :
ORDER BY G.PSGroup, S.Barcodeensures that rows are grouped byPSGroupand sorted byBarcode.Eliana BlakePosted Feb 28, 2025, 2:34 AM
To achieve the desired result of grouping data based on the 'PSGroup' column in your SQL query, you can utilize the `GROUP BY` clause along with appropriate sorting. Here's a modified version of your query that should produce the desired outcome:
In this modified query:
- We concatenate the string `'PSGROUP :'` with the value of `G.PSGroup` to represent the group name in the result set.
- The `GROUP BY` clause has been replaced with the `ORDER BY` clause to organize the results based on the 'PSGroup' first and then 'Barcode'.
- The result set will display the 'PSGROUP' followed by the associated 'Barcode', 'SwitchPrice', and 'PSGID' columns.
By executing this query, you should obtain the desired grouping of data based on the 'PSGroup' in your output, similar to the format you provided in your example. If you have any further questions or need clarification, feel free to ask!