Comma separated values to results - sql

I have 3 tables namely Question, Feedback and FeedbackResult. I wish to generate a report where I need to find the rating average against each question.
Questions
---------
QuestionId Question
1 Question 1
2 Question 2
3 Question 3
4 Question 4
5 Question 5
Feedback
--------
FeedbackId Questions QuestionCount
2 1,2,3,4,5 5 -- Questions column has questionid from Question table
FeedbackResults
---------------
FeedbackResId FeedbackId Answers
1 2 4,3,5,2,3 -- these answers are ratings(1 to 5) against each question
2 2 4,2,3,4,5 -- which means 4 is the rating for QuestionId 1, 2 is for QuestionId 2 etc
3 2 5,3,4,3,2
4 2 4,1,1,1,2
I wish to get result as average rating against each question
Question Rating
Question 1 3.5
Question 2 4
Question 3 5
Question 4 2
Question 5 4.5
Edit
Should I redesign the database table as
FeedbackResults
---------------
FeedbackResId FeedbackId QuetionID Answers UserId
1 2 4 4 1
2 2 1 3 1
3 2 5 3 1
4 2 1 2 2

Related

How to count how many times a name appear? [duplicate]

This question already has answers here:
How to count number of occurrences for all different values in database column?
(2 answers)
Closed 3 years ago.
I have a table like the following:
ID X Y
5 2 0
5 1 1
5 3 3
4 -2 1
4 0 0
3 5 -3
I would like to count how the records for each ID
ID count
5 3
4 2
3 1
This is pretty much all
SELECT ID, COUNT(*) AS count FROM TABLE GROUP BY ID

