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

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

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

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)

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.

SQL query to update [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
I have this table employee with columns
name | salary
-------------
A | 5000
B | 2000
c | 1000
another table
works with columns
name| work
---------
A | w1
A | w2
A | w3
B | w4
B | w5
I want to increase salary of employee by 100 per work and update only if number of works are greater than 1.
Can anyone help me out with this. I need a sql update query for this( no stored proc or trigger or cursor).
Please try using merge statement:
MERGE
INTO employee
USING (
select distinct "name", count(*) over (partition by "name") cnt from works
)x
ON (employee."name" = x."name")
WHEN MATCHED THEN
UPDATE
SET salary = salary+(100*case when cnt=1 then 0 else cnt end);
here is proper query
update employee as a set salary = salary + 100 * NVL((
SELECT count(*)
FROM works as b
where b.name=a.name
group by name
having count(*)>1), 0)

count by column name [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
Trans|Store|Date
1 | 10 |9/01/13
2 | 10 |9/01/13
3 | 20 |9/01/13
4 | 10 |9/02/13
What I am trying to query is
(For Date = 9/01/13)
Store|#Trans|Date
10 | 2 |9/01/13
20 | 1 |9/01/13
How do I achieve this any ideas ?
You may try this:-
select store, count(*) as Transt from table
where Date = '9/01/13'
group by store
If that doesn't do it, this might.
Select
count(Store) as #Trans,
Store
from
tablename
where
Date = '9/01/13'
group by
Store
This smells like homework something fierce though.