I am newbie to SQL Joins.
I have two tables
Version
Vid, VName, IsActive
1 V1 1
2 V2 0
3 V3 1
Sub-Version
SVid,Vid, VName
1 1 0.1
2 1 0.2
3 2 0.1
In above tables each Version has many Sub-Version’s .
I need to fetch results from the above tables where the output should be like this.
Vid, VName, IsActive, SubVersionExists(Bit)
1 V1 1 1
2 V2 0 1
3 V3 1 0
in above result set the column name “SubVersionExists” represents if the version has SubVersion records in Sub-Version table.
Hope this explains the problem scenario well.
Thanks in advance.
Here is a version without joins, but using
EXISTS (SELECT ... )which returns a boolean value:http://sqlfiddle.com/#!5/712bd/9
Or, if your sql engine doesn’t convert booleans to 0/1, you can use
CASE:Another version with
LEFT JOIN+GROUP BY, if you don’t want to useCOUNT():Or you can use
DISTINCTinstead ofGROUP BY: