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 |
|
pecunn
Starting Member
20 Posts |
Posted - 2011-05-06 : 02:36:14
|
| Hi guys,i have query in here showing number of result based on result category. My query looks like this.SELECT resultCategory, Count(*) as 'number of result'FROM resultWHERE resultcategory in ('Good mark', 'pass', 'fail') AND resultDate BETWEEN date1 AND date2GROUP BY resultCategoryThis will return the data as belowresultCategory | number of resultgood mark | 5pass | 1fail | 3however when i changed the date, which do not have student PASS on that period of time the result will showing asresultCategory | number of resultgood mark | 5fail | 3what i want to see is still keep showing category PASS but with 0/null value, is that possible?resultCategory | number of resultgood mark | 5pass | 0/ nullfail | 3 |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2011-05-06 : 03:47:52
|
| SELECT DISTINCT r1.resultCategory, number_of_resultFROM result r1 LEFT OUTER JOIN ( SELECT resultCategory, Count(*) as number_of_result FROM result WHERE resultcategory in ( 'Good mark', 'pass', 'fail' ) AND resultDate BETWEEN date1 AND date2 GROUP BY resultCategory ) r2 ON r1.resultCategory = r2.resultCategory--------------------------http://connectsql.blogspot.com/ |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-05-06 : 03:51:49
|
[code]declare @Sample table (id int identity(1,1),txt varchar(255), ddate date)insert @Sampleselect 'A','20110505' union allselect 'A','20110506' union allselect 'B','20110401' union allselect 'C','20110505'select * from @Sampleselecttxt,sum(case when ddate between '20110504' and '20110510' then 1 else 0 end) as count_txtfrom @Samplegroup by txt[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2011-05-07 : 13:09:10
|
| [code]declare @resultCategores table(resultCategory varchar(9))insert into @resultCategories values('Good mark'), ('pass'), ('fail')SELECT t.resultCategory, Count(r.resultCategory) as 'number of results'FROM @resultCategories tleft join result r on r.resultCategory = t.resultCategoryWHERE resultDate BETWEEN @date1 AND @date2GROUP BY t.resultCategory[/code]MirkoMy blog: http://mirko-marovic-eng.blogspot.com/ |
 |
|
|
pecunn
Starting Member
20 Posts |
Posted - 2011-05-08 : 21:17:47
|
| Thanks a lot lionofdezertIt works well |
 |
|
|
|
|
|
|
|