473,385 Members | 1,872 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,385 software developers and data experts.

Basic SQL question...

Hello, folks.

I have the following query that I run that pulls up the count of the number of orders of a product.

SELECT Product, Count(Product) AS Total
FROM Orders_View
GROUP BY Product, Year([DateOpened]), Month([DateOpened])
HAVING (((Year([DateOpened]))=2004) AND ((Month([DateOpened]))=10))
ORDER BY Product;

This query is fine but the COUNT() function only coughs-up the non-zero results. How can I get this query to cough-up the zero counts as well for all products listed in the Product table?

TIA...
Jul 23 '05 #1
6 1287
Count(Product) will count non-null columns. To count null columns
either use ISNULL function or count(*).

SELECT Product, Count(isnull(Product,0)) AS Total
FROM Orders_View
GROUP BY Product, Year([DateOpened]), Month([DateOpened])
HAVING (((Year([DateOpened]))=2004) AND ((Month([DateOpened]))=10))
ORDER BY Product;

SELECT Product, Count(*) AS Total
FROM Orders_View
GROUP BY Product, Year([DateOpened]), Month([DateOpened])
HAVING (((Year([DateOpened]))=2004) AND ((Month([DateOpened]))=10))
ORDER BY Product;

Jul 23 '05 #2

Blue Streak wrote:
Hello, folks.

I have the following query that I run that pulls up the count of the number of orders of a product.
SELECT Product, Count(Product) AS Total
FROM Orders_View
GROUP BY Product, Year([DateOpened]), Month([DateOpened])
HAVING (((Year([DateOpened]))=2004) AND ((Month([DateOpened]))=10))
ORDER BY Product;

This query is fine but the COUNT() function only coughs-up the non-zero results. How can I get this query to cough-up the zero counts
as well for all products listed in the Product table?
TIA...


SELECT Product,
(SELECT COUNT(*)
FROM Orders_View
WHERE Orders_View.Product = Product.Product
AND Year([DateOpened])=2004
AND Month([DateOpened])=10) AS ProductSales
FROM Product

--
David Rowland
http://dbmonitor.tripod.com

Jul 23 '05 #3
Blue Streak (an*******@msn.com) writes:
I have the following query that I run that pulls up the count of the
number of orders of a product.

SELECT Product, Count(Product) AS Total
FROM Orders_View
GROUP BY Product, Year([DateOpened]), Month([DateOpened])
HAVING (((Year([DateOpened]))=2004) AND ((Month([DateOpened]))=10))
ORDER BY Product;

This query is fine but the COUNT() function only coughs-up the non-zero
results. How can I get this query to cough-up the zero counts as well
for all products listed in the Product table?


It is always adviceable for this type of question to post:

1) CREATE TABLE statement for your tables (and in this case also the view)
2) INSERT statements with sample data.
3) The desired output given the sample data.

That permits anyone answering your question to easily cut and paste into
to Query Analyzer and develop a tested solution.

The solution suggested by David Rowland should give you the desired
result, but it may not be that performant.

An alternative is to explorr GROUP BY ALL, but with out the table and
view definitions, I can't test.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4
You might want work on better names for data elements. A name should
tell us what the data element is, and not how it is stored (i.e. VIEW);
it ought to tell us the attribute involved (product what? Weight? Size?
Id?)

Since you did not bother with DDL, I am going to assume that there is
Products table somewhere and that this will work.

SELECT P.product_id, COUNT(O.product_id) AS total
FROM Products AS P
LEFT OUTER JOIN
Orders AS O
ON P.product_id = O.product_id
WHERE O.date_opened BETWEEN '2004-10-01 00:00:00.000'
AND '2004-10-31 23:59:59.99'
GROUP BY I.product_id;

Hint about temporal data: do not split it up into pieces to do numeric
or string operations on the conversions. Think of it as a data type in
its own right, with its own operators. This is not just a matter of the
extra overhead, but the way you approach a problem.

Jul 23 '05 #5
10Q very much!

"louis" <lo************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Count(Product) will count non-null columns. To count null columns
either use ISNULL function or count(*).

SELECT Product, Count(isnull(Product,0)) AS Total
FROM Orders_View
GROUP BY Product, Year([DateOpened]), Month([DateOpened])
HAVING (((Year([DateOpened]))=2004) AND ((Month([DateOpened]))=10))
ORDER BY Product;

SELECT Product, Count(*) AS Total
FROM Orders_View
GROUP BY Product, Year([DateOpened]), Month([DateOpened])
HAVING (((Year([DateOpened]))=2004) AND ((Month([DateOpened]))=10))
ORDER BY Product;

Jul 23 '05 #6
Mercy Buckets!!!!

This one worked!
"dbmonitor" <db***************@hotmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...

Blue Streak wrote:
Hello, folks.

I have the following query that I run that pulls up the count of the

number of orders of a product.

SELECT Product, Count(Product) AS Total
FROM Orders_View
GROUP BY Product, Year([DateOpened]), Month([DateOpened])
HAVING (((Year([DateOpened]))=2004) AND ((Month([DateOpened]))=10))
ORDER BY Product;

This query is fine but the COUNT() function only coughs-up the

non-zero results. How can I get this query to cough-up the zero counts
as well for all products listed in the Product table?

TIA...


SELECT Product,
(SELECT COUNT(*)
FROM Orders_View
WHERE Orders_View.Product = Product.Product
AND Year([DateOpened])=2004
AND Month([DateOpened])=10) AS ProductSales
FROM Product

--
David Rowland
http://dbmonitor.tripod.com

Jul 23 '05 #7

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

Similar topics

6
by: pauldepstein | last post by:
I am reading Grimshaw and Ortega's "C++ and Numerical Methods." They construct a vector class which contains the variable vec, a float* variable where the length of the array (number of...
6
by: DH | last post by:
I have a VERY basic question about figuring database size. I've inherited a database which is generally similar to this basic one: Item, Red, Blue, Green, Yellow (text), (int),(int),(int),(int)...
9
by: Malcolm | last post by:
After some days' hard work I am now the proud possessor of an ANSI C BASIC interpreter. The question is, how is it most useful? At the moment I have a function int basic(const char *script,...
4
by: Ramesh | last post by:
hi, Let me ask some basic questions. Can anybody explain me about the following questions: 1. When we have to create sn key? Whenever we compiled Component we have to create or it is a one time...
13
by: Pete | last post by:
I'm cross posting from mscom.webservices.general as I have received no answer there: There has been a number of recent posts requesting how to satisfactorily enable BASIC authorization at the...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
1
by: frankhanretty | last post by:
Do I have to install Visual basic on the remote terminals as I did on the server? I have an visual basic 5 application running fine on my client's server and he is now networked. He wants to run the...
4
by: Chris Asaipillai | last post by:
Hi there My compay has a number of Visual Basic 6 applications which are front endeed onto either SQL Server or Microsoft Access databases. Now we are in process of planning to re-write these...
3
by: Scott Stark | last post by:
Hello, I'm trying to get a better handle on OOP programming principles in VB.NET. Forgive me if this question is sort of basic, but here's what I want to do. I have a collection of Employee...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
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,...

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.