Click here to Skip to main content
15,917,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables namely Table1 & Table2. Table1 has two columns namely "Column1" & "Column2"
I need to write SQL Query to get count of Table2.Column1 where Table2.Column1=Table1.Column1 and Table1.Column2=1

Could you please assists? Thanks in advance
Posted
Updated 14-Jun-14 20:18pm
v2
Comments
[no name] 14-Jun-14 8:56am    
http://www.w3schools.com/sql/sql_func_count.asp

1 solution

Here is your Query
SQL
SELECT COUNT(T1.Column1) FROM @Table1 T1 INNER JOIN @Table2 T2 ON ( (T1.column1=T2.Column1) AND T1.column2=1)


I have tested this with below sample data

SQL
DECLARE @Table1 AS Table(Column1 INT,Column2 INT)
DECLARE @Table2 AS Table(Column1 INT)

INSERT INTO @Table1 
SELECT 1,1
UNION
SELECT 3,1
UNION
SELECT 2,2

INSERT INTO @Table2 
SELECT 1
UNION
SELECT 2

SELECT COUNT(T1.Column1) FROM @Table1 T1 INNER JOIN @Table2 T2 ON ( (T1.column1=T2.Column1) AND T1.column2=1)


Result of this query according to sample data considered in this example
Count = 1

Hope this helps, If yes then accept and vote the answer
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900