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

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

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

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

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

Pivoting on multiple columns

I have the following computed data
Pl_Name Cnt Status Level
CF 21 Active 2
CF 3 Closed 2
INFRA 1 Closed 3
OTHER 40 Active 2
OTHER 4 Active 3
OTHER 2 Closed 1
OTHER 9 Closed 2
OTHER 5 Closed 3
MSG 5 Active 2
The query used for above result was
SELECT Platforms.PlatformName,COUNT(Ticket.TicketId) AS Count,Ticket.TicketStatus,Ticket.Priority
FROM Platforms
INNER JOIN Ticket
ON Platforms.ID = Ticket.Platform_id
GROUP BY Platforms.PlatformName,Ticket.TicketStatus,Ticket.Priority
Now I Need the following result from above result
Level1 Level2 Level3
PL_Name Active Closed Active Closed Active Closed
CF 0 0 21 3 0 0
INFRA 0 0 0 0 0 1
OTHER 0 2 40 5 4 5
MSG 0 0 2 0 0 0
Try this .. You need to use pivot to transpose your rows to columns.
SELECT Pl_Name,
Isnull([Level1_active], 0)[Level1_active],
Isnull([Level1_closed], 0)[Level1_closed],
Isnull([Level2_active], 0)[Level2_active],
Isnull([Level2_closed], 0)[Level2_closed],
Isnull([Level3_active], 0)[Level3_active],
Isnull([Level3_closed], 0)[Level3_closed]
FROM (SELECT Pl_Name,
cnt,
'Level' + CONVERT(VARCHAR(50), level) + '_'
+ status piv_col
FROM Computeddata) a
PIVOT (Max(cnt)
FOR piv_col IN([Level1_active],
[Level1_closed],
[Level2_active],
[Level2_closed],
[Level3_active],
[Level3_closed])) piv
Note: Result is some what similar to your result column headers might be same as you expect

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;