473,805 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return a count of 0

max
I am beginning to learn sql and need some help.

I have a table of customers with their addresses. Let's say I want to
run a query returning the number of customers whose last name is
"Smith" by state.

If I use the following:

SELECT customers.state , Count(*) AS [Count Last Name]
FROM customers
WHERE (((customers.la st_name)="Smith "))
GROUP BY customers.state ;

it returns almost what I want. If there are no "Smiths" in a
particular state, then that state is ommitted in my query result. I
would like to return that state with a count field value of 0 if a
particular name doesn't exist in that state. Do I need to use some
kind of conditional expression to accomplish this? Thanks for any
ideas.

max

Oct 17 '06 #1
4 5653
max wrote:
I am beginning to learn sql and need some help.

I have a table of customers with their addresses. Let's say I want to
run a query returning the number of customers whose last name is
"Smith" by state.

If I use the following:

SELECT customers.state , Count(*) AS [Count Last Name]
FROM customers
WHERE (((customers.la st_name)="Smith "))
GROUP BY customers.state ;

it returns almost what I want. If there are no "Smiths" in a
particular state, then that state is ommitted in my query result. I
would like to return that state with a count field value of 0 if a
particular name doesn't exist in that state. Do I need to use some
kind of conditional expression to accomplish this? Thanks for any
ideas.

max
You'll need a StateList table to be added to the query. In the query
builder add both tables. Click on the relationship line and select the
option for All records In StatesList and those that match in Customers.
You'll also need to group on the StatesList state name, not the state
in the customers file
Oct 17 '06 #2
max
salad wrote:
max wrote:
I am beginning to learn sql and need some help.

I have a table of customers with their addresses. Let's say I want to
run a query returning the number of customers whose last name is
"Smith" by state.

If I use the following:

SELECT customers.state , Count(*) AS [Count Last Name]
FROM customers
WHERE (((customers.la st_name)="Smith "))
GROUP BY customers.state ;

it returns almost what I want. If there are no "Smiths" in a
particular state, then that state is ommitted in my query result. I
would like to return that state with a count field value of 0 if a
particular name doesn't exist in that state. Do I need to use some
kind of conditional expression to accomplish this? Thanks for any
ideas.

max
You'll need a StateList table to be added to the query. In the query
builder add both tables. Click on the relationship line and select the
option for All records In StatesList and those that match in Customers.
You'll also need to group on the StatesList state name, not the state
in the customers file
I tried to do what you recommended. But it is still not returning the
results I want. Here is the sql statement that was generated

SELECT [StateList].state, Count(*) AS [Count Last Name]
FROM [StateList] LEFT JOIN customers ON [StateList].state =
customers.state
WHERE (((customers.la st_name)="Smith "))
GROUP BY [StateList].state;

Am I doing something wrong? Thanks

Oct 18 '06 #3
max wrote:
salad wrote:
>>max wrote:
>>>I am beginning to learn sql and need some help.

I have a table of customers with their addresses. Let's say I want to
run a query returning the number of customers whose last name is
"Smith" by state.

If I use the following:

SELECT customers.state , Count(*) AS [Count Last Name]
FROM customers
WHERE (((customers.la st_name)="Smith "))
GROUP BY customers.state ;

it returns almost what I want. If there are no "Smiths" in a
particular state, then that state is ommitted in my query result. I
would like to return that state with a count field value of 0 if a
particular name doesn't exist in that state. Do I need to use some
kind of conditional expression to accomplish this? Thanks for any
ideas.

max

You'll need a StateList table to be added to the query. In the query
builder add both tables. Click on the relationship line and select the
option for All records In StatesList and those that match in Customers.
You'll also need to group on the StatesList state name, not the state
in the customers file


I tried to do what you recommended. But it is still not returning the
results I want. Here is the sql statement that was generated

SELECT [StateList].state, Count(*) AS [Count Last Name]
FROM [StateList] LEFT JOIN customers ON [StateList].state =
customers.state
WHERE (((customers.la st_name)="Smith "))
GROUP BY [StateList].state;

Am I doing something wrong? Thanks
Nope. I gave a pointer but not the ideal. There are times when Count
won't work. In this case we were filtering on the lastname being Smith.
And that filtered most of your records out. What we needed to do was
sum...if it's Smith, add 1, else add 0.

SELECT StateList.State , Sum(IIf([LastName]="Smith",1,0 )) AS Expr1
FROM StateList LEFT JOIN Customers ON StateList.State = Customers.State
GROUP BY StateList.State ;

See if this works.
Oct 18 '06 #4
max

salad wrote:
max wrote:
salad wrote:
>max wrote:

I am beginning to learn sql and need some help.

I have a table of customers with their addresses. Let's say I want to
run a query returning the number of customers whose last name is
"Smith" by state.

If I use the following:

SELECT customers.state , Count(*) AS [Count Last Name]
FROM customers
WHERE (((customers.la st_name)="Smith "))
GROUP BY customers.state ;

