Hi
I have table with Location,Branch. I want those records in which Branch has more than 1 location
Thanks
Hi
I have table with Location,Branch. I want those records in which Branch has more than 1 location
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Jan 30, 2025, 1:31 AM EST
You can use GROUP BY and HAVING to filter records where a Branch has more than one Location.
SELECT Branch FROM your_table GROUP BY Branch HAVING COUNT(DISTINCT Location) > 1;Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Jan 30, 2025, 1:32 AM EST
Hi Ramco,
You can achieve this using SQL by grouping the records by
Branchand filtering those that have more than one distinctLocation.SELECT Location, Branch FROM YourTable WHERE Branch IN ( SELECT Branch FROM YourTable GROUP BY Branch HAVING COUNT(DISTINCT Location) > 1 );Tharunkumar MagudeeswaranRank #966 · 2.1K reputationPosted Jan 30, 2025, 2:57 AM EST
Here is the code you can use it to check records with branch having more than 1 location.
SELECT Branch, count(Location) FROM Your_Table GROUP BY Branch HAVING COUNT(1) > 1Tuhin PaulRank #7 · 71.9K reputationPosted Jan 30, 2025, 6:11 AM EST
To retrieve records from a table where a
Branchhas more than oneLocation, you can use a SQL query with a GROUP BY clause and a HAVING condition.SELECT Location, Branch FROM YourTableName WHERE Branch IN ( SELECT Branch FROM YourTableName GROUP BY Branch HAVING COUNT(Location) > 1 );Subquery:
The subquery groups the data by
Branchand uses theHAVINGclause to filter branches with more than oneLocation.HAVING COUNT(Location) > 1ensures only branches with multiple locations are selected.Main Query:
The main query retrieves all records from the table where the
Branchis in the result of the subquery.For example see the next sample table:
Table:
LocationsSELECT Location, Branch FROM Locations WHERE Branch IN ( SELECT Branch FROM Locations GROUP BY Branch HAVING COUNT(Location) > 1 );