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

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

Related

Comma separated values to results

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

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

Pandas : dataframe cumsum , reset if other column is false [duplicate]

This question already has an answer here:
How to reset cumsum after change in sign of values?
(1 answer)
Closed 4 years ago.
I have a dataframe with 2 columns, the objective here is simple ; reset the df.cumsum() if a row column is set to False;
df
value condition
0 1 1
1 2 1
2 3 1
3 4 0
4 5 1
the wanted result is as follows :
df
value condition
0 1 1
1 3 1
2 6 1
3 4 0
4 9 1
If i loop over the dataframe as described in this post Python pandas cumsum() reset after hitting max
i can achieve the wanted results, but i was looking for a more vectorized way using pandas standard functions
How about:
df['cSum'] = df.groupby((df.condition == 0).cumsum()).value.cumsum()
Output:
value condition cSum
0 1 1 1
1 2 1 3
2 3 1 6
3 4 0 4
4 5 1 9
You'll group consecutive rows together until you encounter a 0 in the condition column, and then you apply the cumsum within each group separately.

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**

Using temporary extended table to make a sum

From a given table I want to be able to sum values having the same number (should be easy, right?)
Problem: A given value can be assigned from 2 to n consecutive numbers.
For some reasons this information is stored in a single row describing the value, the starting number and the ending number as below.
TABLE A
id | starting_number | ending_number | value
----+-----------------+---------------+-------
1 2 5 8
2 0 3 5
3 4 6 6
4 7 8 10
For instance the first row means:
value '8' is assigned to numbers: 2, 3 and 4 (5 is excluded)
So, I would like the following intermediairy result table
TABLE B
id | number | value
----+--------+-------
1 2 8
1 3 8
1 4 8
2 0 5
2 1 5
2 2 5
3 4 6
3 5 6
4 7 10
So I can sum 'value' for elements having identical 'number'
SELECT number, sum(value)
FROM B
GROUP BY number
TABLE C
number | sum(value)
--------+------------
2 13
3 8
4 14
0 5
1 5
5 6
7 10
I don't know how to do this and didn't find any answer on the web (maybe not looking with appropriate key words...)
Any idea?
You can do what you want with generate_series(). So, TableB is basically:
select id, generate_series(starting_number, ending_number - 1, 1) as n, value
from tableA;
Your aggregation is then:
select n, sum(value)
from (select id, generate_series(starting_number, ending_number - 1, 1) as n, value
from tableA
) a
group by n;