[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL query issue

Posted on 2013-05-12
5
Medium Priority
?
258 Views
Last Modified: 2013-05-12
Hi I have one table where it has two kinds of record the example would be bellow:

transationtbl
account | total amount used | type of transaction
6565 | 1000 | local
6565 |200|international
5252 | 300 | local
4565 |1000| international
4565 |300| local
1020 | 100| local

so what I want a result with SQL from this is to list all accounts that have used ONLY local transaction if they used both local/international I don't need them or if they used only International ... I only need LOCAL made transaction

thanks for help
0
Comment
Question by:hi4ppl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39159366
Try this:

SELECT account, SUM([type of transaction = 'local') AS CountLocal, SUM([type of transaction = 'international') AS CountInternational
FROM YourTable
GROUP BY account
HAVING SUM([type of transaction = 'local') <> 0 AND SUM([type of transaction = 'international') =0

Open in new window

0
 
LVL 1

Author Comment

by:hi4ppl
ID: 39159401
Hi this is not working it gives syntax error and im trying in postgress, plus I see you opened [ and never closed it maybe im wrong but I didn't understand this.

and when I remove [ completely I get syntax error at where it says type of transaction

thanks for help
0
 
LVL 61

Accepted Solution

by:
mbizup earned 1200 total points
ID: 39159438
Yep - that was a typo.  Did you try closing the brackets?

SELECT account, SUM([type of transaction] = 'local') AS CountLocal, SUM([type of transaction] = 'international') AS CountInternational
FROM YourTable
GROUP BY account
HAVING SUM([type of transaction] = 'local') <> 0 AND SUM([type of transaction] = 'international') =0

Open in new window



If field names have spaces in them, you need to enclose them in [] brackets (Access and SQL Server)... not sure about postgress, but that also most likely needs some kind of delimiter around any field name containing spaces.
0
 
LVL 1

Author Comment

by:hi4ppl
ID: 39159444
Thanks I got the logic I made some modification and it worked instead of type='local' I used sum(case when transactiontype='local' then 1 else 0 end)

thanks for your time and help
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39159452
Good for you working that out!  :-)

That case syntax is the best alternative since it requires only one column.  I forget it is an option... It is not available in Access (which is my primary database development platform).
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

656 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