Solved

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

Posted on 2014-01-09
4
273 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql server concatenate fields 10 35
Querying data from 3 SQL tables 2 32
SQL Syntax: How to force case sensitive query? 2 29
error in my cursor 5 30
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.

772 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