it returns almost what I want. If there are no "Smiths" in a
particular state, then that state is ommitted in my query result. I
would like to return that state with a count field value of 0 if a
particular name doesn't exist in that state. Do I need to use some
kind of conditional expression to accomplish this? Thanks for any
ideas.

max
You'll need a StateList table to be added to the query. In the query
builder add both tables. Click on the relationship line and select the
option for All records In StatesList and those that match in Customers.
You'll also need to group on the StatesList state name, not the state
in the customers file

I tried to do what you recommended. But it is still not returning the
results I want. Here is the sql statement that was generated

SELECT [StateList].state, Count(*) AS [Count Last Name]
FROM [StateList] LEFT JOIN customers ON [StateList].state =
customers.state
WHERE (((customers.la st_name)="Smith "))
GROUP BY [StateList].state;

Am I doing something wrong? Thanks
Nope. I gave a pointer but not the ideal. There are times when Count
won't work. In this case we were filtering on the lastname being Smith.
And that filtered most of your records out. What we needed to do was
sum...if it's Smith, add 1, else add 0.

SELECT StateList.State , Sum(IIf([LastName]="Smith",1,0 )) AS Expr1
FROM StateList LEFT JOIN Customers ON StateList.State = Customers.State
GROUP BY StateList.State ;

See if this works.

Yes. That did the trick. Thanks so much.

Oct 18 '06 #5

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

Similar topics

3
9328
by: Canes_Rock | last post by:
The information posted at: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=slrnarv28n.e4j.TuxTrax%40fortress.tuxnet&rnum=1&prev=/groups%3Fq%3Dsuppress%2Bcarriage%2Breturn%2Bgroup:comp.lang.python.*%26hl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.lang.python.*%26selm%3Dslrnarv28n.e4j.TuxTrax%2540fortress.tuxnet%26rnum%3D1 seemed to provide a solution to eliminating the line feed and causing a carriage return for the text displayed...
1
15647
by: Greg Smith | last post by:
I am trying to write a SQL query that will return a record count to my C# application. I wrote the following query: ALTER PROCEDURE up_justification_duplicate AS SELECT COUNT(*) FROM tblJustifications RETURN COUNT(*)
4
13013
by: OutdoorGuy | last post by:
Greetings, I am attempting to compile the code below, but I am receiving an error message when I do so. The error message is: "CSO161: 'Forloop.CalcAvg(int)': Not all code paths return a value". Any idea as to what I'm doing wrong? I'm sure it's something simple. Thanks in advance! public class ForLoop
11
4318
by: Joe HM | last post by:
Hello - I have the following very simple code ... Dim lStringA As String = "" Dim lStringB As String = "" .... lStringB = Replace(lStringA, "DUMMY", "", Compare:=CompareMethod.Text)
8
3382
by: Andrew Robinson | last post by:
Are these two equivalent? Is one better than the other? I tend to go with #1 but started wondering.... Thanks, 1: using (SqlConnection cn = new SqlConnection(DataConnection)) using (SqlCommand cm = new SqlCommand("ItemCount", cn)) { cm.CommandType = CommandType.StoredProcedure;
10
5621
by: Raj | last post by:
I need a VB function to return array of collections like Private Type Employee empname as string address as string salary as integer deptno as integer End Type dim employees() as Employee
3
10015
by: cmartin1986 | last post by:
I have written a sql query and I need it to return 0 when it doesn't find any matches to my criteria. I have tried adding iif statements, tried sum, and just Count, all of these methods work fine to return the values when it finds matches, but i need it also to return a 0 when there are no matches. Here is what I got. SELECT "CAL Recieved" as Tags, .,sum(iif(.,1,0)) AS FROM GROUP BY . HAVING (((.)=(Date()-1))); UNION ALL SELECT...
12
1863
by: subashsavji | last post by:
function CF_3Formula return Number is begin IF :DLY_TYPE='BEFORETIME' THEN SELECT COUNT(:DLY_TYPE)INTO :CP_1 FROM TEMP_CNS_999 WHERE :DLY_TYPE='BEFORETIME'; RETURN :CP_1; ELSIF :DLY_TYPE='DELAY' THEN SELECT COUNT(:DLY_TYPE)INTO :CP_2 FROM TEMP_CNS_999 WHERE :DLY_TYPE='DELAY'; RETURN :CP_2;
12
2318
by: subramanian100in | last post by:
Below is my understanding about count algorithms. Return type of count and count_if algorithms is iterator_traits<InputIterator>::difference_type. If the container contains more than 'difference_type' elements satisfying the condition, then count and count_if algorithm cannot return a value greater than 'difference_type'. As an example, suppose maximum value of 'difference_type' is INT_MAX.
16
2182
by: Stefano Sabatini | last post by:
Hi all, I'm facing this design problem. I have a table which defines the behaviour of an Object in a given state according to the type of event it is receiving. So for each couple event,state I want I set in the table a different handler.
0
10356
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...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
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
6874
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();...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
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
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
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.