Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
TANVIP
Regular Visitor

NEED HELP IN SQL

Hi

I am having two tables contact and contact category as mentioned below now 

 contact category

Contact categoryidtype
1phone
2mobile

contact

IDValueContact CategoryId
197867445453                              1
114343556677                              2
29783673526                              1
3                               1
31465276127                              2
418397387309                              2
50281938978                              2

 i want to get result entry of type phone when 'phone' is not available then 'mobile' should display 
as shown below 

IDValue
197867445453
29783673526
31465276127
418397387309
50281938978

please help me how to write sql quey to get this result 

thank you.

1 ACCEPTED SOLUTION
Anonymous
Not applicable

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.           

View solution in original post

1 REPLY 1
Anonymous
Not applicable

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.           

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors