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 |
|
palak
Yak Posting Veteran
55 Posts |
Posted - 2008-03-14 : 13:43:00
|
| create function mytotalcount(@audit varchar(50), @startdate datetime, @enddate datetime)returns tableasreturn(select t.value,sum(t.countvalue) as totalcount from(select sm.value,count(sm.value) as countvaluefrom subjectbase sjoin stringmap smon s.organizationid = sm.organizationidinner join audit aon s.subjectid=a.subjectidinner join incidentbase ion i.subjectid=s.subjectidwhere a.auditid= @audit and (i.modifiedon between @startdate and @enddate) and sm.attributename = 'contractservicelevelcode' and sm.ObjectTypeCode = 112group by sm.value) tgroup by t.value)value totalcount------------------NHLBI Employee 329NIH Employee 329Public 329VIP 329instead of different values i m getting same...there is something wrong in joins..can anyone help me?thanks. |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
|
|
jemajoign
Starting Member
7 Posts |
Posted - 2008-03-14 : 14:42:46
|
| I can't tell what you're trying to do, but here's some thoughts.You're grouping by sm.value in the subquery. So that will give you back something like:Table t:Value ------ CountValueGroup1 ------ count1Group2 ------ count2...GroupN ------ countNAnd then you are grouping the above data by t.Value in the second Group By. But notice that the data is already grouped by t.Value. So that second Group By won't in fact group anything together. Then for any given row you'll have TotalCount == t.CountValue because there is only one row in every group.Hope that helps. And I'm no SQL expert, so someone please correct me if I'm wrong.Thanks,Thomas |
 |
|
|
|
|
|