select sum grouped records by count - sql

I have an excel sheet that contains invoices of orders
Each invoice has the following data
ID = a unique id for each ID
CUSTOMER = The name of the ID
AMT = The total value of the invoices
DATE = The date of the purchase
For the following data
CID CUSTOMER AMT DATE
1 James 100 1/1/2012
2 Mark 110 1/1/2012
3 John 110 2/1/2012
1 James 200 2/1/2012
3 John 140 2/1/2012
2 Mark 120 3/1/2012
I need to select records from excel sheet so that i have this output
CID Customer INVCOUNT TotalValue
1 James 2 300
1 John 2 250
1 Mark 2 230
This is the sql i tried
Select
i.[CID],
i.[CUSTOMER],
Count(i.[CID]) as INVCOUNT,
sum(i.AMT) as TotalValue
From
[Invoices] i
Where
i.[DATE] >= #2/1/2012# And
i.[DATE] <= #3/1/2012#
Group By
i.[CID], i.[CUSTOMER]
Having
Count(i.[CID]) > 1
pls this is an excel query, not mysql. The tag i used earlier was a mistake.
WHat am i doing wrong?

To get the output you specified:
Select
1 as CID,
i.[CUSTOMER],
Count(i.[CID]) as INVCOUNT,
sum(i.AMT) as TotalValue
From
[Invoices] i
Where
i.[DATE] >= #1/1/2012# And
i.[DATE] <= #3/1/2012#
Group By
i.[CID], i.[CUSTOMER]
Having
Count(i.[CID]) > 1
To get the output I think you actually want:
Select
i.[CID],
i.[CUSTOMER],
Count(i.[CID]) as INVCOUNT,
sum(i.AMT) as TotalValue
From
[Invoices] i
Where
i.[DATE] >= #1/1/2012# And
i.[DATE] <= #3/1/2012#
Group By
i.[CID], i.[CUSTOMER]
Having
Count(i.[CID]) > 1
This should generate
CID Customer INVCOUNT TotalValue
1 James 2 300
2 John 2 250
3 Mark 2 230

Related

Grouping and Summarize SQL

My table looks like the following:
income
date
productid
invoiceid
customerid
300
2015-01-01
A
1234551
1
300
2016-01-02
A
1234552
1
300
2016-01-03
B
1234553
2
300
2016-01-03
A
1234553
2
300
2016-01-04
C
1234554
3
300
2016-01-04
C
1234554
3
300
2016-01-08
A
1234556
3
300
2016-01-08
B
1234556
3
300
2016-01-11
C
1234557
3
I need to know : Number of invoices per customer, how many customers in total (for example one invoice = several customers, two invoices = two customers, three invoices = three customers, and so..).
What is the syntax for this query?
In my sample data above, customer 1 has two invoices, customer 2 one invoice and customer 3 three invoices. So there is one customer each with a count of 1, 2, and 3 invoices in my example.
Expected result:
invoice_count
customers_with_this_invoice_count
1
1
2
1
3
1
I tried this syntax and I'm still stuck:
select * from
(
select CustomerID,count(distinct InvoiceID) as 'Total Invoices'
from exam
GROUP BY CustomerID
) a
Select Count(customerID),CustomerID From a
Group By customerID
Having Count(customerID) > 1

Aggregate before and after a date column

