How to get avg between two values in SQL Server [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 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.

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

Understanding Sum(1) sum(2) sum(3) [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 2 years ago.
Improve this question
Consider a table below having one column having 10 records.
I am not able to understand how
SUM(1) gives output 10
SUM(2) gives output 20
SUM(3) gives output 30
create table test_a4(idCol numeric);
insert into test_a4(idCol) values (1),(2), (3), (4), (5) , (6), (7), (8), (9) , (10)
Select SUM(1) FROM test_a4 -- SUM(1) gives output 10
Select SUM(2) FROM test_a4 -- SUM(2) gives output 20
Select SUM(3) FROM test_a4 -- SUM(3) gives output 30
Well, sum(1) does just what it says: sum() fixed integer value 1 across all rows in the table. You have 10 rows, so this produces 10 - in other words this is 1 * 10.
Same logic turns sum(2) to 20 (that's 2 * 10), and so on.
To say the least, it is quite unclear what the actual intent of this query is.
Hmmm . . . How can I explain this? Let's use fewer rows. You have a table:
id
1
2
3
Then when you do:
select sum(10)
from t
it is really calculating:
id num
1 10
2 10
3 10
Hence, you get 30. That is, the "constant" is applied to every row being summed.

Subtract values from the same Column 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 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.

Unit record of each type [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
On SQL Server 2008,I need to select the first distinct occurrence of each person in the following table:
ID WeeklyAvg MonthlyAvg
1 8 0
1 7 3
2 9 1
2 6 4
2 6 4
.......................
....................
The output should be:
1 8 0
2 9 1
How do I achieve this?
Even better if I can avoid putting all the 'distinct' columns in the group by clause,just because sql server restricts so.
Appreciate the help.
You can use ROW_NUMBER to get the "first" row:
SELECT ID, WeeklyAvg, MonthlyAvg
FROM
(
SELECT ID, WeeklyAvg, MonthlyAvg,
ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) RowNum
FROM {table}
) A
WHERE RowNum = 1
Note that the "first" row will be arbitrary unless you specify a particular order.

Remove trailing zeros from a decimal column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem ā€” and include valid code to reproduce it ā€” in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
How can I remove the trailing zero from the below table using SQL:
SKU SIZE
ABC 35.000000
QWE 36.000000
RTY 37.000000
VGY 38.000000
Expected results:
SKU SIZE
ABC 35
QWE 36
RTY 37
VGY 38
In sql server cast the column containing decimal values to Int
Select SKU,CAST (Size AS INT)
From tablename
For MySql use integer division function DIV
SELECT SKU,Size DIV 1
From Tablename
I suggest
SELECT sku, int(size)
FROM tablename
It's less typing. ;-)