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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
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
Works exactly as I wanted, and now my reports are much faster than with recursive and numerous queries.
Thanks again :)
-Bakkulf
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,descri
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,cou
Query2 = select tableanswer.chapter,count(
Result Query = select Query1.Chapter,value1,valu
I think this will help you, if you have question let me know.
Koo