SQL Query Calculate Total - sql

So let's say I have data in my query like this:
Name: | Cost:
Oliver 20
Oliver 3
Oliver 2
Sarah 100
Sarah 7
How would I go about merging the data for each person into one row and having a total cost?

you can use group by and sum()
select name, sum(cost)
from your_table
group by name

Related

Active Record Sort by 2 conditions

How can I sort by number first and further sort same number names by alphabet?
Example:
Score | Name
-----------
12 John
11 Paul
10 Dave
9 Adam
9 Ben
9 David
Just use the SQL syntax for ordering by multiple columns:
order by Score, Name
Select * from Table Order by Score , Name

BigQuery Percentile Partitioned by Value in Column

I have a table with many entries for many users. I would like to compute the 20th percentile for each user into a separate table.
I have:
User
Value
John
1
John
14
John
34
John
11
Mary
3
Mary
5
...
...
And I want to have a table with the 20th percentile for each user like:
Name
Percentile
John
xx
Mary
yy
Thank you in advance!
try below
select distinct user,
percentile_cont(value, 0.2) over(partition by user) percentile
from your_table

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

How to produce detail, not summary, report sorted by count(*)?

Oracle 11g:
I want results to list by highest count, then ch_id. When I use group by to get the count then I loose the granularity of the detail. Is there an analytic function I could use?
SALES
ch_id desc customer
=========================
ANAR Anari BOB
SWIS Swiss JOE
SWIS Swiss AMY
BRUN Brunost SAM
BRUN Brunost ANN
BRUN Brunost ROB
Desired Results
count ch_id customer
===========================================
3 BRUN ANN
3 BRUN ROB
3 BRUN SAM
2 SWIS AMY
2 SWIS JOE
1 ANAR BOB
Use the analytic count(*):
select * from
(
select count(*) over (partition by ch_id) cnt,
ch_id, customer
from sales
)
order by cnt desc
select total, ch_id, customer
from sales s
inner join (select count(*) total, ch_id from sales group by ch_id) b
on b.ch_id = s.chi_id
order by total, ch_id
ok - the other post that happened at the same time, using partition, is the better solution for Oracle. But this one works regardless of DB.

Ranking Students by Grade in SQL

I have a table like this:
Date StudentName Score
01.01.09 Alex 100
01.01.09 Tom 90
01.01.09 Sam 70
01.02.09 Alex 100
01.02.09 Tom 50
01.02.09 Sam 100
I need to rank the students in the result table by score within different dates, like this:
Date Student Rank
01.01.09 Alex 1
01.01.09 Tom 2
01.01.09 Sam 3
01.02.09 Alex 1
01.02.09 Sam 1
01.02.09 Tom 2
How can I do this in SQL?
You want to use the rank function in T-SQL:
select
date,
student,
rank() over (partition by date order by score desc) as rank
from
grades
order by
date, rank, student
The magic is in the over clause. See, it splits up those rankings by date, and then orders those subsets by score. Brilliant, eh?
You should use ORDER BY:
SELECT * FROM Students ORDER BY Date,Rank
That will order the data by date, then rank. You can add as many fields as you want, as long as they are comparable (you can't compare BLOBs or long text fields).
Hope that helps.
Do you know about using an ORDER BY clause?
You need to write a function that will computer the rank for a given student and date. Then you can "ORDER BY Date, Rank()"