Average of maximum - sql

I am new to sql and tried to find similar topic, but with no success.
If someone could help, I will be very grateful:-)
Here is a tiny part of big code, which is based on Group by 1 column 'CustomerID' (and I do not need more!).
I need to calculate Maximum of [Field] per each year and afterwards arrive at average of those MAX values.
SELECT
t0.[CustomerID]
,MAX(t0.[CustomerName]) as 'CustomerName'
,SUM(CASE WHEN t0.[Year] = 2021 then t0.[SalesValue_USD] END) as 'Sales_USD_2021'
,SUM(t0.[SalesValue_USD]) as 'Sales_USD_total'
,AVG(MAX(t0.[Field]) OVER (PARTITION BY [Year])) as 'Field_avr'
From market t0
Where [Year] in (2019,2020,2021)
Group by t0.[CustomerID]

You can do it in two steps
SELECT [CustomerID],
MAX('CustomerName') as 'CustomerName',
SUM('Sales_USD_2021') as 'Sales_USD_2021',
SUM('Sales_USD_total') as 'Sales_USD_total',
AVG(Field_max) as 'Field_avr'
FROM (
SELECT
t0.[CustomerID], t0.[Year]
,MAX(t0.[CustomerName]) as 'CustomerName'
,SUM(CASE WHEN t0.[Year] = 2021 then t0.[SalesValue_USD] END) as 'Sales_USD_2021'
,SUM(t0.[SalesValue_USD]) as 'Sales_USD_total'
,MAX(t0.[Field]) as 'Field_max'
From market t0
Where [Year] in (2019,2020,2021)
Group by t0.[CustomerID], t0.[Year] ) t
Group by [CustomerID]

Related

SQL help to display no. of openings for all branches for each month of a year

Hi I need to generate a SSRS report to show how many centers got opened for each month in a calendar year under each branch. report will have 13 columns, first column being all the branches in each row and remaining 12 columns will have months of an year as header. I'm trying to get a result of each branch having no. of openings per month, so I can feed SSRS to display in tabular format. If a branch doesnt have any openings for any month, I need to display 0.
Branch table
=============
Branchid
Branchname
CenterOpen table
================
CenterOpenID
BranchID
CenterOpenDate
below is the SQL I had written
WITH months(MonthNumber) AS (
SELECT
1
UNION ALL
SELECT
MonthNumber + 1
FROM
months
WHERE
MonthNumber < 12
),
cteBranch(BranchID, BranchName, TargetOpenDate, Month, Count) as (
SELECT
B.BranchID,
B.BranchName,
CS.TargetOpenDate,
MONTH(CS.TargetOpenDate) as Month,
count(Month(CS.TargetOpenDate)) as Count
FROM
Branch B
left join goal.CenterOpenSchedule CS ON CS.BranchID = B.BranchID
GROUP BY
B.BranchID,
B.BranchName,
CS.TargetOpenDate,
MONTH(CS.TargetOpenDate)
)
select
*
from
months
cross join cteBranch
order by
BranchID asc,
MonthNumber asc
If I use cross join, months are repeating for each branch, how to resolve this? your help is highly appreciated.
Not sure which database you are on.
There are different ways to extract month/year from date.
Based on your example SQL, I'm going to use MONTH()
select branchName,
count(case when month(centerOpenDate) = 1 then branchId end) Jan_Count
...
...
count(case when month(centerOpenDate) = 12 then branchId end) Dec_Count
from Branch b
join CenterOpen co
on (b.BranchId = co.BranchId)
where year(centerOpenDate) = <your year filter>
group by branchName
This will take care of your below requirements:
" first column being all the branches in each row and remaining 12 columns will have months of an year as header."
and also -
"If a branch doesnt have any openings for any month, I need to display 0."
Your question is not explicit, but you seem to want a single year -- so you need to filter on the year.
The rest is basically conditional aggregation:
select b.branchName,
sum(case when month(co.centerOpenDate) = 1 then 1 else 0 end) as jan,
sum(case when month(co.centerOpenDate) = 2 then 1 else 0 end) as feb,
. . .
sum(case when month(co.centerOpenDate) = 12 then 1 else 0 end) as dec
from Branch b join
CenterOpen co
on b.BranchId = co.BranchId
where year(co.centerOpenDate) = #year
group by b.branchName

Running total for grouped data SQL

I know there are a lot of questions about this and I have tried a lot of the answers but none have worked for me yet (see below for the most promising).
I want to get the running total in the column amount grouped by project and date divided into two groups (invoiced or costs). The group is dependent on the Cost Center as you can see in the query.
This topic has seemed to be the closest to what I'm trying to achieve but I cant get it to work.
SQL - running total when data already grouped
With the code below I get the error message "Column 'M3FDBPRD.MVXJDTA.FGLEDG.EGAIT2' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause." But I don't want to group by cost center in the result, I only want to use it to group the amount.
The reuslt im trying to achevie is this (if using the data above):
My current code is this:
SELECT
Project As Projektnr,
LastDayOfMonth,
-SUM(CASE WHEN CostCenter= '9000' THEN Amount
WHEN CostCenter = '2100' THEN Amount
WHEN CostCenter = '2200' THEN Amount
WHEN CostCenter = '2300' THEN Amount END) OVER (ORDER BY LastDayOfMonth) AS Invoiced,
-SUM(CASE WHEN CostCenter > '9000' and CostCenter < '9999' THEN Amount END) OVER (ORDER BY LastDayOfMonth) AS RunningTotalCosts
FROM [M3FDBPRD].[MVXJDTA].[FGLEDG]
WHERE Project = '150001'
GROUP BY LastDayOfMonth, Project
ORDER BY LastDayOfMonth
I have SQL Server version 13
Thank you
You are mixing window functions and aggregation. The logic looks like:
SUM(SUM(CASE WHEN CostCenter IN ('9000' '2100', '2200', '2300')
THEN Amount
END)
) OVER (ORDER BY LastDayOfMonth) AS Invoiced,
SUM(SUM(CASE WHEN CostCenter > '9000' and CostCenter < '9999'
THEN Amount
END)
) OVER (ORDER BY LastDayOfMonth) AS RunningTotalCosts
That is, the SUM()s need to nested. The inner SUM() is the aggregation. The outer SUM() is the window function.

