0

Hello I have two tables as below

tblContactType

typeId  typeName active
1       Email      1
2       Phone      1
3       Address    1
4       Fax        1

tblContact

id    IdName   typeId  groupId
100    test      1       1
101    test2     1       1
102    test3     1       2
103    test4     2       2
104    test5     2       3
105    test6     3       3

Want the results to be with column names as typeName count and grouped by group id. Results should be total number of types associated to a group,which are associated to a contact.

GroupId    EmailCount    PhoneCount  AddressCount     FaxCount
1           2             0             0               0
2           1             1             0               0
3           0             1             1               0  
2
  • what have you tried so far? you can achieve this by LEFT JOIN, GROUP BY, SUM and IIF or CASE Commented Jun 11, 2017 at 21:52
  • do you know pivot query? Commented Jun 11, 2017 at 22:59

2 Answers 2

1

You can group by and pivot as below:

Select * from (
    Select t.groupid, tct.typename, t.id from tblContact t 
    inner join tblContactType tct 
    on t.typeid = tct.typeid
) a
pivot (count(a.id) for typename in ([Email],[Phone],[Address],[Fax]) ) p

For dynamic list of columns you can use dynamic query as below:

declare @cols1 varchar(max)
declare @query nvarchar(max)

Select  @cols1 = stuff((Select distinct ','+QuoteName(typename) from tblContactType for xml path('')),1,1,'')
Set     @query = '  Select * from (
        Select t.groupid, tct.typename, t.id from tblContact t 
        inner join tblContactType tct 
        on t.typeid = tct.typeid
    ) a
    pivot (count(a.id) for typename in (' + @cols1 + ') ) p '

Select @query --Check the generated query is good and then execute below
--exec sp_executesql @query

Output as below:

+---------+---------+-------+-----+-------+
| groupid | Address | Email | Fax | Phone |
+---------+---------+-------+-----+-------+
|       1 |       0 |     2 |   0 |     0 |
|       2 |       0 |     1 |   0 |     1 |
|       3 |       1 |     0 |   0 |     1 |
+---------+---------+-------+-----+-------+
Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly fine thanks. Also how can I add a total column and display total of each row in that column??
0

Here is another solution.

SELECT groupId,
        SUM(CASE WHEN c.typeId = 1 THEN 1 ELSE 0 END) 'EmailCount',
        SUM(CASE WHEN c.typeId = 2 THEN 1 ELSE 0 END) 'PhoneCount',
        SUM(CASE WHEN c.typeId = 3 THEN 1 ELSE 0 END) 'AddressCount',
        SUM(CASE WHEN c.typeId = 4 THEN 1 ELSE 0 END) 'FaxCount'
FROM tblContact c
    JOIN tblContactType ct ON c.typeId = ct.typeId  
GROUP BY groupId 

Results

-------------------------------------------------------------
groupId  |  EmailCount | PhoneCount | AddressCount | FaxCount
-------------------------------------------------------------
  1      |     2       |    0       |      0       |    0
  2      |     1       |    1       |      0       |    0
  3      |     0       |    1       |      1       |    0
-------------------------------------------------------------

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.