Windows Develop Bookmark and Share   
 index > Windows Forms General > SQL help
 

SQL help

Hi, I have been writing a query to get sum of few columns and I am not able to get the rows that has atleast one column with no zero.
eg.
ExpenseA ExpenseB ExpenseC
$1000 $0 $0
$0 $200 $300
$0 $0 $0

and so on.

Now how do I get get only the first 2 rows not the third row since all the columns are zero. Here is how I have my query
Select sum(ExpenseA), sum(ExpenseB), sum(ExpenseC) from tblExpenses where ExpenseA > 0 or ExpenseB > 0 or ExpenseC > 0
I am still getting all the rows. Is there some built in sql function that will do this?

Thank you.
Ravi Santha  Monday, November 05, 2007 6:49 PM
if there's no aggregation involved, your original query wont work either.



it should be

Select ExpenseA, ExpenseB, ExpenseC from tblExpenses where
ExpenseA > 0 or ExpenseB > 0 or ExpenseC > 0

and it should work.
H. _冬_ Tony  Monday, November 05, 2007 7:14 PM
why you show Sum but the condition is on Expense?

how about

Select sum(ExpenseA), sum(ExpenseB), sum(ExpenseC) from tblExpenses where sum(ExpenseA) > 0 or sum(ExpenseB) > 0 or sum(ExpenseC) > 0
H. _冬_ Tony  Monday, November 05, 2007 6:57 PM
I tried that but I am getting the following error

"An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference."

thank you
Ravi Santha  Monday, November 05, 2007 7:05 PM
if there's no aggregation involved, your original query wont work either.



it should be

Select ExpenseA, ExpenseB, ExpenseC from tblExpenses where
ExpenseA > 0 or ExpenseB > 0 or ExpenseC > 0

and it should work.
H. _冬_ Tony  Monday, November 05, 2007 7:14 PM
thank you
Ravi Santha  Monday, November 05, 2007 7:31 PM

You can use google to search for other answers

Custom Search

More Threads

• KeyPress
• Autohide menu controls?
• prevent typing in dateTimePicker?
• why cannot get new processes list?
• Message blocking panel
• Delete a row programmatically in DataGridView
• Application.SetCompatibleTextRenderingDefault(false); problem
• How do I trigger SettingChanging from custom property of ApplicationSettingsBase?
• application learning question
• Multiple keys in hotkey?