I have two tables: db.transactions and db.salesman, which I would like to combine in order to create an output that has aggregated sales before each salesman's hire date and after each salesman's hire date.
select * from db.transactions
index sales_rep sales trx_date
1 Tom 200 9/18/2020
2 Jerry 435 6/21/2020
3 Patrick 1400 4/30/2020
4 Tom 560 5/24/2020
5 Francis 240 1/2/2021
select * from db.salesman
index sales_rep hire_date
1 Tom 8/19/2020
2 Jerry 1/28/2020
3 Patrick 4/6/2020
4 Francis 9/4/2020
I would like to aggregate sales from db.transactions before and after each sales rep's hire date.
Expected output:
index sales_rep hire_date agg_sales_before_hire_date agg_sales_after_hire_date
1 Tom 8/19/2020 1200 5000
2 Jerry 1/28/2020 500 900
3 Patrick 4/6/2020 5000 300
4 Francis 9/4/2020 2900 1500
For a single sales rep, to calculate the agg_sales_before_hire_date is likely:
select tx.sales_rep, tx.sum(sales)
from db.transactions tx
inner join db.salesman sm on sm.sales_rep = tx.sales_rep
where hire_date < '8/19/2020' and sales_rep = 'Tom'
group by tx.sales_rep
PostGRESQL. I am also open to the idea of doing it into Tableau or Python.
Using CROSS JOIN LATERAL
select
sa.sales_rep, sa.hire_date,
l.agg_sales_before_hire_date,
l.agg_sales_after_hire_date
from salesman sa
cross join lateral
(
select
sum(tx.sales) filter (where tx.trx_date < sa.hire_date) agg_sales_before_hire_date,
sum(tx.sales) filter (where tx.trx_date >= sa.hire_date) agg_sales_after_hire_date
from transactions tx
where tx.sales_rep = sa.sales_rep
) l;
Use conditional aggregation:
select tx.sales_rep,
sum(case when tx.txn_date < sm.hire_date then sales else 0 end) as before_sales,
sum(case when tx.txn_date >= sm.hire_date then sales else 0 end) as after_sales
from db.transactions tx inner join
db.salesman sm
on sm.sales_rep = tx.sales_rep
group by tx.sales_rep;
EDIT:
In Postgres, you would use filter for the logic:
select tx.sales_rep,
sum(sales) filter (where tx.txn_date < sm.hire_date) as before_sales,
sum(sales) filter (where tx.txn_date >= sm.hire_date then sales) as after_sales

SQL Group by range and total column

