Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Help with SQL query

Author  Topic 

mlazur
Starting Member

5 Posts

Posted - 2010-08-24 : 11:17:32
Hi, I need to create a query that will help separate the 'sources' of a records in my table.

There are 3 main sources: ESPN, FOX, and ABC.
There is also an individual identifier, but it is not unique for the table.

I basically have a list of football players that were shown on these networks and I want to see which ones were only shown on 1 network, 2 networks (and all combinations) and 3 networks.

For example:

ID Network
1 ESPN
1 FOX
2 ABC
3 FOX
4 ESPN
4 ABC
4 FOX
5 ABC
5 ESPN
6 FOX
6 ABC

So for this sample data I would want to know that there was 1 ID in ESPN & FOX, 1 ID in just ABC, 1 ID just in FOX, 1 ID in all three, 1 ID in ABC and ESPN, and 1 ID in FOX and ABC.

Any help would be greatly appreciated. Thanks.

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2010-08-24 : 11:19:46
please post structure of your table along with sample data and desired output.

and please clarify your output.
Go to Top of Page

MSquared
Yak Posting Veteran

52 Posts

Posted - 2010-08-24 : 12:11:48
It looks like you just want to group the Networks by ID and then count the Network groups. try this


declare @t table (id int, Network varchar(10))
insert into @t
select 1,'ESPN' union all
select 1,'FOX' union all
select 2,'ABC' union all
select 3,'FOX' union all
select 4,'ESPN' union all
select 4,'ABC' union all
select 4,'FOX' union all
select 5,'ABC' union all
select 5,'ESPN' union all
select 6,'FOX' union all
select 6,'ABC' union all
select 7,'ABC1' union all
select 7,'FOX1' union all
select 8,'ABC1' union all
select 8,'FOX1'


--select * from @t

Select sum(Num) as NumInstances, Networks from (
select count(distinct a.ID) Num, a.id,
Stuff(
(select ',' + Network
from @t b
where a.ID = b.ID
for XML path ('')
)
, 1, 1, '') Networks
from @t a
group by a.ID) v group by Networks


For Faster results please follow the posting guidelines here

http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page

mlazur
Starting Member

5 Posts

Posted - 2010-08-24 : 12:17:49
Table has ID, SOURCE, FIRST_NAME, LAST_NAME in it.

Some sample data:

ID SOURCE FIRST_NAME LAST_NAME
1 ESPN Joe Montana
1 ESPN Joe Montana
2 FOX Alex Smith
2 ESPN Alex Smith
2 ABC Alex Smith
3 FOX Tom Brady
3 ABC Tom Brady
4 ESPN Chris Baker
4 FOX Chris Baker
5 ESPN N Suh
5 FOX N Suh
6 FOX Chris Simms
6 ABC Chris Simms
7 ABC Vince Young
7 ESPN Vince Young
8 FOX Randy Moss
9 ABC Chris Henry

So there are 9 unique players in this list.

I do not really care about output format, I just want to know the queries that I can use to find the following information:

# of IDs with only 1 source: 3 (ID: 1, 8, 9)
# of IDs with only 1 source from ESPN: 1 (ID: 1)
# of IDs with only 1 source from FOX: 1 (ID: 8)
# of IDs with only 1 source from ABC: 1 (ID: 9)

# of IDs with only 2 sources: 5 (ID: 3, 4, 5, 6, 7)
# of IDs with only 2 sources from ESPN & ABC: 1 (ID: 7)
# of IDs with only 2 sources from ESPN & FOX: 2 (ID: 4, 5)
# of IDs with only 2 sources from ABC & FOX: 2 (ID: 3, 6)

# of IDs with only 3 sources: 1 (ID: 2)

Ultimately I would like to know all of these numbers, through a series or queries.

I can also create another temporary table to assist with this analysis.

Thanks, any help is greatly appreciated.

Mike
Go to Top of Page
   

- Advertisement -