SQL Columns Grouping - sql

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.

Related

Extract list of Sellers with TTM > 100k for the last 3 months in SQL (PostgreSQL)

Quick question:
I want to extract a list of sellers with TTM sales > 100k for the last 3 months. I need a snapshot of the data for the last 3-4 months:
Sellers which have TTM sales > 100k in January
Sellers which have TTM sales > 100k in Febr
Sellers which have TTM sales > 100k in March
I want to create it in a dynamic way on which if I want to extract the data for 6 months, only need to change > Jan to > Oct and store the data in a table
Eg. Jan - 100 sellers, Febr. 200 sellers, March - 75 Sellers
Table used:
Seller list (id, marketplace)
Sales (id, marketplace, sale_day, sale_amount)
Output 1 - TTM:
Output 2 - TTM (only sellers > 100k):
This last output has to be dynamic. I want to get the last 3 months data based on a "RUN_DATE". per eligible seller
TTM = Trailing Twelve Months (sales per seller on the last 12 months)
eg. Sales in Jan (Jan 2020 - Dec 2020)
Sales in Febr (Febr 2020 - Jan 2021)
Thise has to be filterer by 100k.
My actual logic is to take a snapshot of them per month Jan , Febr, March and union.
I suspect you want aggregation:
select id, date_trunc('month', sale_date) as yyyymm,
sum(sale_amount) as total_sales
from sales
group by yyyymm, id;
Then for the last 3 months and filtering, you would do:
select id, date_trunc('month', sale_date) as yyyymm,
sum(sale_amount) as total_sales
from sales s
where s.sale_date >= date_trunc('month', now()) - interval '3' month
group by yyyymm, id
having sum(sale_amount) > 100000;

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)

Hive: error calculating SUM then MAX of grouped items

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;

SQL Difference Between Sum of Two Months

I'm trying to find the difference between the previous years month and current years month. An example would be the SUM of sales for January 2013 and the difference of SUM of sales for January 2014 sales. This is being done to see how much we made from the previous year. I have a group by that shows the total sales by month and year. I'm having trouble on defining how to find the difference between the two months. Thank you for your help. Its greatly appreciated.
Table
Date Sales
1/1/2013 100
1/12/2013 150
1/21/2013 90
1/4/2014 200
1/17/2014 50
1/20/2014 100
Result of Group By
Jan 2013
340
Jan 2014
350
Difference
Jan 2014 - Jan 2013
340 - 350 = 10
The best way to do this depends on the database. The first thing you need to do is to aggregate the data. Then a simple join will get the data you need. Here is one method:
with ym as (
select year(date) as yr, month(date) as mon,
sum(sales) as sales
from table t
group by year(date), month(date)
)
select ym.yr, ym.mon, ym.sales, ymprev.sales as prev_sales,
(ym.sales - ymprev.sales) as diff
from ym join
ym ymprev
on ymprev.yr = ym.yr - 1 and ymprev.mon = ym.mon;

SQL statement to generate a table of totals by month

I am very unfamiliar with advanced SQL.
Lets say I have the following table (in Access - using Jet 4.0 OLEDBAdapter in VB.NET).
Table - Items
ID Date Account Amount
----- ------ ------- ------
1 1/1/2013 Cash 10.00
2 2/1/2013 Cash 20.00
3 1/2/2013 Cash 30.00
4 2/2/2013 Cash 40.00
5 1/1/2013 Card 50.00
6 2/1/2013 Card 60.00
7 1/2/2013 Card 70.00
8 2/2/2013 Card 80.00
And I want to generate the following - totals for each account per month
Table - Totals
Account Jan Feb
----- ----- ------
Cash 30.00 70.00
Card 110.00 150.00
Is this possible using one SQL statement. I can do it in two but it is very slow.
Edit - the closest I have got is this - but it doesn't generate columns
SELECT accFrom, Sum(amount)
FROM Items
WHERE Year(idate) = '2012'
GROUP BY Month(idate), accFrom
Using your sample data, this is the output I got from the query below with Access 2010.
Account 2013-01 2013-02
------- ------- -------
Card $120.00 $140.00
Cash $40.00 $60.00
My totals don't match your expected output. I suspect your date values were d-m-yyyy format, but my US locale interpreted them as m-d-yyyy. It's better to present dates in yyyy-m-d format to avoid that confusion.
Anyway this query formats the dates as yyyy-mm, and then pivots to generate the columns for each year-month combination. So it will accommodate a growing date range without requiring you to modify the query. And, as the date range grows, you could eventually add a WHERE clause to limit to columns to a convenient subset.
TRANSFORM Sum(i.Amount) AS SumOfAmount
SELECT i.Account
FROM Items AS i
GROUP BY i.Account
PIVOT Format(i.Date,'yyyy-mm');
Since there are exactly 12 months in a year, you do not need to pivot; just calculate the sum for each month:
SELECT Account,
Sum(IIF(Month(Date)=01, Amount, 0)) AS Jan,
Sum(IIF(Month(Date)=02, Amount, 0)) AS Feb,
Sum(IIF(Month(Date)=03, Amount, 0)) AS Mar,
Sum(IIF(Month(Date)=04, Amount, 0)) AS Apr,
Sum(IIF(Month(Date)=05, Amount, 0)) AS May,
Sum(IIF(Month(Date)=06, Amount, 0)) AS Jun,
Sum(IIF(Month(Date)=07, Amount, 0)) AS Jul,
Sum(IIF(Month(Date)=08, Amount, 0)) AS Aug,
Sum(IIF(Month(Date)=09, Amount, 0)) AS Sep,
Sum(IIF(Month(Date)=10, Amount, 0)) AS Oct,
Sum(IIF(Month(Date)=11, Amount, 0)) AS Nov,
Sum(IIF(Month(Date)=12, Amount, 0)) AS "Dec"
FROM Items
WHERE Year(Date) = 2013
GROUP BY Account
Goose is right, you'll need to pivot on your Date column and use SUM() as the aggregate.
The syntax will look something similar to:
SELECT account, [1/1/2013] as jan, [2/1/2013] as feb, ... -- each month you want to select
FROM
(
SELECT date, account, amount FROM items
)
PIVOT
(
SUM(amount) FOR date IN
(
[1/1/2013], [2/1/2013], ... -- each date you want to have its own column
)
) AS pvt