How to Calculate amount value of employees repeated many times - sql

I have SQL file that contains more than 500 employees. So many employee are repeated many times like
Name amount Id
--------------------
Raj 500 1
Kumar 300 4
Karthi 400 3
Raj 300 1
Raj 800 1
Kumar 300 4
In the above sample, Raj is repeated many times. My question: I want to calculate all Raj name Amount values. How can I get the total amount of Raj employee? Please give some idea please help me

simply use sum and group by
select sum(amount), Name from table Group by name

Related

merge two rows into one sql

this is my table schema, total_hours column is the result of a sum function.
Id name client total_hours
1 John company 1 100
1 John company 2 200
2 Jack company 3 350
2 Jack company 2 150
I want to merge the rows with similar ID into one row, looking like this.
Id name client_a total_hours_a client_b total_hours_b
1 John company 1 100 company 2 200
2 Jack company 3 350 company 2 150
I tried to use pivot but this function does not seem to exist in Dbeaver. Here is my query
SELECT
client
,name
,sum(hours) AS total_hours
FROM pojects
GROUP BY client, name;
Thanks in advance if anyone could be of any help.

select only one row in sum and group

i have a table TB_Orders , i save orders in thatl ,ike below :
userid
factorid
productname
economy
vip
count
1
A-123
name1
13000
18000
3
1
A-123
name2
9000
13000
4
2
A-124
name2
2000
5000
2
For example, user 1: has registered two orders with invoice number A-123.
And user 2 has only registered one order
I want to report as follows:
userid
factorid
total economy
total vip
total count
1
A-123
21000
31000
7
2
A-124
2000
5000
2
thanks for your help
This is an aggregation query:
select userid, factorid, sum(economy), sum(vip), sum(count)
from tb_orders
group by userid, factorid;
Aggregation is a very basic part of SQL functionality. If you don't recognize it, I would suggest that you brush up on your SQL skills with a tutorial, book, or something like that.

MAX value from the sum of columns in Hive

I'm new to Hive, and I'm stuck on a fairly simple problem. My data looks like:
Name---Day---Doctor Bill--- Room Bill
Rakesh 1 2500 1500
Raja 1 5000 2300
Raju 1 4500 2000
Rakesh 2 3750 2250
Rakesh 3 3550 1750
Raja 2 4500 4000
Raju 2 3450 4725
To find out who paid the highest of total doctor bill?
Query:
hive> insert overwrite table maxdrbill select t.name,sum(t.drbill) as totaldrbill from patient t join (select name from patient group by name order by sum(drbill) desc LIMIT 1) t1 on t.name=t1.name GROUP by t.name;
When I run the below query in hive I get the following error:
FAILED: Error in semantic analysis: Line 1:149 Invalid table alias or
column reference drbill
Query
select name,SUM(doctorbill) as s from bills GROUP BY name ORDER BY s DESC LIMIT 1;
Output
Rakesh 9800
Hope it helps!
Performance wise I believe this will be much better as the data does not need to be sorted out to get the max.
Just get the Max value after suming:
SELECT t1.Name, MAX(TotalDrBill) FROM
(SELECT t.Name, SUM(t.drbill) as TotalDrBill FROM Patient t GROUP BY t.Name) t1

SQL - count without group by? I need to use two ids for a join

I thought I could count a column and add it as a column as I can with a sum but I get an error about having to group by / having. An example of what I want...
Initial table...
Global ID Local ID Name Role
100 1 Andy Manager
100 2 Andy Manager
100 1 John Co-Manager
200 1 Andy Co-Manager
200 2 John Manager
200 2 Mike Manager
I then want to add a column that counts the number of Manager in each group / local pairing...
Global ID Local ID Name Role Manager Count
100 1 Andy Manager 1
100 2 Andy Manager 1
100 1 John Co-Manager 0
200 1 Andy Co-Manager 0
200 2 John Manager 2
200 2 Mike Manager 2
I tried just joining the two tables on the global / local ID and then adding a column for count of the Role. The problem is that I get an error about grouping / having by but I don't want to group anything. I just want to add the column and still have the same number of rows. Any way around this?
FYI - for the last two rows, the last column has 2 because John and Mike are on the same group / local ID
It looks like your definition of a group is a unique combination of [global_id, local_id]. If that is the case you do want to group by those two values, and do a count, where the role is Manager. But because you want other columns from the original table, you must do that count within an inline view, and then join back to the original table, like so:
select t.*, v.mgr_in_grp
from tbl t
left join (select global_id, local_id, count(*) as mgr_in_grp
from tbl
where role = 'Manager'
group by global_id, local_id) v
on t.global_id = v.global_id
and t.local_id = v.local_id
Fiddle: http://sqlfiddle.com/#!2/fb3ace/2/0
Notice that there is a difference on row 3, as compared to your expected output.
John, Co-manager at global_id and local_id 100 and 1, respectively, belongs to a pair that has a manager in that combination (Andy, your first row). So the count appears should be 1.

SQL Sum count based on an identifier

Assume I have a table with the following data:
Name TransID Cost
---------------------------------------
Susan 1 10
Johnny 2 10
Johnny 3 9
Dave 4 10
I want to find a way to sum the Costs per name (assume the Names are unique) so that I get a table like this:
Name Cost
---------------------------------------
Susan 10
Johnny 19
Dave 10
Any help is appreciated.
This is relatively straightforward: you need to use a GROUP BY clause in your query:
SELECT Name,SUM(Cost)
FROM MyTable
GROUP BY Name