Solved

MY SQL QUERY For multiple users

Posted on 2013-01-05
3
284 Views
Last Modified: 2013-01-05
Hello Experts,

Stuck with the sql query!
One table contains status and amount and users for a specific proj.
column1,column2,column3,column4
p1,wip,100,user1
p2,wip,100,user2
p3,wip,100,user3

Open in new window

And, the query I tried as
SELECT sum(column3) AS total FROM table1 WHERE column2='wip' and column4='user1' OR column4='user2' OR column4='user3'

Open in new window

This query, returns the result as required. However, if I change column2='unbilled' then also it returns the same results.

I am stuck at this position, as there might be ton's of data and we required to get this column2 and these three users were the fixed users.

Can you suggest the correction in this or any refeerence?

Thanks,
Shail
0
Comment
Question by:ShaileshShinde
3 Comments
 
LVL 15

Accepted Solution

by:
gplana earned 100 total points
ID: 38746708
You should add parentesis:
SELECT sum(column3) AS total FROM table1 WHERE column2='wip' and (column4='user1' OR column4='user2' OR column4='user3')

Open in new window


or even better use IN operator instead of ORs:

SELECT sum(column3) AS total FROM table1 WHERE column2='wip' and column4 IN ('user1','user2','user3')

Open in new window

0
 
LVL 18

Expert Comment

by:Andrej Pirman
ID: 38746710
Try with parenthesis or order your OR's and AND's properly, like 2+2*2 is not 8, but 6 :)

Your query is actually:
SELECT sum(column3)
AS total
FROM table1
WHERE ( column2='wip' and column4='user1' )
     OR column4='user2'
     OR column4='user3'
;

Fit your desired results.
0
 
LVL 1

Author Closing Comment

by:ShaileshShinde
ID: 38746748
Thanks A Lot!
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

919 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

17 Experts available now in Live!

Get 1:1 Help Now