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 |
darbar
Starting Member
16 Posts |
Posted - 2013-10-24 : 09:38:58
|
HI,I am having following data:c1 c2 c3122 40 1122 107 2122 83 3122 79 3122 81 3122 31 4122 206 5122 207 5122 208 5122 147 6122 148 6122 131 6122 36 7122 153 8122 151 8122 86 9122 82 10122 112 11122 37 12122 38 12122 74 13122 154 14122 155 14122 213 15I want to count number of occurrence in column c3. i.e., count 1 occurred value 9 times, count 2 occurred value 3 times, count 3 occurred value 3 times.Outputc1 p1 p2 p3122 9 3 3Support is highly appreciable.Thanks in advance. |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-24 : 10:17:49
|
quote: Originally posted by darbar HI,I am having following data:c1 c2 c3122 40 1122 107 2122 83 3122 79 3122 81 3122 31 4122 206 5122 207 5122 208 5122 147 6122 148 6122 131 6122 36 7122 153 8122 151 8122 86 9122 82 10122 112 11122 37 12122 38 12122 74 13122 154 14122 155 14122 213 15I want to count number of occurrence in column c3. i.e., count 1 occurred value 9 times, count 2 occurred value 3 times, count 3 occurred value 3 times.Outputc1 p1 p2 p3122 9 3 3Support is highly appreciable.Thanks in advance.
I don't see 1 occurring 9 times in column 3, so this may not be what you want - but based on my understanding:SELECT * FROM (SELECT c1, c3, COUNT(*) NFROM TblGROUP BY c1,c3 ) sPIVOT (MAX(N) FOR c3 IN ([1],[2],[3]))P |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-24 : 10:56:59
|
And this as per my understanding;With NumbersAS(SELECT 0 AS NUNION ALLSELECT N +1FROM NumbersWHERE N + 1 <=9)SELECT *FROM(SELECT c1,NFROM (SELECT DISTINCT c1 FROM table)tCROSS JOIN Numbers n)pOUTER APPLY (SELECT COUNT(*) AS Cnt FROM Table WHERE c3 LIKE '%' + CAST(p.N AS varchar(10)) + '%' )qPIVOT(SUM(Cnt) FOR N IN ([1],[2],[3],...[9],[0]))p ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
darbar
Starting Member
16 Posts |
Posted - 2013-10-24 : 11:31:52
|
Thanks dear for kind support but this what I am not looking for let me clear more:In C3 column: 1,2,4,7,9,10,11,13,15 is having single value so output would be 9. And 8,12,14 is having 2 value each so output would be 3. And 3,5,6 is having 3 value each so output would be 3 and so on. c1 column is n. Every C1 column value starts from 1 to n. and these value may be more than once, twice, and so on..Please suggest me the solution.Thanks for kind support.Pradeep |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-24 : 11:42:54
|
[code]SELECT *FROM(SELECT c1,c2,COUNT(*) AS CntFROM tableGROUP BY c1,c2)tPIVOT (COUNT(c2) FOR Cnt IN ([1],[2],[3]))p[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
darbar
Starting Member
16 Posts |
Posted - 2013-10-24 : 21:32:58
|
query:SELECT *FROM (SELECT const_cd, building_booth_cd, COUNT(*) AS CntFROM pollingstationGROUP BY const_cd, building_booth_cd) t PIVOT (COUNT(building_booth_cd) FOR Cnt IN ([1], [2], [3], [4], [5], [6])) poutput:c1 1 2 3 4 5 6 --Heading122 103 37 9 1 1 0123 143 26 8 0 0 0124 183 30 0 0 0 0125 101 37 5 2 1 0126 95 43 13 2 1 0127 23 23 17 9 4 1128 49 26 11 2 5 0129 30 32 7 7 7 0130 116 35 14 4 0 0131 97 62 3 2 0 0My query:select const_cd, sum(cnt)/6 from (select const_cd, count(building_booth_cd) as cnt from pollingstation group by const_cd, building_booth_cd having count(building_booth_cd)=6 ) t group by const_cd, cnt order by const_cdmy output is:c1 6127 1I want output of query as follows:c1 6 --Heading122 0123 0124 0125 0126 0127 1128 0129 0130 0131 0Please suggest me the solution.Thanks for kind cooperation.Pradeep |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-25 : 01:54:51
|
thats same as what you're getting from my query isnt it? if you just want 6 heading alone just add that in select listDECLARE @YourValue intSET @YourVlue = 6 --set any value hereSELECT *FROM (SELECT const_cd, building_booth_cd, COUNT(*) AS CntFROM pollingstationGROUP BY const_cd, building_booth_cdHAVING COUNT(*) = @YourValue) t PIVOT (COUNT(building_booth_cd) FOR Cnt IN ([1], [2], [3], [4], [5], [6])) p ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|