473,396 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How can I return a row when the condition is not met ?

Hi All,
How can I return a row when the condition is not met for example simplified

SELECT Name,SUM(Cost) AS T FROM DB WHERE Name="+Name +" GROUP BY Name

this works fine but I ned it to return Name and Zero Cost if Name does not exist
May 26 '06 #1
2 2229
Co**********@al.com wrote:
How can I return a row when the condition is not met for example simplified
Rows are not returned when conditions are not met. That is the basic
idea behind databases. If you want the "other" rows to be returned, you
need to change your conditions.
SELECT Name,SUM(Cost) AS T FROM DB WHERE Name="+Name +" GROUP BY Name

this works fine but I ned it to return Name and Zero Cost if Name does not exist


If Name doesn't exist, how can you return Name? Or do you mean that you
want to return all rows, but if Name is not equal to "+Name +" you want
Cost to be 0?

If so, then just use the if-sentence in your query.
select Name,if(Name='+Name +',sum(Cost),0) from x group by Name;
May 26 '06 #2
Aggro wrote:
Co**********@al.com wrote:
How can I return a row when the condition is not met for
example simplified

Rows are not returned when conditions are not met. That is the basic
idea behind databases. If you want the "other" rows to be returned, you
need to change your conditions.
SELECT Name,SUM(Cost) AS T FROM DB WHERE Name="+Name +" GROUP BY Name

this works fine but I ned it to return Name and Zero Cost if Name does
not exist

If Name doesn't exist, how can you return Name? Or do you mean that you
want to return all rows, but if Name is not equal to "+Name +" you want
Cost to be 0?

If so, then just use the if-sentence in your query.
select Name,if(Name='+Name +',sum(Cost),0) from x group by Name;


if you are using a "where-clause" that specifies col=value, then you will always
get NO ROWS RETURNED if "value" does not exist. Period. you cannot select that
which does not exist. Something must be returned to compare against for the "if"
statements to evaluate.

Now, to get around that issue you will need to handle it in your program.
psuedo-code :
select x from db where x='somevalue';
if rows = 0 then echo "'somevalue' Cost = 0"

again, when using a where clause - you cannot sum something that does not exist.

case and point (from an SQL92 compliant "real" database engine:

select '5',a, case when a is null then '0' else sum(c) end
from testa where a=5 group by a;
0 rows selected

the other poster obviously never tested any of the syntax using the where
clause. That might work if and only if you returned all rows NOT EQUAL to
'Name' but will not work using an EQUALS evaluation.

Maybe if you described the problem you are tring to solve a bit better, then
there could be an alternative solution.
--
Michael Austin.
DBA Consultant
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Jun 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

94
by: John Bailo | last post by:
The c# *return* statement has been bothering me the past few months. I don't like the fact that you can have different code paths in a method and have multiple return statements. To me, it...
27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
11
by: TTroy | last post by:
Hello C programmers, Can someone tell me why ungetc can't sent back EOF, but it's sister function getc has no trouble sending it to us? For a file, this might not make a difference, but for an...
8
sammyboy78
by: sammyboy78 | last post by:
I'm trying to create a class "WeeklyPay" that contains the methods that class "WeeklyPayTest" will use to compute the weekly pay of an employee when the user inputs employee name, hours worked, and...
13
by: cppquester | last post by:
A colleague told me that there is a rule about good stype that a function in C++ should have only one point of return (ie. return statement). Otherwise there might be trouble. I never heard about...
3
by: bobc | last post by:
Using SQL Server 2000... I wrote a wrapper to call a sub proc (code provided below). The intended varchar value returned in the output parameter of each proc is a string implementation of an...
6
by: exander77 | last post by:
I am quite new to c++, about half of a year. I juct wonder, why such code is not possible: int chlen(char * ar) { for(int i=0;ar||return i;i++); } In my opinion, it would be much "cuter"...
12
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.