select monthly report where per day transction show in single row - sql

Date - Amount
12-nov-2016 200
12-nov-2016 100
13- nov -2016 400
13 -nov-2016 200
result show like-
Date Amount
12- nov-2016 300
13- nov-2016 600
as whole month

select [Date], sum(Amount) as Amount from yourTable group by [Date]

Related

Rolling n-day aggregation conditional on another column

struggling to figure out how to implement a code that will allow me to calculate following (using SQL in BigQuery) in an elegant way.
I'd need to calculate a rolling n-day aggregation (let's assume rolling 3-day sum of units ) for each date but only taking into account data that where the price is less than a certain value (let's assume 50).
So based on below table
date
price
units
01-21
30
200
01-22
100
500
01-23
20
200
01-24
20
100
01-25
80
100
01-26
40
250
I'd need my query to return:
date
units
01-21
200
01-22
200
01-23
400
01-24
300
01-25
300
01-26
350
Struggling to figure out how to combine window calculations with the additional conditions.
Thanks in advance!
Consider below approach
select date, sum(if(price < 50, units, 0)) over win units
from your_table
window win as (order by unix_date(date) range between 2 preceding and current row)
if applied to sample data as in your question -
with your_table as (
select date '2022-01-21' date, 30 price, 200 units union all
select '2022-01-22', 100, 500 union all
select '2022-01-23', 20, 200 union all
select '2022-01-24', 20, 100 union all
select '2022-01-25', 80, 100 union all
select '2022-01-26', 40, 250
)
the output is

Sum Amount Per Month Between Date Ranges

I have a table that stores scheduled charges within a date range. I am looking to sum the values per month, but am unsure how to structure the query to handle the range logic. A simplified version of the table structure is below where dates are in MM/DD/YYYY format:
Start End Amount
01/15/2020 04/30/2020 200
02/05/2020 06/30/2020 300
03/01/2020 12/15/2020 400
04/02/2020 10/25/2020 500
The output would display a sum for every month there are records for based on an entry existing in the Start or End column for that month. So the output would look like this:
Month Amount
01/2020 200
02/2020 500
03/2020 900
04/2020 1400
05/2020 1200
06/2020 1200
07/2020 900
08/2020 900
09/2020 900
10/2020 900
11/2020 500
12/2020 500
Any ideas of how to handle this without a separate table storing each month/year to check if it is within the range?
You can use a recursive CTE and then aggregate:
with cte as (
select datefromparts(year(start), month(start), 1) as dte, end, amount
from t
union all
select dateadd(month, 1, dte), end, amount
from cte
where eomonth(dte) < eomonth(end)
)
select dte, sum(amount)
from cte
group by dte;
Here is a db<>fiddle.

How to get monthwise sum from table?

Table Transaction(Id, DateTime, Debit, Credit)
I want a monthwise sum of Debit and Credit.
What is a good option to retrieve monthwise result?
Sample Output:
Month Id Debit Credit
January 1 200 70
January 2 400 80
February 1 400 90
February 2 300 50
Try this below script with GROUP BY function. I have added YEAR in consideration other wise same month from different year will count as same month.
SELECT YEAR(DateTime),
MONTH(DateTime),
Id,
SUM(Debit) total_debit,
SUM(Credit) total_credit
FROM your_table
GROUP BY YEAR(DateTime), MONTH(DateTime), Id
Apply Group by clause to SQL Query
group by month(DateTime),Year(DateTime)

sql quest with amount and exchange rate

How to choose customers who have made a large amount of payments in December 2018 if we take into account the exchange rate
I have a table:
Trandate date - transaction date
Transum numeric (20,2) - amount of payment
CurrencyRate numeric (20,2) - currency exchange rate
ID_Client Trandate Transum CurrencyRate Currency
--------------------------------------------------------
1 2018.12.01 100 1 UAH
1 2018.12.02 150 2 USD
2 2018.12.01 200 1 UAH
3 2018.12.01 250 3 EUR
3 2018.12.02 300 1 UAH
3 2018.12.03 350 2 USD
7 2019.01.08 600 1 UAH
but I think that "max" is not at all what I need
SELECT ID_Client, MAX(Transum*CurrencyRate)
FROM `Payment.TotalPayments`
WHERE YEAR(Trandate) = 2018
AND MONTH(Trandate) = 12
I need something this
ID_Client Transum
3 1750
Where 1750 is a "UAH" and 350USD + 300UAH + 250EUR, exchange rate of USD is 2, exchange rate of EUR is 3.
If you're trying to get the sum of transaction amounts by client for the year 2018 and month of December, you could write it like this:
SELECT ID_Client, SUM(Transum*CurrencyRate) as payment_total_converted
FROM `Payment.TotalPayments`
WHERE YEAR(Trandate) = 2018
and MONTH(Trandate) = 12
group by ID_Client
If you want things grouped by each client, year, and month in a given date range, you'd write it like this:
SELECT ID_Client, YEAR(Trandate) as tran_year, MONTH(Trandate) as tran_month,
SUM(Transum*CurrencyRate) as payment_total_converted
FROM `Payment.TotalPayments`
WHERE Trandate between '2018-12-01' and '2019-01-01'
group by ID_Client, YEAR(Trandate), MONTH(Trandate)
I added a column name for your computed column so that the result set is still relational (columns need distinct names).
I'd recommend reading up on the SQL 'group by' clause (https://www.w3schools.com/sql/sql_groupby.asp) and aggregate (https://www.w3schools.com/sql/sql_count_avg_sum.asp, https://www.w3schools.com/sql/sql_min_max.asp) operators.
I think you want sum(). Then you can order by the result:
SELECT ID_Client, SUM(Transum*CurrencyRate) as total
FROM `Payment.TotalPayments`
WHERE Trandate >= '2018-12-01' AND Transdate < '2019-01-01'
GROUP BY ID_Client
ORDER BY total DESC;

How to calculate two columns data with conditions in sql

Date Quantity Transaction_Type
11-07-2017 200 Sale
15-07-2017 200 Purchase
20-07-2017 500 Sale
10-08-2017 200 Purchase
10-10-2017 200 Purchase
12-10-2017 200 Sale
12-12-2017 200 Sale
20-12-2017 500 Sale
How can i get the below output.
Month Quantity
07 500
10 0
12 700
You seem to want:
select to_char(date, 'YYYY-MM') as yyyymm,
sum(case transaction_type when 'Purchase' then quantity
when 'Sale' then - quantity
end)
from t
group by yyyymm
order by yyyymm;
Note that I included the year in the month column. If you don't want the year, remove it from the to_char() format.