How to do running subtraction in SQL [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I know how to do running sum but can't figure out how to do running subtract.
Example: we have single column of data and a new column will be formed for running subtraction.
Value
------
10
1
3
4
Output needs to be:
10
9
6
2
I need both these columns next to each other
Any suggestion please.

Your question only makes sense if you have a column that specifies the ordering. In that case:
select (case when row_number() over (order by <ordercol>) = 1
then col
else 2 * first_value(col) over (<ordercol>) - sum(col) over (order by <ordercol>)
end) as output
from t;
That is, return the column value on the first row. Otherwise, the math is a little tricky. But you want the first value minus the sum of the rest of the column. Arithmetically, this is the same as twice the first value minus the cumulative sum.
EDIT:
As Shawn points out, this can be simplified to:
select 2 * first_value(col) over (<ordercol>) - sum(col) over (order by <ordercol>) as output
from t;

Related

Auto increment for duplicate rows [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
I have a table with column name No, which have duplicate values (which is necessary). I need to add an incrementing value in another column sub_no
For Example:
No
sub_no
1
0
1
1
1
2
2
0
2
1
3
0
3
1
3
2
3
3
I have tried update and select queries.
A window function will give you the results you want:
SELECT [No], ROW_NUMBER() OVER(PARTITION BY [No] ORDER BY [No]) - 1 AS
[sub_no]
FROM <table_name>;
You can find more information on window functions in T-SQL here.

Count by ID if value is lower than and set the value 1 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a problem.
My table is tableXYZ
I have a row a where i set the year.
I have a row b where i need to set value 1 if the year from row a is lower than 2019 and 0 if is > 2019.
After all i need to make a count from row b only the 1 values as total.
How can i do that, because i tried a lot of examples but doesn't work.
This might help:
SELECT COUNT(1) AS [b_count]
FROM (
-- Use case to evaluate column a and set value for column b
SELECT a,
CASE WHEN a < 2019 then '1' else '0' end as [b]
FROM tableXYZ
) AS X -- Alias of subquery
WHERE X.b = '1' -- Select only rows where b = '1'

SQL QUERY SQL SSRS [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Hi can anyone help me with sum up first two rows in table and then rest would be same. example is
ID SUM
12 60
0 20
1 30
2 50
3 60
I am expecting
ID SUM
0 80
1 30
2 50
3 60
I am doing this from memory - so if this doesnt work let me know and we can do it another way looking at the row number;
Assuming you have a unique ID to sort it by as you suggested, you could do something like this;
you may want to change the order to be desc if that's how you classify your 'top 2'
SELECT TOP 2 ID,
SUM(VALUE)
FROM [Table]
GROUP BY ID
ORDER BY ID
UNION
SELECT ID,
VALUE
FROM [Table]
WHERE ID NOT IN (SELECT TOP 2 ID
FROM [Table] ORDER BY ID)

How can I count repeat rows in SQL Server 2012? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to assign one unique number for each repeated row in select statement.
table
Name | Position
Mark Wol 1
Mark wol 1
MArtha 1
Martha 1
and I want this
1 Mark Wl 1
2 Martha 1
You can use as few or as many rows as you want. If you want to use all of the rows, list them all.
SELECT Row1, Row2, Row3,
ROW_NUMBER() OVER (
PARTITION BY Row1, Row2, Row3,
ORDER BY Row1
)
FROM Table
The best solution is to already have a unique ID for each record and then you are choosing to show that (which will come from the first found record)...
SELECT name,COUNT(*),id
FROM test_table
GROUP BY name
HAVING COUNT(*) > 1

SQL select statement to Exclude a column from being sorted [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am looking for a way to exclude a single column from being sorted using SQL select statement.
Lets say a table has 10 columns and when an ORDER BY is done on a specific column using select statement, all the 10 columns are sorted. Is it possible to exclude any specific column (say column 6) from being sorted? If so how.
**
Before
ID name
-----------
1 papaya
2 apple
3 strawberry
4 banana
Required
ID name
-----------
1 apple
2 banana
3 papaya
4 strawberry
Appreciate your help
This will generate the ids and they will be always shown in order but that won't be the actual id, you might need that as well
SELECT #a:=#a+1 id,name from tableName order by name