473,809 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2253
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
13914
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 would be more orthogonal if a method could only have one return statement. --
27
2677
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
7666
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 scanf cannot fill any fields it returns EOF. I have run some tests on scanf and, so far, I've not found an EOF return. For example: If the programer formats scanf for an int or
11
3494
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 interactive terminal, it is probably nice to push EOF back (because to user doesn't want to provide an EOF twice). How is it getc can send EOF down it's pipe, but we can't send EOF down ungetc's pipe (especially when this pipe is the same)? ...
8
1865
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 pay rate. I get an error when trying to compile WeeklyPay.java: WeeklyPay.java:78: missing return statement }//end displayPay ^ Also if anyone sees anything else that's going to give me a problem please tell me about it....
13
2916
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 it and doubt it. Anybody heard of it? What would be the advantage? Regards, Marc Example:
3
2281
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 array. (The string separates elements by adding a period after each value. e.g. 1. 2. 3. 4. 5. etc., although my simplified example only creates two elements.) My vb.net calling code parses the returned string into individual elements.
6
1836
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" than this: int chlen(char * ar) {
12
1481
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 (hashString == "MD5") { return System.Security.Cryptography.MD5.Create(); } else { return System.Security.Cryptography.SHA512.Create(); }
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.