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)
Related
I'm trying to produce a report with the total invoice amount for each customer in the month of December :
date
customer
invoice amount
01/12/2021
AB1
40
02/11/2021
AB2
60
12/12/2021
CE6
1000
31/12/2021
RF9
0.5
Could I get any pointers? I'm still fairly new to postgresql.
You should use GROUP BY for your purposes.
SELECT customer, SUM(invoice_amount) as total_invoice_amount
FROM your_table
WHERE EXTRACT(MONTH FROM date) = 12
GROUP BY customer
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;
I have an SQL View Name dbo.financialactuals
ACCTNAME Month Balance Acctctgry Year Description
sales 1 344.78 income 2018 revenueAX
sales 2 2744.78 income 2018 ProduceAX
sales 3 8745.78 income 2018 annualAx
INTEREST INC 1 7866 Interest 2018 ProduceAX
INTEREST INC 2 766 Interest 2018 CTGAX3
sales 5 744.78 other 2018 AX
I tried to sum balance by AcctName But it still showing the same result. I think it's because of the description column. So, I removed the description from the select statement but it's acting same.
SELECT AcctName,
SUM(Balance) AS Balance,
AcctCtrgry,
Year,
FROM dbo.financialactuals
GROUP BY AcctName, Acctctrgy, Year,
I need an output like this.
AcctNAme Balance AcctCtrgy Year
sales -3089.56 income 2018
INTEREST INC 9632 InterestINC 2018
As it was said already in the comments this should give the requested result.
SELECT AcctName,
SUM(Balance) AS Balance,
AcctCtrgry,
Year,
FROM dbo.financialactuals
GROUP BY AcctName, Acctctrgy, Year
But the sum on the sales will be -11835.34 not -3089.56.
I would like to run a query that calculates maximum money spent for each month of each credit card. For each credit card, I will need to calculate the sum of money spent each month. I have a table containing transactions of credit cards credit_transact:
processdate timestamp ""
cardno_hash string ""
amount int ""
year int ""
month int ""
Made-up sample data:
card year month amount
a123 2016 12 23160
a123 2016 10 287
c123 2016 11 5503
c123 2016 11 4206
I would like:
card year month amount
a123 2016 12 23160
c123 2016 11 9709
One important thing is year and month are partition columns.
I have tried a subquery like below:
USE credit_card_db;
SELECT sum_amount_transact.cardno_hash, sum_amount_transact.year, sum_amount_transact.month, MAX(sum_amount_transact.sum_amount)
FROM
(
SELECT cardno_hash, year, month, SUM(amount) AS sum_amount FROM credit_transact
GROUP BY cardno_hash, year, month
) AS sum_amount_transact
GROUP BY sum_amount_transact.cardno_hash, sum_amount_transact.year;
However, the following error is shown:
java.lang.Exception: org.apache.hive.service.cli.HiveSQLException: Error while compiling statement: FAILED: SemanticException Line 0:-1 Invalid column reference 'month'
The following subquery worked fine and returned results as expected:
SELECT cardno_hash, year, month, SUM(amount) AS sum_amount FROM credit_transact
GROUP BY cardno_hash, year, month
The result is:
card year month amount
a123 2016 12 23160
a123 2016 10 287
c123 2016 11 9709
Would very much appreciate if anyone can help with this problem.
I can't quite tell what you really want, but I'm pretty sure you want row_number(). I think you want the maximum month per year:
SELECT ct.*
FROM (SELECT cardno_hash, year, month, SUM(amount) AS sum_amount,
ROW_NUMBER() OVER (PARTITION BY cardno_hash, year ORDER BY SUM(amount) DESC) as seqnum
FROM credit_transact
GROUP BY cardno_hash, year, month
) ct
WHERE seqnum = 1;
I'm looking everywhere for an answer but nothing seems to compare with my problem. So, using rollup with query:
select year, month, count (sale_id) from sales
group by rollup (year, month);
Will give the result like:
YEAR MONTH TOTAL
2015 1 200
2015 2 415
2015 null 615
2016 1 444
2016 2 423
2016 null 867
null null 1482
And I would like to sort by total desc, but I would like year with biggest total to be on top (important: with all records that compares to that year), and then other records for other years. So I would like it to look like:
YEAR MONTH TOTAL
null null 1482
2016 null 867
2016 1 444
2016 2 423
2015 null 615
2015 2 415
2015 1 200
Or something like that. Main purpose is to not "split" records comparing to one year while sorting it with total. Can somebody help me with that?
Try using window function max to get max of total for each year in the order by clause:
select year, month, count(sale_id) total
from sales
group by rollup(year, month)
order by max(total) over (partition by year) desc, total desc;
Hmmm. I think this does what you want:
select year, month, count(sale_id) as cnt
from sales
group by rollup (year, month)
order by sum(count(sale_id)) over (partition by year) desc, year;
Actually, I've never use window functions in an order by with a rollup query. I wouldn't be surprised if a subquery were necessary.
I think you need to used GROUPING SETS and GROUP_ID's. These will help you determine a NULL caused by a subtotal. Take a look at the doc: https://docs.oracle.com/cd/B19306_01/server.102/b14223/aggreg.htm