id group sal
----------------
3 a 1000
3 b 500
2 c 2000
I need the result like this.
id sum
-------
3 1500
2 2000
SELECT id
, SUM(sal) 'sum'
FROM yourtablename
GROUP BY id;
I think you're looking for something like this.
You could group by the id and sum the sal:
SELECT id, SUM(sal)
FROM sometable
GROUP BY id
Related
A Table Dept
Id Salary Dept
1 1000 A
2 2000 B
3 5000 A
4 2500 C
5 3000 D
So the Output
Id Salary Dept
1 6000 A
Need To Get the Sum of Salary and Which department as the max salary using Join or Window function
You seem to want:
select d.dept, sum(d.salary)
from dept d
group by d.dept
order by max(d.salary) desc
fetch first 1 row only
If you are using MSSQL you can try this
select top 1 d.Dept, Sum(d.Salary) as Salary from Dept d group by d.Dept order by Sum(d.Salary) desc
I have one database and time to time i change some part of query as per requirement.
i want to keep record of results of both before and after result of these queries in one table and want to show queries which generate difference.
For Example,
Consider following table
emp_id country salary
---------------------
1 usa 1000
2 uk 2500
3 uk 1200
4 usa 3500
5 usa 4000
6 uk 1100
Now, my before query is :
Before Query:
select count(emp_id) as count,country from table where salary>2000 group by country;
Before Result:
count country
2 usa
1 uk
After Query:
select count(emp_id) as count,country from table where salary<2000 group by country;
After Query Result:
count country
2 uk
1 usa
My Final Result or Table I want is:
column 1 | column 2 | column 3 | column 4 |
2 usa 2 uk
1 uk 1 usa
...... but if query results are same than it shouldn't show in this table.
Thanks in advance.
I believe that you can use the same approach as here.
select t1.*, t2.* -- if you need specific columns without rn than you have to list them here
from
(
select t.*, row_number() over (order by count) rn
from
(
-- query #1
select count(emp_id) as count,country from table where salary>2000 group by country;
) t
) t1
full join
(
select t.*, row_number() over (order by count) rn
from
(
-- query #2
select count(emp_id) as count,country from table where salary<2000 group by country;
) t
) t2 on t1.rn = t2.rn
SQL> SELECT * FROM student;
NAME ID AGE MARK1 MARK2 TOTAL
-------------------- ---------- ---------- ---------- ---------- -----------
Ananda 200 22 90 95
Chris 250 18 80 75
Gokul 325 17 50 50
SQL> SELECT MAX(mark1),name FROM student;
SELECT MAX(mark1),name FROM student
*
ERROR at line 1:
ORA-00937: not a single-group group function
As you can see the error,
can anyone suggest me a query to select the Maximum mark from the table
and display it along with the corresponding name of the student??
Is it even possible without using GROUP BY clause?
As you can see, there's no logical way of using GROUP BY clause here.
If you want to get the name of the student also, you need to use a join:
SELECT T2.Mark,T1.name
FROM student T1 JOIN
(SELECT MAX(Mark1) as Mark
FROM student) T2 on T1.Mark=T2.mark
Result:
MAXMARK NAME
------------
90 Ananda
Sample result in SQL Fiddle
If am not wrong you just need Order by and ROWNUM
select * from
(
SELECT mark1,name FROM student Order by Mark1 desc
)
Where ROWNUM = 1
or You can use Analytic functions
select * from
(
SELECT Row_number()over(order by Mark1 desc) as RN,mark1,name FROM student
)
Where RN = 1.
If there is a tie in max mark and you want all the rows with max marks then use Dense_Rank instead of Row_number
I found an easy solution:
SELECT mark1,name
FROM student
WHERE mark1=
(SELECT MAX(mark1) FROM student);
Result:
MARK1 NAME
----- -------
90 Ananda
I have a table in which has duplicate values. by eliminating these values I want sum of distinct values. but without group by.
My table
--------------------------------------
ID City collection
---------------------------------------
1 xyz 5000
2 xyz 5000
3 abc 2000
4 pqr 3000
5 xyz 5000
6 pqr 3000
7 abc 2000
-----------------------------------
I want result of whole collection column but eliminate city name
ex. Result = 10000
not like
xyz 15000
abc 4000
pqr 6000
or not like 25000
but result should be 10000 by eliminated
Get the Distinct combinations of City and Collection first and then do the SUM
select SUM(Collection) as Collection
from
(
select distinct City, Collection
from table
) data
select sum(collection) as output
from
(
select collection, row_number() over(partition by city order by collection) as RN
from yourtable
) as inside
where RN=1
If you just want the sum of the distinct values of the collections column without the city name (I assume this is what you want from your statement "but eliminate city name") then simply run this:
SELECT SUM(DISTINCT [collection]) FROM tableName
This will return the value 10000 like you are after.
I've got the following table:
ID Name Sales
1 Kalle 1
2 Kalle -1
3 Simon 10
4 Simon 20
5 Anna 11
6 Anna 0
7 Tina 0
I want to write a SQL query that only returns the rows that
represents a salesperson with sum of sales > 0.
ID Name Sales
3 Simon 10
4 Simon 20
5 Anna 11
6 Anna 0
Is this possible?
You can easily get names of the people with the sum of sales that are greater than 0 by using the a HAVING clause:
select name
from yourtable
group by name
having sum(sales) > 0;
This query will return both Simon and Anna, then if you want to return all of the details for each of these names you can use the above in a WHERE clause to get the final result:
select id, name, sales
from yourtable
where name in (select name
from yourtable
group by name
having sum(sales) > 0);
See SQL Fiddle with Demo.
You can make it like this, I think the join will be more effective than the where name in() clause.
SELECT Sales.name, Sales.sales
FROM Sales
JOIN (SELECT name FROM Sales GROUP BY Sales.name HAVING SUM(sales) > 0) AS Sales2 ON Sales2.name = Sales.name
This will work on some databases, like oracle, mssql, db2
SELECT ID, Name, Sales
FROM
(
SELECT ID, Name, Sales, sum(sales) over (partition by name) sum1
FROM <table>
) a
WHERE sum1 > 0