Solved

Microsoft SQL query

Posted on 2016-11-02
7
37 Views
Last Modified: 2016-11-07
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
Comment
Question by:Bob Tian
  • 5
  • 2
7 Comments
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Do you have sample data and the expected output ?
1
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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
 

Author Comment

by:Bob Tian
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Yes it is branchName <<Typo>> :) Let me rewrite it for you ! Wait for sometime.
1
 
LVL 17

Accepted Solution

by:
Pawan Kumar Khowal earned 500 total points
Comment Utility
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
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
Hi Bob,
Is this done?

Regards,
Pawan
0
 

Author Closing Comment

by:Bob Tian
Comment Utility
thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now