Author |
Topic |
ngquochung
Starting Member
13 Posts |
Posted - 2006-11-26 : 01:47:16
|
How to find the average of another nested count aggregation?For exampleselect count (PANUM)from PAPERwhere AVG (count(PANUM))>5Thanks |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-11-26 : 03:33:47
|
Your question doesn't make sense what-so-ever.Firstly you are taking COUNT and then you are taking AVG of that COUNT. What are you trying to achive?Harsh AthalyeIndia."Nothing is Impossible" |
|
|
ngquochung
Starting Member
13 Posts |
Posted - 2006-11-26 : 04:13:30
|
In the department, there are many staff and every staff authors a certain number of paper (panum is the paper id).What I want to achieve is: display the author that has quantity of paper (for each author only) greater than the average of paper quantity (for all staffs member in the department)--DEPTNUM is the ID represent the DEPARTMENT--ACNUM is the ID represent the AUTHORselect ACNUM, FAMNAME, GIVENAMEfrom AUTHOR, ACADEMICwhere AUTHOR.ACNUM = ACADEMIC.ACNUM and ACNUM in (select ACNUM from AUTHOR group by ACNUM having count (ACNUM)> (select avg (count(ACNUM)) from ACADEMIC group by DEPTNUM)) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-26 : 04:23:18
|
You mean to select to all authors whos papers quantity is larger than the average quantity of all authors?Peter LarssonHelsingborg, Sweden |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-11-26 : 04:48:50
|
Where in this case, the PANUM comes into the picture. It would be helpful if you could post your table structures here.Harsh AthalyeIndia."Nothing is Impossible" |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-11-26 : 05:02:36
|
May be this:Select ACNUM, FAMNAME, GIVENAMEfrom AUTHOR JOIN ACADEMICON AUTHOR.ACNUM = ACADEMIC.ACNUM WHERE AUTHOR.ACNUM in ( select ACNUM from AUTHOR au group by ACNUM having count(ACNUM) > ( select count(ACNUM)/(SELECT COUNT(ACNUM)*1.0 FROM ACADEMIC) from ACADEMIC ac WHERE ac.DEPTNUM = au.DEPTNUM ) ) Harsh AthalyeIndia."Nothing is Impossible" |
|
|
ngquochung
Starting Member
13 Posts |
Posted - 2006-11-26 : 05:27:46
|
No I meanthe number of paper by each author GREATER THAN the average of (the number of paper by all the author of the department) |
|
|
ngquochung
Starting Member
13 Posts |
Posted - 2006-11-26 : 05:28:05
|
Department(DeptNum, Descrip, Instname, DeptName, State, Postcode)Academic(AcNum, DeptNum, FamName, GiveName, Initials, Title)Paper(PaNum, Title)Author(PaNum, AcNum)Field(FieldNum, ID, Title)Interest(FieldNum, AcNum, Descrip) |
|
|
ngquochung
Starting Member
13 Posts |
Posted - 2006-11-26 : 05:32:58
|
Hi, I'm afraid ur solution is not correct. It results in such errors:Server: Msg 8118, Level 16, State 1, Line 1Column 'AUTHOR.ACNUM' is invalid in the select list because it is not contained in an aggregates function and there is no GROUP BY clause.Server: Msg 8118, Level 16, State 1, Line 1Column 'ACADEMIC.FAMNAME' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.Server: Msg 8118, Level 16, State 1, Line 1Column 'ACADEMIC.GIVENAME' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-26 : 06:03:48
|
select a.panum, a.acnum, p.titlefrom (select panumfrom papergroup by panumhaving count(*) >= (SELECT 1.0 * COUNT(*) / COUNT(DISTINCT PANUM) FROM paper) qinner join paper p on p.panum = q.panuminner join author a on a.panum = q.panumPeter LarssonHelsingborg, Sweden |
|
|
ngquochung
Starting Member
13 Posts |
Posted - 2006-11-27 : 09:15:23
|
Hi everyone,Thank you a lot for your assistance. I still need some more help.The full question is: Considering the Griffith University, which staff have authored more than the average number of papers authored by a staff member in the department that they are from? Return the AcNum and name of staff, and the number of papers they have written, in alphabetical order of their family name. Note that a paper may have more than one author and it should be counted for each author. Staff who do not write papers at all are not counted.My solution is:select AUTHOR.ACNUM, FAMNAME, GIVENAME, count (PANUM)from DEPARTMENT, ACADEMIC, AUTHORwhere DEPARTMENT.DEPTNUM = ACADEMIC.DEPTNUM and ACADEMIC.ACNUM = AUTHOR.ACNUM and INSTNAME = 'Griffith University' and AUTHOR.ACNUM in ( select ACNUM from AUTHOR group by ACNUM having count (PANUM)>any ( select count(PANUM) /count(distinct ACNUM) from AUTHOR group by ACNUM ) ) group by AUTHOR.ACNUM, FAMNAME, GIVENAMEorder by FAMNAMEI think there is still mistakes due to the incorrect grouping. I don't know how to fix it. When testing, it results in missing some value in the attribute panum. The data structure of tables is already provided in last post.Thanks a lot |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-27 : 09:17:52
|
Start a new thread by send a new post or topic instead!Peter LarssonHelsingborg, Sweden |
|
|
ngquochung
Starting Member
13 Posts |
Posted - 2006-11-27 : 09:26:58
|
This is not new topic. This is virtually the full question from the brief questions in the previous posts also in this thread.Regards,NQH |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-11-27 : 09:33:30
|
Are you able to present some sample data for each and one table?Peter LarssonHelsingborg, Sweden |
|
|
X002548
Not Just a Number
15586 Posts |
|
ngquochung
Starting Member
13 Posts |
Posted - 2006-11-27 : 18:35:35
|
[code]PANUM ACNUM FAMNAME GIVENAME DEPTNUM ----------- ----------- -------------------- -------------------- --- 1 100 Kornreich Jeroen 1001 101 Yee Mark 1002 102 Fernandes Russell 1003 103 Cohen Richard 1003 104 Polemitis Joe 1004 105 Brower Tony 1005 106 Royce Mel 1005 107 Christine Leonardo 1005 108 Brandon Chuck 1005 109 Gustafsson Hakan 1015 111 Barton Michel 1326 113 Sadhal Sharanya 1017 114 Morley Chris 1017 115 Garton Graham 1457 116 Nair Marc 1477 117 Baliga Lance 1018 118 Rickert Hannu 1459 119 Lamkin Bas 1029 120 Tang Greg 10210 121 Skelton Robert 10211 122 Harris Mark 14211 123 Wyld Mitchell 10211 124 Haenssgen Francis 10211 125 Ahlberg Grosek 10211 126 Bodoh Anne 102[/code] |
|
|
ngquochung
Starting Member
13 Posts |
Posted - 2006-11-27 : 18:38:07
|
select PANUM, ACADEMIC.ACNUM, FAMNAME, GIVENAME, DEPTNUMfrom AUTHOR join ACADEMIC on ACADEMIC.ACNUM = AUTHOR.ACNUMwhere PANUM in ( select PANUM from AUTHOR group by PANUM having count (ACNUM) <= all( select count (DEPTNUM) from ACADEMIC group by ACNUM ) )order by PANUM Here is the solution of mine, it's not correct yetSome tuples are missed.PANUM ACNUM FAMNAME GIVENAME DEPTNUM ----------- ----------- -------------------- -------------------- ----------- 2 102 Fernandes Russell 1004 105 Brower Tony 1006 113 Sadhal Sharanya 1018 118 Rickert Hannu 14510 121 Skelton Robert 10215 134 Owen Kenneth 10319 142 Lawitzke Ky 10520 143 Stewart Andrew 10621 144 Kinsella Declan 10623 148 Delavega Demetrios 10830 163 Peterson Stefan 11131 164 Stanton Ballintijn 111 Regards,NQH |
|
|
|