Link to home
Start Free TrialLog in
Avatar of henrikba
henrikba

asked on

General SQL (SQLite): Sum/Count based on values in the dataset

I have a database in SQLite (which supports subqueries and standard aggregate functions).

One table describes the structure (chapters), one stores answers to questions, related to each chapter, and one table stores answer types. I want to get out data on each chapter, with a count on each of the values in the answer type table.

The data model is (simplified) like this:

Table Structure
+-------------+------------+
| Field       |  Type      |
+-------------+------------+
| Chapter     | int        |
| Description | varchar    |
+-------------+------------+
 
Table Answer
+-----------+----------+
| Field     |  Type    |
+-----------+----------+
| Answer    | int      |
| Chapter   | int      |
+-----------+----------+
 
Table AnswerOption
+-------------+------------+
| Field       |  Type      |
+-------------+------------+
| AnswerOptId | int        | (relates to Answer.Answer)
| Description | varchar    |
+-------------+------------+


I would really like one(!) query that gives a result like this:
+---------+---------+---------+---------+---------+
| Chapter | Value 1 | Value 2 | Value 3 | Value 4 |
+---------+---------+---------+---------+---------+
|       1 |       3 |       2 |      15 |       6 |
|       2 |      11 |       0 |       7 |      16 |
|       3 |       6 |       1 |      12 |       1 |
+---------+---------+---------+---------+---------+
Where Value N corresponds to each row in the AnswerOption table.

Please let me know if you need more information - the question is complicated to explain, but might be even harder to crack :)

Br,
Henrik
Avatar of netcool
netcool
Flag of Malaysia image

Hi,

 My understanding is you have three table as
1. Table Structure - fields - Chapter,Descripition.
2. Table Answer - fields - Chapter,Answer
3. Table AnswerOption -fields - Answer,Answeroption,description.

now you have to find the sum and count of each chapter field in first table.

you can generate Query as

Query1 = select tablestructure.chapter,count(answer) as value1,Sum(answer) as value2 from tablestructure,TableAnswer where tablestructure.chapter = tableAnswer.chapter Group by tablestructure.chapter

Query2 = select tableanswer.chapter,count(answeroption) as value3,sum(answeroption) as value4 from tableAnswer,tableAnswerOption where tableanswer.Answer = tableansweroption.answer Group by tableanswer.chapter

Result Query = select Query1.Chapter,value1,value2,value3,value4 from Query1,Query2 where Query1.Chapter = Query2.Chapter

I think this will help you, if you have question let me know.

Koo  
ASKER CERTIFIED SOLUTION
Avatar of mar1ner
mar1ner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of henrikba
henrikba

ASKER

Hi, guys!!
Terribly sorry for the long wait; I've been on vacation and totally forgot all about this case.
I'll check both solutions today and get back to you ASAP.
-H
mar1ner; thanks a lot for this comprehensive solution! I have now tried it in my own project and it turns out to work perfectly with some adaption to my slightly more complex datamodel.

Works exactly as I wanted, and now my reports are much faster than with recursive and numerous queries.

Thanks again :)
-Bakkulf