Hi
I have table OWHS with fields BplId,Location. I want list of those reocrd which BPlId has more than 1 Location
Thanks
Hi
I have table OWHS with fields BplId,Location. I want list of those reocrd which BPlId 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
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 29, 2025, 11:58 AM EDT
To retrieve records from the
OWHStable where theBplIdhas more than one uniqueLocation, you can use SQL. The query will involve grouping the data byBplIdand filtering those groups that have more than one distinctLocation.SELECT BplId, Location FROM OWHS WHERE BplId IN ( SELECT BplId FROM OWHS GROUP BY BplId HAVING COUNT(DISTINCT Location) > 1 );Explanation of the Query
Subquery (
INClause) :BplIdvalues that have more than one uniqueLocation.GROUP BY BplIdto group the records byBplId.HAVING COUNT(DISTINCT Location) > 1condition ensures that only thoseBplIdvalues with more than one distinctLocationare selected.Outer Query :
BplIdandLocation) from theOWHStable where theBplIdmatches the list ofBplIdvalues returned by the subquery.Tuhin PaulRank #7 · 71.9K reputationPosted Mar 29, 2025, 11:59 AM EDT
If you prefer not to use a subquery, you can achieve the same result using a
JOIN:SELECT o.BplId, o.Location FROM OWHS o JOIN ( SELECT BplId FROM OWHS GROUP BY BplId HAVING COUNT(DISTINCT Location) > 1 ) sub ON o.BplId = sub.BplId;Explanation of the Alternative Query
Subquery (
sub) :BplIdvalues with more than one distinctLocation, similar to the previous query.JOIN :
OWHS) is joined with the subquery on theBplIdcolumn.BplIdvalues matching those in the subquery are retrieved.Pavan Nalluri119 reputationPosted Apr 2, 2025, 2:05 PM EDT
SELECT BplId, COUNT(Location) AS NoOfLoc FROM OWHS GROUP BY BplId HAVING COUNT(Location) > 1;Explanation:
The above query displays the BplId for which Id there is count of Location greater than 1, we grouped teh resukt by id and then filtered it based on the count of location
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Apr 2, 2025, 2:34 PM EDT
Hello Ramco,
You can use the HAVING clause with GROUP BY to find BplId values that have more than one associated Location.
SELECT BplId FROM OWHS GROUP BY BplId HAVING COUNT(DISTINCT Location) > 1;Good Luck!