Change columns every 5 rows in a SQL query [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a table of data and have some records.
I want to change every fifth row of data and update the column ParentId to be equal of first row Id of five row group - like this:
Id ParentId ....
-------------------------------
1 1
2 3
3 10
4 5
5 5
6 5
7 2
8 2
9 2
10 2
11 2
12 7
13 7
and result I need in SQL Server should be like this:
Id ParentId ....
-------------------------------
1 1
2 1
3 1
4 1
5 1
6 6
7 6
8 6
9 6
10 6
11 11
12 11
13 11
...
You can use this code if you want update your table:
Update YourTable set ParentId= (Ceiling(Id*1.0/5)-1)*5 + 1
and if you want just select rows use this :
select Id, (Ceiling(Id*1.0/5)-1)*5 + 1 as ParentId from yourtable
update myTable
set ParentId = 5 * (Id / 5) + 1
where Id % 5 != 0
update myTable
set ParentId = 5 * ((Id /5) - 1) + 1
where Id % 5 = 0

reorder sort_order in table with sqlite

I have this table:
id sort_ord
0 6
1 7
2 2
3 3
4 4
5 5
6 8
Why does this query:
UPDATE table
SET sort_ord=(
SELECT count(*)
FROM table AS pq
WHERE sort_ord<table.sort_ord
ORDER BY sort_ord
)
WHERE(sort_ord>=0)
Produce:
id sort_ord
0 4
1 5
2 0
3 1
4 2
5 4
6 6
I was expecting all sort_ord fields to subtract by 2.
Here is defined: https://www.sqlite.org/isolation.html
About this link i can interpret, you has several instances for one query (update table and select count table) and independent of each other.
When you are in update sort_data(5) id 5, you have new data for read on every "SET sot_ord" (understanding what say about isolation), and now the result is 4.
Every select is a new instance and a new data reading
id sort_ord
0 4
1 5
2 0
3 1
4 2
5 5**
6 8**

sql HAVING & averages [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Learning the having clause, and averages (did AVG before), and I have to admit, I'm confused.
I'll post a problem here, one of the simpler ones on this weeks' assignment, and ask someone to explain it to me in 'real world' terminology (not the textbook language, which is sparse at best).I kinda see what I need to do, but averaging multiple values within same column has me somewhat confused, as does using 'Having' over 'Where'.
SQL> desc inventory
Name
----------------------------
BOOK_CODE
BRANCH_NUM
ON_HAND
SQL> SELECT * FROM INVENTORY;
BOOK BRANCH_NUM ON_HAND
---- ---------- ----------
0180 1 2
0189 2 2
0200 1 1
0200 2 3
0378 3 2
079X 2 1
079X 3 2
079X 4 3
0808 2 1
1351 2 4
1351 3 2
1382 2 1
138X 2 3
2226 1 3
2226 3 2
2226 4 1
2281 4 3
2766 3 2
2908 1 3
2908 4 1
3350 1 2
3743 2 1
3906 2 1
3906 3 2
5163 1 1
5790 4 2
6128 2 4
6128 3 3
6328 2 2
669X 1 1
6908 2 2
7405 3 2
7443 4 1
7559 2 2
8092 3 1
8720 1 3
9611 1 2
9627 3 5
9627 4 2
9701 1 2
9701 2 1
9701 3 3
9701 4 2
9882 3 3
9883 2 3
9883 4 2
9931 1 2
Maybe this helps:
You know how SELECT determines which COLUMNS to return? And you use WHERE in order to choose which ROWS will be returned? Obvious so far, right. But when you aggregate values from multiple rows together into one returned value, then you have to use HAVING if you want to perform some filtering on those.
For example:
SELECT author, title
FROM books
WHERE author = 'Twain, Mark'
would give you the list of all the books Mark Twain wrote.
And
SELECT author, count(*)
FROM books
GROUP BY author
would give you the count of books written by each author.
Finally, if you wanted only those authors who had written 3 or more books:
SELECT author, count(*)
FROM books
GROUP BY author
HAVING count(*) >= 3
I intentionally simplified the data here, but you can probably see how it applies. Another example could be to show all the authors whose books average less than $10:
SELECT author, average(price)
FROM books
GROUP BY author
HAVING average(price) < 10
If you tried to use WHERE here, all you could do is filter out rows in which the price of a single book was less:
SELECT author, average(price)
FROM books
GROUP BY author
WHERE price < 10
That would be the wrong answer of course, mostly because it would fail. But even if it returned a result, it would be the average price of all books less than $10 -- not the same at all.

SQL - conditional statements in crosstab queries - say what

I am working with MS Access 2007. I have 2 tables: Types of Soda, and Likeability.
Types of Soda are: Coke, Pepsi, Dr. Pepper, and Mello Yellow
Likeability is a lookup with these options: Liked, Disliked, No preference
I know how to count the number of Cokes or Mello Yellows in the table using DCount("[Types]", "[Types of Soda]", "[Types]" = 'Coke')
I also know how to count the number of Liked, Disliked, No preference.
("[Perception]", "[Likeability]", "[Perception]" = 'Liked')
But, what if I need to count the number of "Likes" by Type.
i.e. the table should look like this:
Coke | Pepsi | Dr. Pepper | Mello Yellow
Likes 9 2 12 19
Dislikes 2 45 1 0
No Preference 0 12 14 15
I know in Access I can create a cross tab queries, but my tables are joined by an ID. So my [Likeability] table has an ID column, which is the same as the ID column in my [Types] table. That's the relationship, and that's what connects my tables.
My problem is that I don't know how to apply the condition for counting the likes, dislikes, etc, for ONLY the Types that I specify. It seems like I first have to check the [Likeability] table for "Likes", and cross reference the ID with the ID in the [Types] table.
I am very confused, and you may be too, now. But all I want to do is count the # of Likes and Dislikes for each type of soda.
Please help.
Its not really clear (to me anyway) what your tables look like so lets assume the following
tables
Soda
------
Soda_ID (Long Integer (Increment))
Soda_Name (Text(50)
Perception
------
Perception_ID (Long Integer (Increment))
Perception_Name (Text(50)
Likeability
-----------
Likeability_ID (Long Integer (Increment))
Soda_ID (Long Integer)
Perception_ID (Long Integer)
User_ID (Long Integer)
Data
Soda_Id Soda_Name
------- ---------
1 Coke
2 Pepsi
3 Dr. Pepper
4 Mello Yellow
Perception_ID Perception_Name
------------- ---------
1 Likes
2 Dislikes
3 No Preference
Likeability_ID Soda_ID Perception_ID User_ID
-------------- ------- ------------- -------
1 1 1 1
2 2 1 1
3 3 1 1
4 4 1 1
5 1 2 2
6 2 2 2
7 3 2 2
8 4 2 2
9 1 3 3
10 2 3 3
11 3 3 3
12 4 3 3
13 1 1 5
14 2 2 6
15 2 2 7
16 3 3 8
17 3 3 9
18 3 3 10
Transform query You could write a query like this
TRANSFORM
Count(l.Likeability_ID) AS CountOfLikeability_ID
SELECT
p.Perception_Name
FROM
Soda s
INNER JOIN (Perception p
INNER JOIN Likeability l
ON p.Perception_ID = l.Perception_ID)
ON s.Soda_Id = l.Soda_ID
WHERE
p.Perception_Name<>"No Preference"
GROUP BY
p.Perception_Name
PIVOT
s.Soda_Name;
query output
Perception_Name Coke Dr_ Pepper Mello Yellow Pepsi
--------------- ---- ---------- ------------ -----
Dislikes 1 1 1 3
Likes 2 1 1 1