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.
| 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 Network1 ESPN1 FOX2 ABC 3 FOX4 ESPN4 ABC 4 FOX 5 ABC5 ESPN6 FOX6 ABCSo 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. |
 |
|
|
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 thisdeclare @t table (id int, Network varchar(10))insert into @tselect 1,'ESPN' union allselect 1,'FOX' union allselect 2,'ABC' union allselect 3,'FOX' union allselect 4,'ESPN' union allselect 4,'ABC' union allselect 4,'FOX' union allselect 5,'ABC' union allselect 5,'ESPN' union allselect 6,'FOX' union allselect 6,'ABC' union allselect 7,'ABC1' union allselect 7,'FOX1' union allselect 8,'ABC1' union allselect 8,'FOX1' --select * from @tSelect 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, '') Networksfrom @t a group by a.ID) v group by NetworksFor Faster results please follow the posting guidelines herehttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
|
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_NAME1 ESPN Joe Montana1 ESPN Joe Montana2 FOX Alex Smith2 ESPN Alex Smith2 ABC Alex Smith3 FOX Tom Brady3 ABC Tom Brady4 ESPN Chris Baker4 FOX Chris Baker5 ESPN N Suh5 FOX N Suh6 FOX Chris Simms6 ABC Chris Simms7 ABC Vince Young7 ESPN Vince Young8 FOX Randy Moss9 ABC Chris HenrySo 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 |
 |
|
|
|
|
|
|
|