Solved

SQL query issue

Posted on 2013-05-12
5
249 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
  • 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 300 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

12 Experts available now in Live!

Get 1:1 Help Now