Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MY SQL QUERY For multiple users

Posted on 2013-01-05
3
Medium Priority
?
291 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 400 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
The viewer will learn how to count occurrences of each item in an array.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

972 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