ASP Classic SQL query to get cumulative ammounts per day [duplicate] - sql

This question already has answers here:
How to get cumulative sum
(16 answers)
Closed 6 years ago.
I am struggling with a SQL query I have a collection process and I want to have a query that show me every day how much we have collected so far during the current month in a cumulative way. I have all the information in one table named Procesado the date field is Process Date and the amount is in lcamnt Field.
So far I have this:
Query:
SELECT
Procesado.ProcessDate, SUM(Procesado.lcamnt) AS Monto
FROM
Procesado
WHERE
Procesado.ProcessDate >= Procesado.ProcessDate
GROUP BY
Procesado.ProcessDate
Table value
Table Name: Procesado
ProcessDate lcamnt
05/26/2016 $1000
05/26/2016 $500
05/27/2016 $2000
05/27/2016 $1000
05/28/2016 $5000
Desired output
05/26/2016 $1500
05/27/2016 $4500
05/28/2016 $9500

select p.ProcessDate,
(select sum(lcamnt)
from procesado p2
where
p2.ProcessDate <= p.ProcessDate
) as Monto
from procesado p
group by p.processDate

Related

SQL - Monthly cumulative count of new customer based on a created date field

Thanks in advance.
I have Customer records that look like this:
Customer_Number
Create_Date
34343
01/22/2001
54554
03/03/2020
85296
01/01/2001
...
I have about a thousand of these records (customer number is unique) and the bossman wants to see how the number of customers has grown over time.
The output I need:
Customer_Count
Monthly_Bucket
7
01/01/2021
9
02/01/2021
13
03/01/2021
20
04/01/2021
The customer count is cumulative and the Monthly Bucket will just feed the graphing package to make a nice bar chart answering the question "how many customers to we have in total in a particular month and how is it growing over time".
Try the following SELECT SQL with a sub-query:
SELECT Customer_Count=
(
SELECT COUNT(s.[Create_Date])
FROM [Customer_Sales] s
WHERE MONTH(s.[Create_Date]) <= MONTH(t.[Create_Date])
), Monthly_Bucket=MONTH([Create_Date])
FROM Customer_Sales t
WHERE YEAR(t.[Create_Date]) = ????
GROUP BY MONTH(t.[Create_Date])
Where [Customer_Sales] is the sales table and ??? = your year

Query to calculate average resolve time based on quarter

I have a table having attributes: ticket#, closed date & resolve time.
I need to write a SQL query to calculate the average resolve time in each quarter.
eg in quarter1: 5 tickets are closed (10 days,1 day,3, day,1day.10 days) are resolved time for each ticket
then average resolve time is 5 days
output should be as below
Quarter days
Q1 5
Q2 2 (similarly)
Q3 7
Q4 9
sample data
I really stuck in this query
** This code is tested on Oracle Database. For any syntax related error, you can replace according to you database.
Also, update your column name,date,timestamp,timezone and table name accordingly.
The sample query is like this ...
select temp.quarter,avg(resolve_time)
from
(
select resolve_time,
CASE
WHEN (close_date between TO_DATE('2019-APR-01 00:00','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN') and TO_DATE('2019-JUN-30 23:59','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN')) THEN 1
WHEN (close_date between TO_DATE('2019-JUL-01 00:00','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN') and TO_DATE('2019-SEP-30 23:59','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN')) THEN 2
WHEN (close_date between TO_DATE('2019-OCT-01 00:00','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN') and TO_DATE('2019-DEC-31 23:59','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN')) THEN 3
WHEN (close_date between TO_DATE('2020-JAN-01 00:00','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN') and TO_DATE('2020-MAR-31 23:59','YYYY-MON-DD HH24:MI','NLS_DATE_LANGUAGE=AMERICAN')) THEN 4
ELSE 0 END quarter
from TestStack
) temp
group by temp.quarter;
You can write something like this.
SELECT AVG(resolvetime) AS 'Average Resolve Time'
FROM [TableName]
WHERE closeddate BETWEEN [Start date of Quarter] AND [End date of Quarter]

How do I average the last 6 months of sales within SQL based on period AND year?

