If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL group by question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-01-04, 22:47
carfield carfield is offline
Registered User
 
Join Date: Sep 2002
Location: Hong Kong
Posts: 13
SQL group by question

Let say I want to select information as state in following SQL:

<SQL>
SELECT code_orgstructure.ext_desc,
(select users.givenname from users where users.userid =
transcript_module.userid),
(select users.familyname from users where users.userid =
transcript_module.userid),
(select learningObject.learningid from learningObject where
learningObject.learningid = transcript_module.learningid),
(select learningObject.title from learningObject where
learningObject.learningid = transcript_module.learningid),
avg(CONVERT(float, AICC_core.highestscore))
FROM transcript transcript_module, code_orgstructure code_orgstructure,
AICC_core AICC_core, users users
WHERE users.userid = transcript_module.userid AND AICC_core.session_id
= transcript_module.contentSessionID AND users.leaf_orgid =
code_orgstructure.orgid GROUP BY code_orgstructure.ext_desc
</SQL>

Is it possible to get the same information without subquery? At first I
thought it can be done with

<SQL>
SELECT code_orgstructure.ext_desc, max(CONVERT(float,
AICC_core.highestscore)), users.familyname, users.givenname, learningObject.learningid, learningObject.title FROM transcript transcript_module, code_orgstructure code_orgstructure, learningObject
learningObject, AICC_core AICC_core, users users
WHERE users.userid = transcript_module.userid AND learningObject.learningid = transcript_module.learningid AND transcript_module.userid = users.userid
AND transcript_module.learningid = learningObject.learningid AND
transcript_module.contentSessionID = AICC_core.session_id AND
users.leaf_orgid = code_orgstructure.orgid GROUP BY
code_orgstructure.ext_desc
</SQL>

but of course it fail....
__________________
Visit my homepage at http://www.carfield.com.hk
Reply With Quote
  #2 (permalink)  
Old 04-02-04, 10:28
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: SQL group by question

You need to add all the non-aggregate columns to the GROUP BY clause:

GROUP BY SELECT code_orgstructure.ext_desc, users.familyname, users.givenname, learningObject.learningid, learningObject.title;
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #3 (permalink)  
Old 04-02-04, 10:49
carfield carfield is offline
Registered User
 
Join Date: Sep 2002
Location: Hong Kong
Posts: 13
Re: SQL group by question

Quote:
Originally posted by andrewst
You need to add all the non-aggregate columns to the GROUP BY clause:

GROUP BY SELECT code_orgstructure.ext_desc, users.familyname, users.givenname, learningObject.learningid, learningObject.title;
Thx for reply, however, in my example, what I like to do is select the highest marks among the whole organization, and display the organization name, highest mark, user name and related course name of that highest mark record. How can I do?

If I include the user name, and course name in group by, then it just show the highest score of that student in that course, which is not what I want... Or I should write something like

select orgname, max(username), max(course_name), max(score) ..... group by orgname

??
__________________
Visit my homepage at http://www.carfield.com.hk
Reply With Quote
  #4 (permalink)  
Old 04-02-04, 17:11
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
I'd try:
PHP Code:
SELECT
   code_orgstructure
.ext_desc
,  users.givenname
,  users.familyname
,  learningObject.learningid
,  learningObject.title
,  AICC_core.highestscore
   FROM transcript transcript_module
   JOIN AICC_core AICC_core
      ON 
(AICC_core.session_id transcript_module.contentSessionID)
   
JOIN users users
      ON 
(users.userid transcript_module.userid)
   
JOIN code_orgstructure code_orgstructure
      ON 
(code_orgstructure.orgid users.leaf_orgid)
   
WHERE  AICC_core.highestscore = (SELECT Max(za.highestscore)
      
FROM transcript zt
      JOIN AICC_core za
         ON 
(za.session_id zt.contentSessionID)
      
JOIN users users
         ON 
(zu.userid zt.userid)
      
JOIN code_orgstructure code_orgstructure
         ON 
(zc.orgid zu.leaf_orgid)
      
WHERE  zc.ext_desc code_orgstructure.ext_desc)
   
GROUP BY code_orgstructure.ext_desc 
to see what that does.

-PatP
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On