473,387 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question about query

112 100+
Hi there -

I have two tables, of which table 1 records who have opened an application and table 2 records their answers.

table 1:
Std_id, Name, Status

table 2:
Std_id, Q_id, Answers

where q_id is question id.
Table 1 has one record for each student who has answered questions/just opened an application and table 2 has multiple rows for each student. The table 2 has answers given by each student for 10 questions.

I want to extract the students who have opened the application and also want the answer for question number 5.
I was using the following query,
Expand|Select|Wrap|Line Numbers
  1. Select  app.Std_id,Name as ApplicantName, Status
  2. , NVL((select TRIM (DBMS_LOB.SUBSTR (ans.Answers, 20, 1)) from tabl2  where Q_ID = 5 ), 'N.A') AS School 
  3.  from tabl1  app 
  4. inner join tabl2 ans on APP.Std_id = ANS.Std_id  WHERE  Status  in ('I','C')
this query returns multiple rows and I understand why.As there is an inner join and table 2 has different answers and q_id, so the join picks up every row from table 2. How can I avoid this,also the NVL on select does not work. It picks up all the answers. I want to pick only answer for q_id 5.

can some one please guide me on how can I achieve just one row per student entry. thank you.
Jan 16 '12 #1
8 1843
C CSR
144 100+
I think you need a "Group By" segment in the query
Jan 16 '12 #2
user1980
112 100+
I tried it and unfortunately that does not work.thank you
Jan 16 '12 #3
C CSR
144 100+
Try some form of this:

Expand|Select|Wrap|Line Numbers
  1. SELECT Questions.qID, Students.StudentID, Results.Answer, Results.RecID
  2. FROM Students INNER JOIN (Questions INNER JOIN Results ON Questions.qID = Results.qID) ON Students.StudentID = Results.StudentID
  3. WHERE (((Questions.qID)=2));
  4.  
3 tables: Students, Questions, Results (Results contains the Question ID and the Student ID).

It gets the question you need, pulls all students answering that question and gives answers.
Jan 16 '12 #4
C CSR
144 100+
Note: The Results tbl is akin to your "table2". The Student tbl should match your "Table1," and what I did differently besides changing the nature of the query is to split off your Questions into a separate table. Inner Join and enjoy...
Jan 16 '12 #5
user1980
112 100+
thank you so much for the suggestion. I will work on this.Your query works as required but it does not pick up students who have no record corresponding to the question id 2 ie students can have a record in the answers table and students table even if they have no answer for that question.
I will follow your lead and try to figure how it can be achieved. thank you once again.
Jan 16 '12 #6
C CSR
144 100+
I tested this thoroughly. It completes the Student, Questions and Answers triangle with the holes (unanswered questions) on one side. Its a Nested Select statement that finds the question you search on and produces the list of test-takers that did and did not answer all of the questions, showing the questions, answers and un-answers.

Expand|Select|Wrap|Line Numbers
  1. SELECT Z1.qID, Z1.Question, Z1.StudentID, Z1.Name, Results.RecID, Results.Answer
  2. FROM (SELECT Questions.qID, Questions.Question, Students.StudentID, Students.Name 
  3. FROM Questions, Students)  AS Z1 
  4. LEFT JOIN Results ON (Z1.qID = Results.qID) AND (Z1.StudentID = Results.StudentID);
  5.  
The setup is 3 tables: The first two are Questions [3] and Students [4]. Since I have 3 X 4 = 12, I should always have 12 records in my final query if do not specify any criteria. If I search on any one question, it produces 4 results (1 x 4) whether the question was answered or not.

For testing in my case, I set the 3rd table "Results" with only 10 questions answered because Student 4 didn't answer two in his set of three, and I assume he clicked "skip." (The last query I sent your way only produced the 10 "answered" records). By nesting a SELECT statement, I can get 12 results so you can see who didn't answer what (Student 4 is now listed 3 times, once for each question, but 2 of his answers are blank. I believe this is what you said you want.

I did this in Access so it can be pasted in as an SQL command manually and then you can see the graphic depiction. But Access will not automatically produce this query in the GUI by itself with a drag & drop. You may need to conform it to your platform. Of course, you'd have to set up 3 tables to accommodate the "aggregate attributes," (or field names), in my specific query. Otherwise create what you want and make some naming changes appropriately. Note that you have to apply an "alias" to the nested Select as a temporary table AND the outer attributes, designated in my sample as "Z1". The syntax is sensitive to minor changes in the structure but not with expanding the list of attributes.

Basic Definitions used for tables:

"Questions" Table contains its record IDs and the questions. "Students" contains its record IDs and the student names. "Results" will contain its record IDs, the student's Answers, and the corresponding Question IDs and Student IDs that I assume you would input programmatically as the Student signs in to take the test.

Let me know what you think if you still have time.
Jan 17 '12 #7
C CSR
144 100+
You can use this query to create a final report or stick the results into tangible storage.
Jan 17 '12 #8
user1980
112 100+
than you for the explanation. This is exactly what I was looking for.

thank you for your time.
Jan 17 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Adam Sowalsky | last post by:
Hi, newbie question ahead: suppose i have two html-formatted documents with appropriately coded <?php ?> areas. a typical query is this: file.php?call=123.html in 123.html i define a...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
0
by: Ryan J. Allen | last post by:
Forgive me if this is the wrong group to post to. I have a table and the relevant bits look like this: CREATE TABLE "records" ( "id" SERIAL, "number" integer NOT NULL, "timestamp" timestamp...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
2
by: mmitchell_houston | last post by:
I'm working on a .NET project and I need a single query to return a result set from three related tables in Access 2003, and I'm having trouble getting the results I want. The details: ...
22
by: Stan | last post by:
I am working with Access 2003 on a computer running XP. I am new at using Access. I have a Db with a date field stored as mm/dd/yyyy. I need a Query that will prompt for the month, ie. 6 for...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.