[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 68
  • Last Modified:

Microsoft SQL query

Hello, I am have trouble with this query,

Account number, balance, sum of transaction amounts, and balance - transaction sum for accounts in the New York branch that have at least ten transactions, order by account number.

Customer = {customerID, firstName, lastName, income, birthDate }
Account = {accNumber, type, balance, branchNumber(FK-Branch)}
Owns = {customerID(FK-Customer), accNumber(FK-Account)}
Transactions = {transNumber, accNumber(FK-Account), amount}
Employee = {sin, firstName, lastName, salary, branchNumber(FK-Branch)}
Branch = {branchNumber, branchName, managerSIN(FK-Employee), budget}

I am confused on how to check if they have at least 10 transactions. I have done an inner  join Account, Branch and Transactions but am unsure how to deal with the number of transactions done per account.
0
Bob Tian
Asked:
Bob Tian
  • 5
  • 2
1 Solution
 
Pawan KumarDatabase ExpertCommented:
Do you have sample data and the expected output ?
1
 
Pawan KumarDatabase ExpertCommented:
Try.. this

SELECT    
      a.accNumber          [Account]
	, a.balance            [balance]       
	, SUM(T.TransactionAmount) [Sum of Transactions]
	, a.balance - SUM(CASE WHEN branchNumber = 'New York' THEN T.TransactionAmount ELSE 0 END) Extracol
FROM Account a
INNER JOIN Branch b ON a.branchNumber = b.branchNumber
INNER JOIN Transactions T ON T.accNumber = a.accNumber
GROUP BY
      b.branchName
    , a.type
    , a.accNumber    
HAVING COUNT(T.transNumber) > 10
ORDER BY a.accNumber

Open in new window

1
 
Bob TianAuthor Commented:
I think its b.branchName not b.branchNumber = 'New York'

Also the output would be something like:
AccNum  balance         Sum of Transactions     balance-Sum of Transactions
1               118231.13      12222                               106009.13
2               100808.03      2333                               98475.03

The problem is, I forgot to mention, is that the {transNumber} in Transaction does NOT indicate the amount of transactions done, it only represents a transaction number, combined with account number it uniquely identify a transaction. I am unsure how to find out how many transactions is done per account
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Pawan KumarDatabase ExpertCommented:
Yes it is branchName <<Typo>> :) Let me rewrite it for you ! Wait for sometime.
1
 
Pawan KumarDatabase ExpertCommented:
Updated...

This is how we can count transactions for each account.
, COUNT(*) OVER (PARTITION BY a.accNumber) TransactionPerAccount

SELECT    
      a.accNumber          [Account]
	, a.balance            [balance]       
	, SUM(T.TransactionAmount) [Sum of Transactions]
	, a.balance - SUM(CASE WHEN branchName = 'New York' THEN T.TransactionAmount ELSE 0 END) Extracol
	, COUNT(*) OVER (PARTITION BY a.accNumber) TransactionPerAccount
FROM Account a
INNER JOIN Branch b ON a.branchNumber = b.branchNumber
INNER JOIN Transactions T ON T.accNumber = a.accNumber
GROUP BY
      b.branchName
    , a.type
    , a.accNumber    
HAVING COUNT(*) > 10
ORDER BY a.accNumber

Open in new window


You want only those records where each account has more than 10 transaction ? If Yes then use below -

SELECT * FROM 
(
	SELECT    
		  a.accNumber          [Account]
		, a.balance            [balance]       
		, SUM(T.TransactionAmount) [Sum of Transactions]
		, a.balance - SUM(CASE WHEN branchName = 'New York' THEN T.TransactionAmount ELSE 0 END) Extracol
		, COUNT(*) OVER (PARTITION BY a.accNumber) TransactionPerAccount
	FROM Account a
	INNER JOIN Branch b ON a.branchNumber = b.branchNumber
	INNER JOIN Transactions T ON T.accNumber = a.accNumber
	GROUP BY
		  b.branchName
		, a.type
		, a.accNumber  
)x
WHERE x.TransactionPerAccount > 10 	  
ORDER BY x.Account

--

Open in new window



Hope it helps !!
1
 
Pawan KumarDatabase ExpertCommented:
Hi Bob,
Is this done?

Regards,
Pawan
0
 
Bob TianAuthor Commented:
thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now