Calculating balance Oracle [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've got a problem. I just wanna calc the balance of my table.
my select statement is:
select date, ammount, ?? as Balance
from table
where accountnr = 123
order by date
Output should look like this:
Date Ammount Balance
07/02/2016 -145.55 945.65
25/01/2016 349.45 1091.20
11/11/2015 340.25 741.75
30/09/2015 369.10 401.50
05/04/2015 32.40 32.40
I tried so long, with different ways without luck.

You can do it in a single table scan (i.e. without any joins or correlated sub-queries) with an analytical query:
SELECT "date",
amount,
SUM( amount ) OVER ( ORDER BY "date" ) AS balance
FROM your_table;
If there are multiple accounts in the table then:
SELECT account_number,
"date",
amount,
SUM( amount ) OVER ( PARTITION BY account_number ORDER BY "date" ) AS balance
FROM your_table;

Related

Count distinct monthly running total [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I am trying to find a SQL query to the question of finding the running unique count of distinct customers each month.
This is not the number of distinct customers each month, but rather what is the new incremental total number of unique customers as each month rolls by.
For example, in January I had 10 unique customers and in February I had another 10 customers, but 5 of these who transacted in February were repeat customers and had also transacted in January; so between January and February I really only have a running total of 15 unique customers (10 in January and 5 in February).
What would be the simplest solution to achieve the running unique customer count for each month of the year?
Example Output where (compared to January) in February there were an additional 5 unique customers and in March there were an additional 10 unique customers
This might do:
Select
month,
count(*) as custs,
(select
count(distinct cust_id)
from mytable b
where b.month<=a.month) as RunningUniqueCusts
From mytable a
group by month
Or for month & region
Select
month,
region,
count(*) as custs,
(select
count(distinct cust_id)
from mytable b
where b.month<=a.month
and b.region=a.region) as RunningUniqueCustsForRegion
From mytable a
group by month, region
Update 3-Mar-2022
The following would return the unique customer ids for each month where they didn't appear previously:
SELECT TM.MONTH_ID, TM.CUST_ID
FROM MYTABLE as TM
WHERE NOT EXISTS
(SELECT 1
FROM MYTABLE as PM
WHERE PM.CUST_ID = TM.CUST_ID
and PM.MONTH < TM.MONTH)
GROUP BY TM.MONTH_ID, TM.CUST_ID
ORDER BY TM.MONTH_ID, TM.CUST_ID

Sum of a column that is a SUM [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a query that I inherited:
SELECT SUM(Credit) AS Total, Account, Date
FROM
(
SELECT DISTINCT
CONVERT(char(10), dbo.vCustomer_Invoice.Accounting_Distribution__Document_Date, 101) AS Date
, dbo.vCustomer_Invoice.Accounting_Distribution__Amount_Credit AS "Credit"
, dbo.vCustomer_Invoice.Accounting_Distribution__GL_Account AS "Account"
FROM dbo.vCustomer_Invoice
WHERE CONVERT(char(10), dbo.vCustomer_Invoice.Accounting_Distribution__Document_Date, 101) = '11/03/2020' AND (dbo.vCustomer_Invoice.Accounting_Distribution__GL_Account LIKE '4000%' OR dbo.vCustomer_Invoice.Accounting_Distribution__GL_Account LIKE '4100-700-%')
)
AS D
Group By Account, Date;
This gives me a total by each GL_Account for a date. I'd like to now add a column that Sums by Date. I have in the past used a UNION ALL but I can't get that to work with this query configuration.
Any help would be appreciated.
You can use SUM() window function for this new column:
SELECT SUM(Credit) AS Total,
Account,
Date,
SUM(SUM(Credit)) OVER (PARTITION BY Date) AS Total_By_Date
FROM (
SELECT DISTINCT
CONVERT(CHAR(10), Accounting_Distribution__Document_Date,101) AS Date,
Accounting_Distribution__Amount_Credit AS Credit,
Accounting_Distribution__GL_Account AS Account
FROM dbo.vCustomer_Invoice
WHERE CONVERT(CHAR(10), Accounting_Distribution__Document_Date, 101) = '11/03/2020'
AND (Accounting_Distribution__GL_Account LIKE '4000%' OR Accounting_Distribution__GL_Account LIKE '4100-700-%')
) AS D
GROUP BY Account, Date;

how to get recent 6 dates (Max dates) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I need recent 6 dates count
My code is
select DUE_DATE, count(*)
from DATA
group by DUE_DATE
03/24/2018 10
03/17/2018 20
03/10/2018 15
03/03/2018 23
02/24/2018 42
02/17/2018 32
02/10/2018 15
02/03/2018 17
01/27/2018 23
select DUE_DATE, count(*) from DATA group by DUE_DATE order by DUE_DATE desc limit 6
The simplest case would be if every date is presented and/or you're only interested in 6 most recent dates regardless of whether they're presented in your table or not:
select DUE_DATE, count(*)
from DATA
where DUE_DATE >= trunc(sysdate) - 5
group by DUE_DATE
order by DUE_DATE desc
(Note: dates that are not presented in your table won't be shown).
On the other hand, if you need 6 most recent dates from the subgroup of dates you have in your table, then you'll first need a subquery to fetch you those dates and than use count on those dates only:
select DUE_DATE, count(*)
from DATA
where DUE_DATE in (select distinct DUE_DATE
from DATA
order by DUE_DATE desc limit 6)
group by DUE_DATE
I hope I helped!
Are you looking for fetch first?
select DUE_DATE, count(*)
from DATA
group by DUE_DATE
order by DUE_DATE desc
fetch first 6 rows only;

SQL: how to group by dates in a row? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
It might be already answered before, but apparently I am not googling it right.
Let's say I have a table:
RecordingDate
2018-07-01
2018-07-02
2018-07-06
2018-07-09
2018-07-10
2018-07-11
2018-07-12
2018-07-16
2018-07-17
2018-07-18
I want to group the data by the date of the first recording and count days in a row with recordings:
DateOfFirstRecording RecordingsInARow
2018-07-01 2
2018-07-06 1
2018-07-09 4
2018-07-16 3
How do I do that ?
You can subtract an increasing sequence, to get a constant date. This is the idea:
select min(date), max(date), count(*)
from (select t.*, row_number() over (order by date) as seqnum
from t
) t
group by date - seqnum * interval '1 day'
order by min(date);
Date operations vary significantly depending on the database, but all databases support this functionality with some syntax.
EDIT:
In SQL Server, this looks like:
select
min(RecordingDate) as DateOfFirstRecording,
count(*) as RecordingsInARow
from (select Recordings.*, row_number() over (order by RecordingDate) as seqnum
from Recordings
) t
group by dateadd(day, - seqnum, RecordingDate)
order by min(RecordingDate);

select km difference of a vehicle beween from date to Todate in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
SELECT
VEHICLE_ID,
DRIVER_ID,
SUM(AMOUNT) as Total_Amount,
SUM(ODOMETER) as Total_Odometer,
SUM(LTR) As Total_Ltr
FROM Fuel_Filling
WHERE
COMPANY_ID = #COMPANY_ID
AND VEHICLE_ID = #VEHICLE_ID
AND DATE_TIME BETWEEN #FROM_DATE AND #TO_DATE
GROUP BY VEHICLE_ID, DRIVER_ID
I want to select ODOMETER Between Dates
If ODOMETER is truly the odometer reading then this should work:
SELECT
VEHICLE_ID,
DRIVER_ID,
SUM(AMOUNT) as Total_Amount,
MAX(ODOMETER) - MIN(ODOMETER) as DistanceTravelled,
SUM(LTR) As Total_Ltr
FROM Fuel_Filling
WHERE
COMPANY_ID = #COMPANY_ID
AND VEHICLE_ID = #VEHICLE_ID
AND DATE_TIME BETWEEN #FROM_DATE AND #TO_DATE
GROUP BY VEHICLE_ID, DRIVER_ID