Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more
Hi
I am having two tables contact and contact category as mentioned below now
contact category
| Contact categoryid | type |
| 1 | phone |
| 2 | mobile |
contact
| ID | Value | Contact CategoryId |
| 1 | 97867445453 | 1 |
| 1 | 14343556677 | 2 |
| 2 | 9783673526 | 1 |
| 3 | 1 | |
| 3 | 1465276127 | 2 |
| 4 | 18397387309 | 2 |
| 5 | 0281938978 | 2 |
i want to get result entry of type phone when 'phone' is not available then 'mobile' should display
as shown below
| ID | Value |
| 1 | 97867445453 |
| 2 | 9783673526 |
| 3 | 1465276127 |
| 4 | 18397387309 |
| 5 | 0281938978 |
please help me how to write sql quey to get this result
thank you.
Solved! Go to Solution.
Hi @TANVIP ,
Here is the SQL query to get the desired result:
SELECT ID, Value
FROM contact
WHERE ContactCategoryId = (SELECT ContactCategoryId FROM contactcategory WHERE type = 'phone')
UNION
SELECT ID, Value
FROM contact
WHERE ContactCategoryId = (SELECT ContactCategoryId FROM contactcategory WHERE type = 'mobile')
This query will first select all the entries with ContactCategoryId of 'phone' and then all the entries with ContactCategoryId of 'mobile' if there are no entries with ContactCategoryId of 'phone'.
Best Regards,
Stephen Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @TANVIP ,
Here is the SQL query to get the desired result:
SELECT ID, Value
FROM contact
WHERE ContactCategoryId = (SELECT ContactCategoryId FROM contactcategory WHERE type = 'phone')
UNION
SELECT ID, Value
FROM contact
WHERE ContactCategoryId = (SELECT ContactCategoryId FROM contactcategory WHERE type = 'mobile')
This query will first select all the entries with ContactCategoryId of 'phone' and then all the entries with ContactCategoryId of 'mobile' if there are no entries with ContactCategoryId of 'phone'.
Best Regards,
Stephen Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!
Check out the November 2025 Power BI update to learn about new features.
| User | Count |
|---|---|
| 59 | |
| 43 | |
| 42 | |
| 23 | |
| 17 |
| User | Count |
|---|---|
| 190 | |
| 122 | |
| 96 | |
| 66 | |
| 47 |