Hi , can anyone knows suggest with example .
Tried Query : select * from da5.SwitchPrice as S inner join da5.SwitchPriceGroup as G on S.PSGID=G.PSGID;
Result we got :
Barcode SwitchPrice PSGID PSGroupName
______________________________________________
A01 Price2 1 LUNCH
A02 Price3 1 LUNCH
A03 Price4 1 LUNCH
A04 Price3 2 EVENING
Result we desire to get :
Barcode SwitchPrice PSGID
________________________________________________
PSGROUPName :LUNCH
A01 Price2 1
A02 Price3 1
A03 Price4 1
PSGROUPName:EVENING
A04 Price3 2
Thanks in advance

Tuhin PaulPosted Feb 28, 2025, 8:00 AM
If you prefer to implement this in C# , here's how you can do it:
SQL Query :
Post-Processing :
Scalability :
Tuhin PaulPosted Feb 28, 2025, 7:59 AM
If you want to fetch the data in a single query, your existing SQL query is already correct:
You can use this query to fetch the data and then process it programmatically
Tuhin PaulPosted Feb 28, 2025, 7:59 AM
To achieve the desired output where the PSGROUPName acts as a header and the corresponding rows are grouped under it, you need to process the query result programmatically. SQL alone does not support this kind of hierarchical or formatted output directly. However, you can achieve this by using a programming language like Python, C#, or even SQL with some post-processing logic.
Below is an example implementation in Python using
pandasto format the data as required:Input Data :
pandas.DataFrame. In a real-world scenario, you would fetch this data from your database using a library likesqlalchemyorpyodbc.Grouping :
groupbyfunction groups the data by thePSGroupNamecolumn.Formatting :
PSGroupName) is printed as a header.Barcode,SwitchPrice,PSGID).Blank Line :
Karthik KPosted Feb 28, 2025, 2:59 AM
@ Daniel Wright,
Thanks for your response .,
we require Groupname as header , details in the 2nd row like that
Thanks
Daniel WrightPosted Feb 28, 2025, 2:49 AM
Based on the provided scenario, it seems like you are looking for a way to group the results based on the "PSGroupName" from your query. To achieve the desired result, you can utilize the `GROUP BY` clause in MySQL. Here is an example query that should help you achieve the desired output:
In this query, we have included the `GROUP BY` clause to group the results based on the "PSGroupName". By including all the selected columns along with the grouping column "PSGroupName", you can get the desired output where the results are grouped under each "PSGroupName".
This query should help you achieve the result structure you desire, with the data grouped under the respective "PSGroupName". If you have any further questions or need additional clarification, feel free to ask.