Understanding Sum(1) sum(2) sum(3) [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 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.

Related

How to do running subtraction 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 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;

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

transpose some SQL columns into 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 9 years ago.
Improve this question
ID YEAR FORCE NEIGHBOURHOOD ALL_CRIME ANTI_SOCIAL_BEHAVIOUR BURGLARY CRIMINAL_DAMAGE_AND_ARSON DRUGS OTHER_THEFT PUBLIC_DISORDER_AND_WEAPONS ROBBERY SHOPLIFTING VEHICLE_CRIME VIOLENT_CRIME OTHER_CRIME
1 2013-03 Police AAN_HP 290 91 27 33 11 64 8 6 3 14 27 6
i need to change the order of theses into something like
ID 1
YEAR 2012
FORCE Police
NEIGHBOURHOOD Bradford
ALL_CRIME 12345
ANTI_SOCIAL 87
BURGLARY 10
CRIMINAL_DAMAGE 20
DRUGS 15
OTHER_THEFT 30
If you want to changed the order in which the columns are returning change the order in which you are selecting them
SELECT 1,2,3 FROM t
Would return differently from
SELECT 2,1,3 FROM t
I don't know if this is what you wanted but I found the question confusingly worded!
Quite easy to do with a query:
with lvls as (select /* + materialize */ level lv from dual connect by level <= 9)
select id,
decode(lv,
1,'year',
2,'force'),
...
decode(lv,
1,to_char(year),
2,to_char(force),
...
from yourtable, lvls
order by id,lv;
hint "materialize" will not make Oracle be unperformant (he likes to join here the tables first and then run hierarchy if you do not specify this hint)
try this
select * from your_tab unpivot(col2 for col1 in(ID,YEAR,FORCE,NEIGHBOURHOOD, ALL_CRIME,ANTI_SOCIAL_BEHAVIOUR);
note:All columns mentioned in col1 must be of same datatype

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.