| Author |
Topic |
|
missinglct
Yak Posting Veteran
75 Posts |
Posted - 2004-04-22 : 11:26:09
|
| I need some help on my SQL query:Here is my query:SELECT que.que, que.QueID, ans.AnsID, ans.AnsID AS Expr1, SUM(OrgAnswers.totalHits) AS TotalFROM OrgAnswers INNER JOIN ans ON OrgAnswers.AnsID = ans.AnsID INNER JOIN que ON ans.QueID = que.QueIDWHERE (ans.QueID = 76) OR (ans.QueID = 77)After I executed, this error showed up:************************************Colume Que.que is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.************************************First of all, I would like to understand what the error meansSecond of all, can someone please point me to a good website for "GROUP BY"?Thank you. |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2004-04-22 : 11:32:50
|
| Your solution should look like this:SELECT que.que, que.QueID, ans.AnsID, ans.AnsID AS Expr1, SUM(OrgAnswers.totalHits) AS TotalFROM OrgAnswers INNER JOINans ON OrgAnswers.AnsID = ans.AnsID INNER JOINque ON ans.QueID = que.QueIDWHERE (ans.QueID = 76) OR(ans.QueID = 77)GROUP BY que.que, que.QueID, ans.AnsID, ans.AnsID1.) Any form of aggregation (Sum, Count etc) must happen at a given level. In this example you will get the sum of totalhits for each distinct value of the combination of que.que, que.QueID, ans.AnsID, ans.AnsID.2.) Books On Line is a good place to look. This site is fantastic too. google puts the SQL world at your fingertips.Duane. |
 |
|
|
missinglct
Yak Posting Veteran
75 Posts |
Posted - 2004-04-22 : 11:41:50
|
| Thank you very much for your help :) |
 |
|
|
missinglct
Yak Posting Veteran
75 Posts |
Posted - 2004-04-22 : 13:39:42
|
| One more (?) please :)Here is my sql:SELECT AnsGroup.ansgroupdesc, AnsGroup.ansgroupID, SUM(OrgAnswers.totalHits) AS TotalFROM AnsGroup INNER JOIN ans ON AnsGroup.ansgroupID = ans.ansGroupID INNER JOIN OrgAnswers ON ans.AnsID = OrgAnswers.AnsIDWHERE (ans.QueID = 76) AND (ans.ansGroupID = 1 OR ans.ansGroupID = 2 OR ans.ansGroupID = 3)GROUP BY AnsGroup.ansgroupdesc, AnsGroup.ansgroupIDORDER BY AnsGroup.ansgroupIDThis SQL works just fine but I am not happy with the WHERE clause. Why? Well, I have a table like this:ansGroupsID ansGroupDesc1 Good2 Bad3 Worse4 N/AAnd I want to display Good, bad and worse only. How can I write my WHERE clause to achieve this???? My WHERE clause does this but I wonder if there is a different way to get the same result.Thank you |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-04-22 : 13:45:56
|
| WHERE ans.ansGroupID <> 4ORWHERE ans.ansGroupDesc <> 'N/A'?Do you really need an identity column for your ans table?Tara |
 |
|
|
missinglct
Yak Posting Veteran
75 Posts |
Posted - 2004-04-22 : 13:50:04
|
| Oh, yeah. That's exactly what I am looking for, the <> sign.Tara, you asked me this: "Do you really need an identity column for your ans table?"My answer is: I think I do. I think each table should have an identity column. Am I wrong? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-04-22 : 13:53:17
|
| Yes you are wrong. There are times when you need them and times when you do not. You need them when there is no other candidate for the natural key. Your candidate is the ansGroupDesc. That should be your primary key. I would further argue that you don't even need an ans table though. Why not just store the actual value of ansGroupDesc in your ansGroup table?Tara |
 |