How to get the asked columns for each customers

I have this table called table a
I need to get the CustomerID, sum(Income) of 2015, sum(Income) of 2016, did he ever bought productId A (boolean), is the total sum(income)> 1000 (boolean), number of total InvoiceID
all that in one query and the results should be with 1 row per customer.
please help I don't even know how to start!
This is basically conditional aggregation:
select customerid,
sum(case when extract(year from date) = 2015 then sales end) as sales_2015,
sum(case when extract(year from date) = 2016 then sales end) as sales_2016,
max( product = 'A' ) as ever_bought_a,
sum(income) > 1000 as sum_exceeds_1000,
count(*) as num_invoices
from t
group by customerid;
You haven't specified a database, so this is really psuedocode. You'll need to adapt it for your particular database.

Calculate % of total - redshift / sql

I'm trying to calculate the percentage of one column over a secondary total column.
I wrote:
create temporary table screenings_count_2018 as
select guid,
datepart(y, screening_screen_date) as year,
sum(case when screening_package = 4 then 1 end) as count_package_4,
sum(case when screening_package = 3 then 1 end) as count_package_3,
sum(case when screening_package = 2 then 1 end) as count_package_2,
sum(case when screening_package = 1 then 1 end) as count_package_1,
sum(case when screening_package in (1, 2, 3, 4) then 1 end) as count_total_packages
from prod.leasing_fact
where year = 2018
group by guid, year;
That table establishes the initial count and total count columns. All columns look correct.
Then, I'm using ratio_to_report to calculate the percentage (referencing this tutorial):
create temporary table screenings_percentage as
select
guid,
year,
ratio_to_report(count_package_1) over (partition by count_total_packages) as percentage_package_1
from screenings_count_2018
group by guid, year,count_package_1,count_total_packages
order by percentage_package_1 desc;
I also tried:
select
guid,
year,
sum(count_package_1/count_total_packages) as percentage_package_1
-- ratio_to_report(count_package_1) over (partition by count_total_packages) as percentage_package_1
from screenings_count_2018
group by guid, year,count_package_1,count_total_packages
order by percentage_package_1 desc;
Unfortunately, percentage_package_1 just returns all null values (this is not correct - I'm expecting percentages). Neither are working.
What am I doing wrong?
Thanks!
Since you are already laid out the columns with components and a total, in creating screenings_count_2018, do you actually need to use ratio_to_report?
select
, guid
, year
, count_package_1/count_total_packages as percentage_package_1
, count_package_2/count_total_packages as percentage_package_2
, count_package_3/count_total_packages as percentage_package_3
, count_package_4/count_total_packages as percentage_package_4
from screenings_count_2018
That should work. NB are you guaranteed to never have count_total_packages be zero? If it can be zero you'll need to handle it. One way is with a case statement.
If you wish for the per-package percentages to appear in a single column, then you can use ratio_to_report -- it is a "window" analytic function and it will be something like this against the original table.
with count_table as (
select guid
, datepart(y, screening_screen_date) as year
, screening_package
, count(1) as count
from prod.leasing_fact
where year = 2018
group by guid
, datepart(y, screening_screen_date)
, screening_package
)
select guid
, year
, screening_package
, ratio_to_report(count) over(partition by guid, year, screening_package) as perc_of_total
from count_table
you will need round(100.0*count_package_1/count_total_packages,1) and so on as you already calculated the subtotal and total

How can I add cumulative sum column?

I use SqlExpress
Following is the query using which I get the attached result.
SELECT ReceiptId, Date, Amount, Fine, [Transaction]
FROM (
SELECT ReceiptId, Date, Amount, 'DR' AS [Transaction]
FROM ReceiptCRDR
WHERE (Amount > 0)
UNION ALL
SELECT ReceiptId, Date, Amount, 'CR' AS [Transaction]
FROM ReceiptCR
WHERE (Amount > 0)
UNION ALL
SELECT strInvoiceNo AS ReceiptId, CONVERT(datetime, dtInvoiceDt, 103) AS Date, floatTotal AS Amount, 'DR' AS [Transaction]
FROM tblSellDetails
) AS t
ORDER BY Date
Result
want a new column which would show balance amount.
For example. 1 Row should show -2500, 2nd should -3900, 3rd should -700 and so on.
basically, it requires previous row' Account column's data and carry out calculation based on transaction type.
Sample Result
Well, that looks like SQL-Server , if you are using 2012+ , then use SUM() OVER() :
SELECT t.*,
SUM(CASE WHEN t.transactionType = 'DR'
THEN t.amount*-1
ELSE t.amount END)
OVER(PARTITION BY t.date ORDER BY t.receiptId,t.TransactionType DESC) as Cumulative_Col
FROM (YourQuery Here) t
This will SUM the value when its CR and the value*-1 when its DR
Right now I grouped by date, meaning each day will recalculate this column, if you want it for all time, replace the OVER() with this:
OVER(ORDER BY t.date,t.receiptId,t.TransactionType DESC) as Cumulative_Col
Also, I didn't understand why in the same date, for the same ReceiptId DR is calculated before CR , I've add it to the order by but if thats not what you want then explain the logic better.