With the following function and stored procedure i get a resultset with 2 columns.
What i need additional is a third column total for all open invoices inner score range
It would great for any idea.
ALTER FUNCTION dbo.OpenOrders
(
#MandantId int
)
RETURNS TABLE
AS
RETURN
SELECT SUM(Invoices.Amount - ISNULL(Payment.Amount, 0)) AS Op
FROM Invoices INNER JOIN
Orders ON Invoices.OrderId = Orders.OrderId LEFT OUTER JOIN
Payment ON Invoices.InvoiceId = Payment.InvoiceId
WHERE (Orders.MandantId = #MandantId)
GROUP BY Invoice.InvoiceId, Invoices.Amount
HAVING (SUM(Invoices.Amount - ISNULL(Payment.Amount, 0)) <> 0)
ALTER PROCEDURE dbo.GetOpRanges
#MandantId int
AS
BEGIN
SELECT * INTO #tmp_ranges
FROM
//wrong in first post -> OPDebitorByMandant(#MandantId)
OpenOrders(#MandantId)
SELECT op AS [score range], COUNT(*) AS [number of occurences]
FROM
(SELECT CASE
WHEN op BETWEEN 0 AND 50 THEN ' 0- 50'
WHEN op BETWEEN 50 AND 100 THEN ' 50-100'
WHEN op BETWEEN 100 AND 500 THEN '100-500'
ELSE '500 and >' END AS op
FROM [#tmp_ranges]) AS t
GROUP BY op
RETURN
Result:
score range number of occurences range
------------------+-------------
0- 50 23
50-100 4
100-500 4
500 and > 21
What i need additional is a third column total for all open invoices inner score range.
Target result:
score range number of occurences Total
-----------+--------------------+------
0- 50 23 1.150
50-100 4 400
100-500 4 2.000
500 and > 21 22.000
Tables:
Invoices
InvoiceId CustomerId OrderId DateOfInvoice Amount
----------+----------+-------+-------------+------
1 1 20 20160301 1000.00
2 2 22 20160501 2000.00
3 1 102 20160601 3000.00
...
Orders
OrderId MandantId CustomerId DateOfOrder Amount
-------+---------+----------+-----------+-----------
20 1 1 20160101 1000.00
22 1 2 20160101 2000.00
102 1 1 20160101 3000.00
...
Payment
PaymentId MandantId CustomerId InvoiceId OrderId DateOfPayment Amount
---------+---------+----------+---------+-------+-------------+-------------
1 1 1 1 20 20160310 1000.00
2 1 2 2 22 20160505 2000.00
3 1 1 3 102 20160610 3000.00
...
hope it's helpfull and thanks again in advance for any solution

Get count of two different values but not same values

I have the following table format.
**ID Name Start Date End Date**
1 ABC 1/1/2015 12/31/2015
1 XYZ 4/1/2015 8/31//2015
1 DEF 1/1/2012 12/31/2012
2 ABC 1/23/2011 1/23/2012
2 ABC 1/31/2012 1/31/2013
3 DEF 2/12/2015 5/30/2015
3 XYZ 4/1/2015 6/01/2015
4 DEF 3/1/2015 12/31/2015
4 DEF 4/1/2015 6/30/2015
I need the count of ID's having Different Name which lies in date range of May 2015
Expected Results
ID COUNT
1 2
3 2
P.S - ID 4 also lies in the date range of MAY 2015, but the Name is same i.e DEF. So I need only ID's 1 and 3 but not 4.
Thank You in advance and appreciated for your efforts.
I imagine your sample data doesn't match your desired results, but I think this is what you're looking for using conditional aggregation:
select id, count(*)
from yourtable
group by id
having sum(case when '5/1/2015' between startdate and enddate then 1 else 0 end) > 1
and count(distinct name) = count(name)
SQL Fiddle Demo
The sum aggregation in the having clause is making sure there are multiple records in between that date. The count clause in the having clause is making sure there aren't any duplicates.
declare
#startdate datetime = '20150501',
#enddate datetime = '20150531'
select t.id, count(distinct t.name)
from mytable t
where t.startdate <= #enddate and t.enddate >= #startdate
group by t.id
having count(distinct t.name) > 1

How do you get average of sums in SQL (multi-level aggregation)?

I have a simplified table xx as follows:
rdate date
rtime time
rid integer
rsub integer
rval integer
primary key on (rdate,rtime,rid,rsub)
and I want to get the average (across all times) of the sums (across all ids) of the values.
By way of a sample table, I have (with consecutive identical values blanked out for readability):
rdate rtime rid rsub rval
-------------------------------------
2010-01-01 00.00.00 1 1 10
2 20
2 1 30
2 40
01.00.00 1 1 50
2 60
2 1 70
2 80
02.00.00 1 1 90
2 100
2010-01-02 00.00.00 1 1 999
I can get the sums I want with:
select rdate,rtime,rid, sum(rval) as rsum
from xx
where rdate = '2010-01-01'
group by rdate,rtime,rid
which gives me:
rdate rtime rid rsum
-------------------------------
2010-01-01 00.00.00 1 30 (10+20)
2 70 (30+40)
01.00.00 1 110 (50+60)
2 150 (70+80)
02.00.00 1 190 (90+100)
as expected.
Now what I want is the query that will also average those values across the time dimension, giving me:
rdate rtime ravgsum
----------------------------
2010-01-01 00.00.00 50 ((30+70)/2)
01.00.00 130 ((110+150)/2)
02.00.00 190 ((190)/1)
I'm using DB2 for z/OS but I'd prefer standard SQL if possible.
select rdate,rtime,avg(rsum) as ravgsum from (
select rdate,rtime,rid, sum(rval) as rsum
from xx
where rdate = '2010-01-01'
group by rdate,rtime,rid
) as subq
group by rdate,rtime
How about
select rdate,rtime, sum(rsum) / count(rsum) as sumavg
from
(select rdate, rtime, rid, sum(rval) as rsum
from xx
where rdate = '2010-01-01'
group by rdate,rtime,rid) as subq
group by rdate,rtime