473,396 Members | 1,768 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.

Sql Server Query - won't display zero returns

Hello,

I'm writing a basic query in Sql Server Mgmt Studio 2005. Basically, I'm trying to query a table to get all "issues" reported in a month by "Project Category". The query is working fine except that I cannot get it to display entries for projects that have zero issues for that month - so it comes out blank. I'm pretty sure I need a left join (or something like that) for this, but no matter what I try, those zero entries won't display. Here is the query

Expand|Select|Wrap|Line Numbers
  1.      DECLARE @StartDate datetime, @EndDate datetime,  @ProjectID     int
  2.         DECLARE c1 CURSOR FOR
  3.         SELECT Pj_ID
  4.         FROM dbo.projects
  5.  
  6.         OPEN c1
  7.  
  8.         FETCH NEXT FROM c1
  9.         INTO @ProjectID
  10.  
  11.         WHILE @@FETCH_STATUS = 0
  12.         BEGIN
  13.  
  14.         Select  bg.bg_project as PROJECT, pr.pj_name as CATEGORY,  count (bg.bg_project) as TOTAL  
  15.         from projects pr left outer join  bugs bg 
  16.         on pr.pj_id =bg.bg_project 
  17.         where bg.bg_project = @ProjectID and
  18.         ((bg_reported_date > '08/01/2007') AND (bg_reported_date < '09/01/07')) 
  19.         group by bg.bg_project, pr.pj_name
  20.         FETCH NEXT FROM c1
  21.         INTO @ProjectID
  22.  
  23.         END
  24.         CLOSE c1
  25.         DEALLOCATE c1
  26.  
The results look something like this when imported into Excel:

PROJECT CATEGORY TOTAL
----------- --------------------------------------------------------------------------------
22 EDI 9

PROJECT CATEGORY TOTAL
----------- --------------------------------------------------------------------------------

Thank you in advance for your time!

JCC
Sep 26 '07 #1
3 2000
debasisdas
8,127 Expert 4TB
Please post all your involved tables structures.
Sep 27 '07 #2
azimmer
200 Expert 100+
Hello,

I'm writing a basic query in Sql Server Mgmt Studio 2005. Basically, I'm trying to query a table to get all "issues" reported in a month by "Project Category". The query is working fine except that I cannot get it to display entries for projects that have zero issues for that month - so it comes out blank. I'm pretty sure I need a left join (or something like that) for this, but no matter what I try, those zero entries won't display. Here is the query

Expand|Select|Wrap|Line Numbers
  1.      DECLARE @StartDate datetime, @EndDate datetime,  @ProjectID     int
  2.         DECLARE c1 CURSOR FOR
  3.         SELECT Pj_ID
  4.         FROM dbo.projects
  5.  
  6.         OPEN c1
  7.  
  8.         FETCH NEXT FROM c1
  9.         INTO @ProjectID
  10.  
  11.         WHILE @@FETCH_STATUS = 0
  12.         BEGIN
  13.  
  14.         Select  bg.bg_project as PROJECT, pr.pj_name as CATEGORY,  count (bg.bg_project) as TOTAL  
  15.         from projects pr left outer join  bugs bg 
  16.         on pr.pj_id =bg.bg_project 
  17.         where bg.bg_project = @ProjectID and
  18.         ((bg_reported_date > '08/01/2007') AND (bg_reported_date < '09/01/07')) 
  19.         group by bg.bg_project, pr.pj_name
  20.         FETCH NEXT FROM c1
  21.         INTO @ProjectID
  22.  
  23.         END
  24.         CLOSE c1
  25.         DEALLOCATE c1
  26.  
The results look something like this when imported into Excel:

PROJECT CATEGORY TOTAL
----------- --------------------------------------------------------------------------------
22 EDI 9

PROJECT CATEGORY TOTAL
----------- --------------------------------------------------------------------------------

Thank you in advance for your time!

JCC
I guess your problem lies in the WHERE clause:
Expand|Select|Wrap|Line Numbers
  1. where bg.bg_project = @ProjectID and
  2.         ((bg_reported_date > '08/01/2007') AND (bg_reported_date < '09/01/07')) 
  3.  
These conditions can only be met if there's corresponding data in the bugs (bg) table; in case of the left outer join these fields can end up having NULL in them. I suggest you change it to
Expand|Select|Wrap|Line Numbers
  1. where pr.pj_id = @ProjectID and
  2.         (((bg_reported_date > '08/01/2007') AND (bg_reported_date < '09/01/07')) OR (bg_reported_date is null))
  3.  
or something like this.
Note: you may want to rephrase the selected columns as well along these lines.
Sep 27 '07 #3
Thank you so much for your help. With a little tweaking, this did the trick!

I guess your problem lies in the WHERE clause:
Expand|Select|Wrap|Line Numbers
  1. where bg.bg_project = @ProjectID and
  2.         ((bg_reported_date > '08/01/2007') AND (bg_reported_date < '09/01/07')) 
  3.  
These conditions can only be met if there's corresponding data in the bugs (bg) table; in case of the left outer join these fields can end up having NULL in them. I suggest you change it to
Expand|Select|Wrap|Line Numbers
  1. where pr.pj_id = @ProjectID and
  2.         (((bg_reported_date > '08/01/2007') AND (bg_reported_date < '09/01/07')) OR (bg_reported_date is null))
  3.  
or something like this.
Note: you may want to rephrase the selected columns as well along these lines.
Sep 27 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Mike Cocker | last post by:
Hello, I'm quite weak at PHP, so I was hoping to get some help understanding the below code. First off, I'm trying to create a "query form" that will allow me to display the results on my...
4
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to...
1
by: Yisroel Markov | last post by:
Greetings, In Access 97 I have a subreport with two labels and a control. The record source for the control is a simple SQL query referring to another query: SELECT DISTINCTROW...
26
by: David W. Fenton | last post by:
A client is panicking about their large Access application, which has been running smoothly with 100s of thousands of records for quite some time. They have a big project in the next year that will...
6
by: Martin Lacoste | last post by:
Ok, before I headbutt the computer... don't know why when I add criteria in a query, I get an 'invalid procedure call'. I also don't know why after searching the help in access, the various access...
15
by: Richard Hollenbeck | last post by:
I tried to ask this question before on the 14th of January but I never got a reply. I'm still struggling with the problem. I'll try to rephrase the question: I have a crosstab query with rows...
12
by: strict9 | last post by:
Hello all, I'm writing several queries which need to do various string formating, including changing a phone number from (123) 456-7890. After some problem with data mismatches, I finally got it...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
3
by: 0to60 | last post by:
Please help! I'm using the following code to get an XML doc: string str = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=12345&city=addison"; System.Net.HttpWebRequest request =...
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
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: 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
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,...
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,...
0
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...

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.