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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Cant get this query right??

Author  Topic 

Kimi86
Yak Posting Veteran

79 Posts

Posted - 2012-05-22 : 04:58:45
Hi All,
This is really urgent and important as well, but I cant seem to get a simple thing right. Please help

My Data looks like this
PartKey QuestionKey
1001 1
1001 4
1001 3
1001 1
1001 1
1002 4
1002 3
1002 2
1002 4
1002 4
1002 3
1003 2
1003 5
1003 1
1003 8
1003 2
1003 6



Expected Result Set:
Need Frequesnct of each question for each Partkey
PartKey QuestionKey Frequency
1001 1 3
1001 4 1
1001 3 1
1002 4 3
1002 3 2
1002 2 1
1003 2 2
1003 5 1
1003 1 1
1003 8 1
1003 6 1

Scripts are below:

Create table Part
(PartKey int,
QuestionKey int)

Insert into Part values (1001,1)
Insert into Part values (1001,4)
Insert into Part values (1001,3)
Insert into Part values (1001,1)
Insert into Part values (1001,1)
Insert into Part values (1002,4)
Insert into Part values (1002,3)
Insert into Part values (1002,2)
Insert into Part values (1002,4)
Insert into Part values (1002,4)
Insert into Part values (1002,3)
Insert into Part values (1003,2)
Insert into Part values (1003,5)
Insert into Part values (1003,1)
Insert into Part values (1003,8)
Insert into Part values (1003,2)
Insert into Part values (1003,6)

Do I need cursors??

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-22 : 05:19:32
select PartKey, QuestionKey,count(QuestionKey) as frequency from Part
group by PartKey, QuestionKey

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -