Solved

How can I count the number of times a value exists in a field

Posted on 2014-01-09
4
267 Views
Last Modified: 2014-01-09
Hello, I have two tables. One "header data" and one "data only".

Header table:
[ID]      [DateTime]                        [Comment]
1      2014-01-08 20:10:26.767      Test running 1
2      2014-01-08 20:29:37.203      Test running 2

Data table:
[ID]      [Header_ID]      [Type]
1      1                  'A'
2      1                  'B'
3      1                  'A'
4      1                  'C'
5      2                  'A'
6      2                  'C'
7      2                  'C'

I want to run a query that give me following results (how many times the [Type] field have value 'A','B' or 'C' for every "running".

[ID]      [DateTime]                        [Comment]            [CountA]      [CountB]      [CountC]
1      2014-01-08 20:10:26.767      Test running 1      2            1            1
2      2014-01-08 20:29:37.203      Test running 2      1            0            2
0
Comment
Question by:tselectro
4 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39767462
this should do:
select h.ID
  , h.[DateTime]                        
 , h.[Comment]
 , sum(case when d.type = 'A' then 1 else 0 end) countA
 , sum(case when d.type = 'B' then 1 else 0 end) countB
 , sum(case when d.type = 'C' then 1 else 0 end) countC
from [Header table] h
left join [Data table] d
   on d.header_id = h.id
group by h.ID
  , h.[DateTime]                        
 , h.[Comment]  

Open in new window

0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 39767466
SELECT
H.ID
,H.DateTime
,H.Comment
,DSummed.CountA
,DSummed.CountB
,DSummed.CountC
FROM [Header Table] H
JOIN
(
  SELECT
  D.[Header_ID]
  ,SUM(CASE WHEN TYPE = 'A' THEN 1 ELSE 0 END)   [CountA]
  ,SUM(CASE WHEN TYPE = 'B' THEN 1 ELSE 0 END)   [CountB]
  ,SUM(CASE WHEN TYPE = 'C' THEN 1 ELSE 0 END)   [CountC]
  FROM
  [Data Table] D
  GROUP BY D.[Header_ID]
) D_Summed
ON H.ID = D_Summed.Header_ID

Open in new window

0
 
LVL 11

Expert Comment

by:Louis01
ID: 39767554
--Static Pivot example (Always have only have results for type A, B & C)
select ID, dt, Comment, [A], [B], [C]
 from (select h.*, d.[Type]
        from headerTable h
                inner join dataTable d
          on h.ID = d.Header_ID) p
 pivot (count([Type]) for [Type] in ([A], [B], [C])) as pvt
 order by dt;

Open in new window

--Dynamic Pivot example    (Count every type - including 'D')
declare @cols varchar(max);
select @cols = coalesce(@cols + ',','') + '[' + [Type] + ']'
  from (select distinct [Type] from dataTable d1) d2;
 select @cols;

declare @sql varchar(max);
set @sql = 'select ID, dt, Comment, ' + @cols + '
             from (select h.*, d.[Type]
                    from headerTable h
                            inner join dataTable d
                      on h.ID = d.Header_ID) p
             pivot (count([Type]) for [Type] in (' + @cols + ')) as pvt
             order by dt;'
exec (@sql);             

Open in new window

0
 

Author Closing Comment

by:tselectro
ID: 39767572
Thank you for very fast answer!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now