Solved

MY SQL QUERY For multiple users

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

830 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