Subtract values from the same Column in sql? [closed] - sql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
SELECT SUM(CASE isDeposited WHEN 0 THEN amount WHEN 1 THEN -amount END) AS total
FROM records WHERE memberID = 3;
That's my code. I've tried it and it works but didn't give me the right result.
memberID 3 has a total of 250 - 50 withdraw, which is 200 but it gives me the result of 150.

You have a problem in the calculation, (4 * 50) - 50 = 150.

Related

How can I set the value of some columns to 0 (zero) while making a SELECT in SQL? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 26 days ago.
Improve this question
I’d like to set the value of some columns to 0 (zero) inside an SQL SELECT. That is, I want to override the values of the columns I get from the database, giving them values I decide (0, in this case), something like:
SELECT Id, Name, Age AS 0, CV7 AS 0, ...
FROM ...
Is there a way to achieve that?
You can select a literal value and give it an alias in the query:
SELECT Id, Name, 0 AS Age, 0 AS CV7
FROM mytable

Take 2 digit after comma sql [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 8 months ago.
Improve this question
So, i have column with data type float. i want to take 2 digit after comma
example
0.622
i want the result is
0.62
i already tried some solution from stack overflow but still the result is 0.61.
sum(CASE WHEN a.TOTALBUDGET>bo.Bobot*2 then bo.Bobot*2 else a.totalbudget end) as UW_Rank
sum(CASE WHEN a.TOTALBUDGET>bo.Bobot*2 then bo.Bobot*2 else CAST(ROUND(a.totalbudget,2,1)AS NUMERIC(18,2)) end) as UW_Rank_Test
thanks
Did you tried round function.
declare #val float = '0.622'
SELECT ROUND(#val, 2);

summing up of two 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 3 years ago.
Improve this question
Input:
A
-----
5000
4000
-3000
-2000
Output:
A FEEDBACK
----- --------
9000 POSITIVE
-5000 NEGATIVE
THEY HAVE GIVEN THE INPUT. I NEED TO WRITE A CODE TO BRING THE OUTPUT.
I think this query should do:
select sum(a) as a, 'POSITIVE' as feedback from t where a >= 0
union
select sum(a) as a, 'NEGATIVE' as feedback from t where a < 0

How to get avg between two values in SQL Server [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Please refer to the details shown here:
ID Value
---------
1 120
1 150
Query :
Select avg(value)
from table
group by Id
Current output = 130
Expected output =
120 + 150 / 2 = 135
Please let me know your comments.
SQL Server does integer arithmetic, even for integers. However, it doesn't round integers to the nearest 10. Perhaps on your real data, the following will do what you want:
Select avg(value * 1.0)
from table
group by Id ;
It changes the value to something with a decimal point, so the average is not an integer average.

Assign values to sorted result in SQL [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 9 years ago.
Improve this question
I have got table like below after sorting in SQL:
M_ID
-----
2013/01
2013/02
2013/03
2013/04
2013/05
2013/06
Now I want to assign each entries a particular value like below
M_ID Days
--------------
2013/01 20
2013/02 30
2013/03 40
2013/04 50
2013/05 60
2013/06 70
So, can you please let me know how to do that in SQL Query?
Do you mean something like this (presuming sql-server)?
SELECT M_ID,
Days = (ROW_NUMBER()OVER(ORDER BY M_ID) + 1) * 10
FROM dbo.TableName
Demo