How do I average the last 6 months of sales within SQL?
Here are my tables and fields:
IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD,
IM_ItemWhseHistoryByPeriod.FISCALCALYEAR,
And I need to average these fields
IM_ItemWhseHistoryByPeriod.DOLLARSSOLD,
IM_ItemWhseHistoryByPeriod.QUANTITYSOLD,
The hard part I'm having is understanding how to average the last whole 6 months, ie. fsicalcalperiod 2-6(inside fiscalcalyear 2017).
I'm hoping for some help on what the SQL command text should look like since I'm very new to manipulating SQL outside of the UI.
Sample Data
My Existing SQL String:
SELECT IM_ItemWhseHistoryByPeriod.ITEMCODE,
IM_ItemWhseHistoryByPeriod.DOLLARSSOLD,
IM_ItemWhseHistoryByPeriod.QUANTITYSOLD,
IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD,
IM_ItemWhseHistoryByPeriod.FISCALCALYEAR
FROM MAS_AME.dbo.IM_ItemWhseHistoryByPeriod
IM_ItemWhseHistoryByPeriod
ScaisEdge Attempt #1
if fiscalyear and fiscalperiod are number you could use
select avg(IM_ItemWhseHistoryByPeriod.DOLLARSSOLD) ,
avg(IM_ItemWhseHistoryByPeriod.QUANTITYSOLD)
from my_table
where IM_ItemWhseHistoryByPeriod.FISCALCALYEAR = 2017
and IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD between 2 and 6
or for each item code
select itemcode, avg(IM_ItemWhseHistoryByPeriod.DOLLARSSOLD) ,
avg(IM_ItemWhseHistoryByPeriod.QUANTITYSOLD)
from my_table
where IM_ItemWhseHistoryByPeriod.FISCALCALYEAR = 2017
and IM_ItemWhseHistoryByPeriod.FISCALCALPERIOD between 2 and 6
group by itemcode
Try the following solution and see if it works for you:
select avg(DOLLARSSOLD) as AvgDollarSod,
avg(QUANTITYSOLD) as AvgQtySold
from IM_ItemWhseHistoryByPeriod
where FISCALCALYEAR = '2017
and FISCALCALPERIOD between 2 and 6

How to count the number of active days in a dataset with SQL Server 2008

SQL Server 2008, rendered in html via aspx webpage.
What I want to achieve, is to get an average per day figure that makes allowance for missing days. To do this I need to count the number of active days in a table.
Example:
Date | Amount
---------------------
2014-08-16 | 234.56
2014-08-16 | 258.30
2014-08-18 | 25.84
2014-08-19 | 259.21
The sum of the lot (777.961) divided by the number of active days (3) would = 259.30
So it needs to go "count number of different dates in the returned range"
Is there a tidy way to do this?
If you just want that one row of output then this should work:
select sum(amount) / count(distinct date) as your_average
from your_table
Fiddle:
http://sqlfiddle.com/#!2/7ffd1/1/0
I don't know this will be help to you, how about using Group By, Avg, count function.
SELECT Date, AVG(Amount) AS 'AmountAverage', COUNT(*) AS 'NumberOfActiveDays'
FROM YourTable WITH(NOLOCK)
GROUP BY Date
About AVG function, see here: Link

Collecting values from the upper rows [duplicate]

This question already has answers here:
Calculate a Running Total in SQL Server
(15 answers)
Closed 8 years ago.
I have a table in sql server and I want to summarize a cell with upper rows in T-Sql like this:
Quantity Total
-----------------------------
1 1
5 6
12 18
20 38
I use SQL server 2008, How can I achieve this?
Regards, Majid.
You are looking for a cumulative sum, it would appear. If you are using SQL Server 2012 or later, just do:
select quantity, sum(quantity) over (order by quantity)
from table t;
Note the order by quantity. You need to specify the ordering for the cumulative sum, typically doing another sum.
In earlier versions, you can do this using a correlated subquery:
select quantity,
(select sum(t2.quantity)
from table t2
where t2.quantity <= t.quantity
) as total
from table t;