I want to be able to find out the monthly average of a count
My code at the moment is
SELECT
company,
COUNT(company) AS 'count'
FROM Information
GROUP BY company
I basically need it to be
SELECT company,
count(company) as 'count'
avg(count(company)) per month as 'average'
FROM Information
group by company
I want the result to look something like this
company count monthly average
a 5 6
b 13 14
c 2 2
d 45 45
e 23 21
f 6 5
A very simple approach would be to count per company and month first and then aggregate this data to get total and avarage per company.
select
company,
sum(cnt) as records,
avg(cnt) as records_per_month
from
(
select company, year(start_date), month(start_date), count(*) as cnt
from information
group by company, year(start_date), month(start_date)
) agg
group by company;
But read my comment to your question.
SELECT YEAR(yourDate) * 100 + MONTH(yourDate) YYMM,
company,
count(company) as 'count'
avg(count(company)) per month as 'average'
FROM Information
group by company
,YEAR(yourDate) * 100 + MONTH(yourDate)
Related
I want to know who has the most friends from the app I own(transactions), which means it can be either he got paid, or paid himself to many other users.
I can't make the query to show me only those who have the max friends number (it can be 1 or many, and it can be changed so I can't use limit).
;with relationships as
(
select
paid as 'auser',
Member_No as 'afriend'
from Payments$
union all
select
member_no as 'auser',
paid as 'afriend'
from Payments$
),
DistinctRelationships AS (
SELECT DISTINCT *
FROM relationships
)
select
afriend,
count(*) cnt
from DistinctRelationShips
GROUP BY
afriend
order by
count(*) desc
I just can't figure it out, I've tried count, max(count), where = max, nothing worked.
It's a two columns table - "Member_No" and "Paid" - member pays the money, and the paid is the one who got the money.
Member_No
Paid
14
18
17
1
12
20
12
11
20
8
6
3
2
4
9
20
8
10
5
20
14
16
5
2
12
1
14
10
It's from Excel, but I loaded it into sql-server.
It's just a sample, there are 1000 more rows
It seems like you are massively over-complicating this. There is no need for self-joining.
Just unpivot each row so you have both sides of the relationship, then group it up by one side and count distinct of the other side
SELECT
-- for just the first then SELECT TOP (1)
-- for all that tie for the top place use SELECT TOP (1) WITH TIES
v.Id,
Relationships = COUNT(DISTINCT v.Other),
TotalTransactions = COUNT(*)
FROM Payments$ p
CROSS APPLY (VALUES
(p.Member_No, p.Paid),
(p.Paid, p.Member_No)
) v(Id, Other)
GROUP BY
v.Id
ORDER BY
COUNT(DISTINCT v.Other) DESC;
db<>fiddle
Company_Name Amount Cumulative Total
---------------------------------------------
Company 6 100 100
Company 6 200 300
Company 6 150 450
Company 7 700 700
Company 7 1100 1800
Company 7 500 2300
How can I do cumulative sum group by company as shown in this example?
First, you need a column that specifies the ordering, because SQL tables represent unordered sets. Let me assume you have such a column.
Then the function is sum() as a window function:
select t.*,
sum(amount) over (partition by company order by <ordering col>)
from t;
Note: This does not return 0 for the "first" row for each company, so it really is a cumulative sum. For your logic, you need an additional conditional:
select t.*,
(case when row_number() over (partition by company order by <ordering col>) = 1
then 0
else sum(amount) over (partition by company order by <ordering col>)
end)
from t;
I need to know the total number of Jobs completed by each Employee as well as the total Rate if I only have the following information.
One job can be completed by 2 different Employees.
For example, Employee A has completed 2 jobs (JobID and JobID 2). So the total segments of Employee A is 30 segments but per jobID is different segment rate.
But I just need the total rate of all segments. SO Employee A has complete 2 jobs and a total of 90USD
(10 x 5 USD) + (20 x 2 USD) = 90 USD
This is what I have so far:
select
EmployeeID, count(distinct JobID)
from
Table_name
group by
EmployeeID
But I do not know how to compute the total rate per employee.
Image of the table:
This will help you:
select EmployeeID, COUNT(distinct JobID) as [Total Jobs], SUM([Segements per Job] * [Rate per Segement (USD)]) as [Total Rate (USD)]
from Table_name
Group by EmployeeID
SUM([Segements per Job] * [Rate per Segement (USD)]) will multiply the segements and rate and then sum them for each employee as the query is grouped based on EmployeeID.
I am using this query to get rolling 5 week sum
select reportdate,
department,
count(orders) as count,
sum(count(orders)) over (
partition by department order by reportdate rows between 4 preceding and current row
) as 5 weeksum
from orders
sum(count(orders) works fine when there is value for every department every reportdate.
however if there are no orders for certain reportdates then the sum(count(orders)) does not work.
For eg.
reportdate 1-Jan Dept A count(Orders) is 10
7-Jan dept A 'NO ORDERS' which means blank column
14-Jan dept A count(Orders) is 12
21-Jan dept A count(Orders) is 14
28-jan dept A count(Orders) is 25
31-jan dept A count(Orders) is 5
result for sum(count(orders))
1-Jan 10
7-Jan BLANK
while I expect 7-Jan to show 10
Any suggestions as how that can be achieved?
This is the input table:
Customer_ID Date Amount
1 4/11/2014 20
1 4/13/2014 10
1 4/14/2014 30
1 4/18/2014 25
2 5/15/2014 15
2 6/21/2014 25
2 6/22/2014 35
2 6/23/2014 10
There is information pertaining to multiple customers and I want to get a rolling sum across a 3 day window for each customer.
The solution should be as below:
Customer_ID Date Amount Rolling_3_Day_Sum
1 4/11/2014 20 20
1 4/13/2014 10 30
1 4/14/2014 30 40
1 4/18/2014 25 25
2 5/15/2014 15 15
2 6/21/2014 25 25
2 6/22/2014 35 60
2 6/23/2014 10 70
The biggest issue is that I don't have transactions for each day because of which the partition by row number doesn't work.
The closest example I found on SO was:
SQL Query for 7 Day Rolling Average in SQL Server
but even in that case there were transactions made everyday which accomodated the rownumber() based solutions
The rownumber query is as follows:
select customer_id, Date, Amount,
Rolling_3_day_sum = CASE WHEN ROW_NUMBER() OVER (partition by customer_id ORDER BY Date) > 2
THEN SUM(Amount) OVER (partition by customer_id ORDER BY Date ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
END
from #tmp_taml9
order by customer_id
I was wondering if there is way to replace "BETWEEN 2 PRECEDING AND CURRENT ROW" by "BETWEEN [DATE - 2] and [DATE]"
One option would be to use a calendar table (or something similar) to get the complete range of dates and left join your table with that and use the row_number based solution.
Another option that might work (not sure about performance) would be to use an apply query like this:
select customer_id, Date, Amount, coalesce(Rolling_3_day_sum, Amount) Rolling_3_day_sum
from #tmp_taml9 t1
cross apply (
select sum(amount) Rolling_3_day_sum
from #tmp_taml9
where Customer_ID = t1.Customer_ID
and datediff(day, date, t1.date) <= 3
and t1.Date >= date
) o
order by customer_id;
I suspect performance might not be great though.