Q:
SQL query to sum the values in multiple columns in specific rows
I have an SQL table that includes values in multiple columns for different rows.
I am trying to sum the values in the first two columns for a specific ID value
EXAMPLE
ID Value1 Value2 Value3
-----------------------
1 3 2 5 2
should become:
1 8 8 8 8
A:
select
id,
sum(value1) as value1,
sum(value2) as value2,
sum(value3) as value3
from your_table
group by id
If your ID can be duplicated you can use ROW_NUMBER window function
WITH cte AS(
SELECT ID, [Value1], [Value2], [Value3],
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID ASC) as rn
FROM YourTable
)
SELECT ID, SUM([Value1]), SUM([Value2]), SUM([Value3])
FROM cte
ORDER BY ID ASC
. And, in my case, I felt that I needed to do something to help the cause. I did not know what that something was at the time, but I just went and did it. In the end, it was another woman who had an idea that made it possible.
FRANCES: If you could go back to before the accident, would you do things differently?
RICK: I would not have taken my eyes off of the road for such a long time. It is interesting that I do have “time to think” after the accident, but it is so short. I would have done something else – something more positive and helpful, I am sure.
FRANCES: What advice do you have for other victims of similar incidents?
RICK: It is so helpful to have support, whether it be from friends, family, or people at the hospital. Your support can make all the
Related links:
Comments