2

table B, table c.

Table A has ID, col1

ID    COL1
1      abc
2      bcd

Table B has col2, col3

col2 col3
bcc  abc
acc  bcd

Table C has ID,col4

ID col3
1   qwe
1   tre
1   uid
2   jkj
2   jwekj

Now, i need to select ID, COL1,COL2, count(ID in table C). The sample output will be

ID  COL1  COL2  CNT
1   abc   bcc    3
2   bcd   acc    2

Could anyone please help me in this? Table A and B are joined using COl1 and COL3.

1
  • What version of sql-server are you using? Commented Apr 26, 2012 at 7:11

8 Answers 8

1

Why don't you join Table A & Table C by ID and get the count?

SELECT TableA.ID, TableA.COL1, TableB.COL2, COUNT(TableC.*)
FROM TableA
INNER JOIN TableB ON TableB.COL3 = TableA.COL1
INNER JOIN TableC ON TableC.ID = TableA.ID
GROUP BY TableA.ID, TableA.COL1, TableB.COL2
ORDER BY TableA.ID, TableA.COL1, TableB.Col2
Sign up to request clarification or add additional context in comments.

Comments

1
select  a.id
,       max(a.col1)
,       avg(b.col2)
,       count(distinct c.ID)
from    TableA a
join    TableA b
on      a.col1 = b.col3
left join
        TableC c
on      c.id = a.id
group by
        a.id

4 Comments

Can we get only by grouping on id column?
Have a look at @Arion's answer
All the ID in table A need not present in Table C. So I need to get all the values from Table. If that ID is not present in the Table C, that count should give as 0. Here only I could not get it. I tried with Left outer join. But grouping only on ID is bloking me.
Edited so it only groups on ID, but that means you have to place the other columns in an aggregate function, like min or max or avg
1
select A.ID, A.Col1, B.Col2, Count(*) AS Cnt 
from A
inner join B ON B.Col3 = A.Col1
left join C ON C.ID = A.ID
Group By A.ID, A.Col1, B.Col2

Comments

0
SELECT A.ID, A.COL1, B.COL2, (SELECT COUNT(C.ID) FROM C) AS CNT
FROM A INNER JOIN B ON A.COL1=B.COL3 

Comments

0

Maybe something like this:

SELECT
    TableA.COL1,
    TableB.col2,
    (
        SELECT
            COUNT(*)
        FROM
            TableC
        WHERE
            TableC.ID=TableA.ID
    )
FROM
    TableA
    JOIN TableB
        ON TableA.COL1=TableB.col3

Comments

0
Select 
    a.Id, a.col1, b.col2, count(c.*) As Cnt
From A as a
Inner Join B as b on a.col1 = b.col3
Inner Join C as c on a.Id    = c.Id
Group By a.Id, a.col1, b.col2

Comments

0
declare @A  table(ID int, col1 varchar(50))
declare @B  table(col2 varchar(50), col3 varchar(50))
declare @C  table(ID int, col4 varchar(50))

INSERT INTO @A(ID, col1) values(1, 'abc')
INSERT INTO @A(ID, col1) values(2, 'bcd')

INSERT INTO @B(col2, col3) values('bcc'  , 'abc')
INSERT INTO @B(col2, col3) values('acc'  , 'bcd')

INSERT INTO @C(ID, col4) values('1'  , 'qwe')
INSERT INTO @C(ID, col4) values('1'  , 'tre')
INSERT INTO @C(ID, col4) values('1'  , 'uid')
INSERT INTO @C(ID, col4) values('2'  , 'jkj')
INSERT INTO @C(ID, col4) values('2'  , 'jwekj')


-- The query
SELECT a.ID, a.col1, b.col2, (SELECT COUNT(c.ID) FROM @C c WHERE c.ID = a.ID) CNT
FROM @A a join @b b ON a.col1 = b.col3

Comments

0
select A.iD, A.Col1,B.col2, 
       (select count(*) from tableC C where C.id=A.ID) as cnt
from tableA A join tableB B on A.col1=b.col3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.