Solved

MY SQL QUERY For multiple users

Posted on 2013-01-05
3
289 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
[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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

636 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