SQL QUERY SQL SSRS [closed] - sql

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)

Related

Postgres: Count occurences of integer in an array in a result set [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 1 year ago.
Improve this question
I am trying to count the number of of occurences of integers in an array for each record.
My table is this:
customerid
groups
1
{324,299,523,534,565,212}
2
{324,299,523,534,565,212}
3
{324}
4
{324,299,523,212}
I would like to return the following:
groupid
count
324
4
299
3
523
3
etc
I have tried looping through the cursor and storing count in map but I would like something more performant.
select
groupid,
count(*)
from (
select unnest(groups) as groupid
from table_name
) data
group by groupid
order by count desc
you can use unnest:
select unnest(groups) groupid
,count(*) count
from customergroups
group by groupid
order by count desc

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;

How to use Sum() function with condition? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have an Access database. There is a table named cost with bottom value:
reson Cost Type
------------ ------ ------
A1 2500 1
A1 6500 1
A2 95000 2
A3 2500 1
A1 6500 1
A4 50000 2
Now I want a query that calculate sum of all cost filed where type = 2 and sum of cost filed where type = 1 and substract the first value from the second value.
For example, the above pic calculate final result:
Sum of Type 2 = 145000
Sum of Type 1 = 18000
-------------------------
Final Result = 127000
My Sql Code
select iif(type = 2, sum(cost), -sum(cost)) As col1 from cost group by type
First off, I'm sorry you have to deal with such obnoxious hostility when asking your question here. You asked your question perfectly fine, laying out your table structure, and your desired result. It's understandable that you are new to queries and need help creating them. Not every answer requires code, and not every person knows where to start.
Here is your answer:
Step 1
Make sure you have your table created with the data you provided
Step 2
Create a new query named qySumType1. Build it like this, so it sums everything of type=1. make sure to click the totals button.
Step 3
Create another query, name this one qySumType2. This query should sum everything of type=2.
Step 4
Now create another query called "Final". Add both of your previous queries to it. Now create an expression in the last column to calculate the difference between the 2 numbers. Just like this.
And there you have it. Now just run the Final query anytime you want to get the difference.
Hope this helps! I can't tell you how many times I've started learning something new and relied on a community to help me get started. Always just try your best and wait for a decent answer to your question. Good luck!
Change T1 to the name of your table.
SELECT Sum(T.Type1) AS Type1, Sum(T.Type2) AS Type2, Sum(T.Type2) - Sum(T.Type1) AS DIFF
FROM
(
SELECT Sum(T1.Cost) AS Type1, 0 AS Type2
FROM T1
WHERE (((T1.Type)=1))
UNION
SELECT 0 AS Type1, Sum(T1.Cost) AS Type2
FROM T1
WHERE (((T1.Type)=2))
) AS T;
Type1 | Type2 | DIFF
18000 | 145000 | 127000

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

Having 1 distinct column and a sum value from another column [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I would like to have a solution to this question:
I have a datatable here, as shown below
cid amount
1 5
1 10
2 2
3 5
3 7
3 11
Now I need to write a statement that returns a distinct cid, and the sum of the amount column for each cid. The table should look as below:
cid amount
1 15
2 2
3 23
What is the best way to do this? Thanks.
select cid, sum(amount) amount from table group by cid
Try this way:
select cid, sum(amount)
from tab
group by cid
select cid, sum(amount)
from table_name
group by cid;
select cid,sum(amount) from c1 group by cid