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 |
|
dhinasql
Posting Yak Master
195 Posts |
Posted - 2008-11-07 : 01:08:23
|
| Dear Friends,I need to calculate the total and average rating for the particular content, i will explain with the table structure and the expected output, i hope i here i can get the experts..I have 3 tables accordingly Kr_Reference, Kr_Referencehistory,kr_ReferenceRating.Kr_Reference------------RefId CatId OwnerId10 15 311 16 412 15 5Kr_ReferenceHistory--------------------LinkId RefId Title Status Deleted 1 10 test1 0 02 10 test2 1 03 12 test5 1 0Kr_ReferenceRating-------------------RatId RefId VoterId Points1 10 100 22 10 101 43 10 102 54 12 33 35 12 44 1From the Kr_Reference Table i have to show the results of category, where the CatId=15, so here i can filter 2 references 10 and 12 From the Kr_ReferenceHistory Table i have to check the 10 and 12 which is in active status(status=1), from here we have to fetch the 'title'. [Note : here more than one 10 and 12 refid may be found but only one will be active in the each reference id at a time, so we can find one title for refid = 10 and one title for refid=15.]From the Kr_ReferenceRating Table we have to find how many times the refId 10 and 12 occured, at what are the 'total' point for the refid 10 and 12. So My Expected output is LinkId RefId Title NumberofVoter TotalRating AvgRating 2 10 test2 3 11 3.663 12 test5 2 4 2.00 Please help me to get my expected output, You may find my friends post also here for the same , i hope you can get the clear about my expected output..Thanks in Advance friends... |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-07 : 01:27:17
|
| [code]SELECT rh.LinkId,r.RefId,rh.Title,rr.TotalVoters,rr.TotalRating,rr.AvgRating FROM Kr_Reference rINNER JOIN Kr_ReferenceHistory rhON rh.RefId=r.RefIdAND rh.Status=1INNER JOIN (SELECT RefId,SUM(Points)AS TotalRating,COUNT(VoterId) AS TotalVoters,SUM(Points)*1.0/COUNT(RatId) AS AvgRating FROM Kr_ReferenceRating GROUP BY RefId) rrON rr.RefId=r.RefId[/code] |
 |
|
|
|
|
|
|
|