SQL column of sums of multiple rows while keeping all rows? - sql

So I have a query select that outputs something like this:
(the actual results I'm working with is much more complicated but these are the important parts)
id trans
123 5.00
124 6.00
124 7.00
125 8.00
125 9.00
I want to create a result like this:
id trans total
123 5.00 5.00
124 6.00 13.00
124 7.00 13.00
125 8.00 17.00
125 9.00 17.00
Basically I want to add a column that contains a total of all the transactions for each id, while still showing all the transactions. I think the solution will have something to do with group by, nested selects and the sum function but I can't get it right.

A windowed function works well for this scenario:
select
*
,sum(trans) over(partition by id) as total
from
myTable
SqlFiddle Example

Generally speaking, you want:
SUM(value) OVER (PARTITION BY group)
If the first output is from this:
SELECT id, SUM(col) AS trans
FROM table
GROUP BY id;
Then you need this:
SELECT id, SUM(col) AS trans, SUM(SUM(col)) OVER (PARTITION BY id) AS id_total
FROM table
GROUP BY id;
If the first output is from this:
SELECT id, trans
FROM table;
Then you need this:
SELECT id, trans, SUM(trans) OVER (PARTITION BY id) AS id_total
FROM table;

Related

I have a table of calls data I want to figure out the count Unique accounts called everyday and take sum of unique accounts called by monthly basis

I have a table with 2 unique columns one has an account number and the other is the date. The sample data is given below.
Date account
9/8/2020 555
9/8/2020 666
9/8/2020 777
9/8/2020 888
9/9/2020 555
9/9/2020 999
9/10/2020 555
9/10/2020 222
9/10/2020 333
9/11/2020 666
9/11/2020 111
I would like to calculate the number of unique accounts called every day and sum it up for a month for example if account number 555 is called on 8sept, p sept and 20 Sept its is not adding up to the cumulative sum the result should look like this
date Cumulative Unique Accounts Called SO Far this month
9/8/2020 4
9/9/2020 5
9/10/2020 7
9/11/2020 8
Thank you in advance for your help.
You can do this with aggregation and window functions. First, get the first date for each account, then aggregate and accumulate:
select min_date,
count(*) as as_of_date,
sum(count(*)) over (partition by year(min_datedate), month(min_datedate)
order by min_date
) as cumulative_unique_count
from (select account, min(date) as min_date
from t
group by account, year(date), month(date)
) t
group by min_date;
You can try the below -
with cte as
(
select date,count(*) as total from
(
select date,count,row_number() over(partition by count order by date) as rn
from tablename
)A where rn=1 group by date
)
select date,sum(total) over(order by date) as cum_sum
from cte

Trying to group quantities based off an ID

We have two columns one with ID and another with QTY. And the layout goes along the lines of:
ID QTY
-------------
123 456
123 634
123 4235
234 67
234 735
234 666
What I am trying to do is add up all the numbers based off the ID so it would look like:
ID QTY
-------------
123 5325
234 1468
I currently have the following SQL query:
SELECT CLIENT_ID, ID, QTY_ON_HAND,
SUM(QTY_ON_HAND)
FROM
(select CLIENT_ID, ID, QTY_ON_HAND
FROM INVENTORY
WHERE CLIENT_ID = '(CLIENT ID HERE)')
GROUP BY QTY_ON_HAND
It would be appreciated if anyone can tell me simple way on how to do this.
I do not have a test DB at hand, but it should be this:
select
ID,
sum(QTY) as TOTAL
from
YourTableName
group by
ID;
YourTableName ... name of data table with two columns ID, QTY. Be aware of whole table name, it can be also something like dbo.yourtablename, etc.

Querying table with group by and sum

I have the following table called Orders
Order | Date | Total
------------------------------------
34564 | 03/05/2015| 15.00
77456 | 01/01/2001| 3.00
25252 | 02/02/2008| 4.00
34564 | 03/04/2015| 7.00
I am trying to select the distinct order sum the total and group by order #, the problem is that it shows two records for 34564 because they are different dates.. How can I sum if they are repeated orders and pick only the max(date) - But sill sum the total of the two instances?
I.E result
Order | Date | Total
------------------------------------
34564 | 03/05/2015| 22.00
77456 | 01/01/2001| 3.00
25252 | 02/02/2008| 4.00
Tried:
SELECT DISTINCT Order, Date, SUM(Total)
FROM Orders
GROUP BY Order, Date
Of couse the above won't work as you can see but i am not sure how to achieve what i intend.
SELECT [order], MAX(date) AS date, SUM(total) AS total
FROM Orders o
GROUP BY [order]
You can use the MAX aggregate function to choose the latest Date to appear from each Order group:
SELECT Order, MAX(Date) AS Date, SUM(Total) AS Total
FROM Orders
GROUP BY Order
Simplest query should be:
SELECT MAX(Order), MAX(Date), SUM(Total)
FROM Orders
You can use SUM and MAX together:
SELECT
[Order],
[Date] = MAX([Date]),
Total = SUM(Total)
FROM tbl
GROUP BY [Order]
A word of advice, please refrain from using reserved words like Order and Date for your columns and table names.
Just add MAX(Date) to your SELECT clause.
Try this :
SELECT DISTINCT Order, MAX(Date), SUM(Total)
FROM Orders
GROUP BY Order, Date

Sum Data in SQL by a column

I am looking to add a common value.
My table has the following data:
Customer: $ Value:
123 100.00
123 100.00
abc 100.00
abc 100.00
I want it to display as:
Customer: $ Value:
123 200.00
abc 200.00
There are a number of other columns too that contain various different dates etc but they are not relevant here.
SELECT Customer, SUM(Value)
FROM myTable
GROUP BY Customer
Use GROUP BY Clause and the aggregate function SUM().
For more info about GROUP BY refer here
SELECT Customer, SUM(Value) AS Value FROm table GROUP BY Customer
You could do this:
SELECT
Customer,
SUM(Value) AS Value
FROM
Table1
GROUP BY
Customer

Select all rows based on alternative publisher

I want list all the rows by alternative publisher with price ascending, see the example table below.
id publisher price
1 ABC 100.00
2 ABC 150.00
3 ABC 105.00
4 XYZ 135.00
5 XYZ 110.00
6 PQR 105.00
7 PQR 125.00
The expected result would be:
id publisher price
1 ABC 100.00
6 PQR 105.00
5 XYZ 110.00
3 ABC 105.00
7 PQR 125.00
4 XYZ 135.00
2 ABC 150.00
What would be the required SQL?
This should do it:
select id, publisher, price
from (
select id, publisher, price,
row_number() over (partition by publisher order by price) as rn
from publisher
) t
order by rn, publisher, price
The window functions assigns unique numbers for each publisher price. Based on that the outer order by will then first display all rows with rn = 1 which are the rows for each publisher with the lowest price. The second row for each publisher has the second lowest price and so on.
SQLFiddle example: http://sqlfiddle.com/#!4/06ece/2
SELECT id, publisher, price
FROM tbl
ORDER BY row_number() OVER (PARTITION BY publisher ORDER BY price), publisher;
One cannot use the output of window functions in the WHERE or HAVING BY clauses because window functions are applied after those. But one can use window functions in the ORDER BY clause.
SQL Fiddle.
Not sure what your table name is - I have called it publishertable. But the following will order the result by price in ascending order - which is the result you are looking for:
select id, publisher, price from publishertable order by price asc
if I've got it right. You should use ROW_NUMBER() function to range prices inside of each publisher and then order by this range and publisher.
SELECT ID,
Publisher,
Price,
Row_number() OVER (PARTITION BY Publisher ORDER BY Price) as rn
FROM T
ORDER BY RN,Publisher
SQLFiddle demo