|
|
missinglct
Yak Posting Veteran
75 Posts |
Posted - 2004-04-22 : 14:53:56
|
| Tara, this is what you wrote: "Your candidate is the ansGroupDesc. That should be your primary key. I would further argue that you don't even need an ans table though. Why not just store the actual value of ansGroupDesc in your ansGroup table?"Before I answer your question, I would like to show you my design and then you can tell me whether or not I need an 'ans' table :)Here is what my 'ans' table look like:ansID (PK)queID (FK to 'que' table)CategoryID (FK to 'Category' table)ansdesc (description of the answer)ansGroupID (FK to 'ansgroup' table)Here is what my 'ansgroup' table look like:ansgroupID (PK)ansgroupdesc (descrtiption of the answer)"Why not just store the actual value of ansGroupDesc in your ansGroup table?"I did store the actual value of ansGroupDesc in my 'ansGroup' table. In my 'ansgroup' table, I have 2 fieldnames: 1) ansgroupID 2) ansgroupdesc.I think I can group these 2 tables into 1 big table and the new table would look something like this:ansID (PK)queID (FK to 'que' table)CategoryID (FK to 'Category' table)ansdesc (description of the answer)ansGroupID (FK to 'ansgroup' table)ansgroupdesc I think that should work as well but the reason that I want to separate them into 2 tables is that if the users want to modify the text for ansgroupdesc, all I have to do is to go to 'ansgroup' table and change 3 rows only. If I have only 1 table and store everything, then I have to go to each row and modify the changes.Fx: here is the actual data for the 'ans' table:ansID queID CategoryID ansdesc ansGroupID 1 1 1 Very great extent 12 1 1 Great extent 13 1 1 Moderate extent 24 1 1 Slight extent 25 1 1 None at all 36 1 1 Do not know 47 1 1 N/A 4Fx: here is the actual data for the 'ansGroup' table:ansgroupID ansgroupdesc1 Good2 Moderate3 None4 NoscoreOk, if I combine 2 tables together, then I get this:Fx: here is the actual data for the 'ans' table:ansID queID CategoryID ansdesc ansGroupdesc 1 1 1 Very great extent Good2 1 1 Great extent Good3 1 1 Moderate extent Moderate4 1 1 Slight extent None5 1 1 Do not know NoScore6 1 1 N/A NoScore I don't think combining 2 tables into 1 is a very good idea. Fx in this case, what if the users change the ansgroupdesc for each answer, then it would be a nightmare to go back and make the change to every single row. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-04-22 : 15:56:33
|
| I'm not saying that you shouldn't use identity columns as your primary key. I'm just saying that not every table needs one. For instance, say you have a state table. You want to store the state abbreviation and state name. It just doesn't make sense to have an identity column for this type of table as there is an obvious candidate key. You might not have one or it might take quite a few columns to get to one. We've got a table in one of our applications that takes about 10 columns for the primary key. Due to this table being involved in joins a lot, it made more sense for performance to use an identity column here.Tara |
 |
|
|
missinglct
Yak Posting Veteran
75 Posts |
Posted - 2004-04-23 : 12:42:12
|
| Hummm.....I have executed this query in SQL server and I got the results I want. But when I implemented this on the web using ASP then I get a different result:Here is what I see in SQL server when I run:Great 198Moderate 349None 49Here is what I see on the web when I run:Great 147Great 51Moderate 236Moderate 113None 49I want to combine 147 and 51 for 'Great'and 236 and 113 for 'Moderate'Does anyone know why I was able to combine this on the back end using SQL server and not on the front end????Thank you in advance, LisaHere is my SQL queries and some asp codes :******************************************************** Set Conn = Server.CreateObject("ADODB.Connection")Conn.CommandTimeout = 0conn.open strConnect' Retrieve questions, answers and totalhitsSQL="SELECT que.que, ans.ansID, AnsGroup.ansgroupdesc, AnsGroup.ansgroupID, SUM(OrgAnswers.totalHits) " & _ " FROM que INNER JOIN ans ON que.queID = ans.queID INNER JOIN " & _ " ansGroup ON ans.ansGroupID=ansgroup.ansgroupID INNER JOIN " & _ " OrgAnswers ON ans.AnsID = OrgAnswers.AnsID " & _ " WHERE (ans.QueID = 76) AND (ans.ansGroupID <> 4) " & _ " GROUP BY ans.ansID, que.que, AnsGroup.ansgroupdesc, AnsGroup.ansgroupID " & _ " ORDER BY AnsGroup.ansgroupID" Set RS=Conn.execute(SQL)' Write out the questionResponse.Write RS("que")Response.Write "<p>"' Write a Do....While....Not loop to loop all the answersDo While Not RS.EOF ansTotalHit=RS("ansID") SQL1="SELECT SUM(OrgAnswers.totalHits) " & _ " FROM que INNER JOIN ans ON que.queID = ans.queID INNER JOIN " & _ " ansGroup ON ans.ansGroupID=ansgroup.ansgroupID INNER JOIN " & _ " OrgAnswers ON ans.AnsID = OrgAnswers.AnsID " & _ " WHERE (ans.QueID = 76) AND (ans.ansGroupID <> 4) AND ans.ansID= " & ansTotalHit & _ " GROUP BY AnsGroup.ansgroupdesc, AnsGroup.ansgroupID " & _ " ORDER BY AnsGroup.ansgroupID" ' Execute SQL1 Set RS1=Conn.execute(SQL1) Sum=RS1(0).value ' Write out the ansgroupdesc and sum Response.Write RS("ansgroupdesc") & " " & Sum & "<br>" ' Move to the next answer RS.MoveNext ' End the loopLoop' Close the RSRS.closeSet RS=Nothing' Close the connectionconn.CloseSet conn=Nothing |
 |
|
|
missinglct
Yak Posting Veteran
75 Posts |
Posted - 2004-04-23 : 13:37:25
|
| Please ignore my last post. I got it. Thx. |
 |
|
|
scotchbroom
Starting Member
17 Posts |
Posted - 2004-04-23 : 16:04:21
|
| Besides just using the keys for joining, I thought they were used because they take up less resources. A related table would only have to store an int instead of the long text descriptions. Is this a misconception?Example:1 Software Engineering2 Human Resources3 Sales4 Finance5 Project OperationsIt's more conducive to use the int (key) field than to have the text repeated in a related field, is it not? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-04-23 : 16:17:33
|
| It's been debated here a bunch:http://www.sqlteam.com/item.asp?ItemID=2599http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=6136Do a search on natural key in the forums and you'll see even more threads.Tara |
 |
|
|
|