Solved

I Would Like my Count Field Query to Return Zeroes for Dates With No Data Instead of Simply Not Listing That Date

Posted on 2011-09-30
20
186 Views
Last Modified: 2012-08-14
I seem to have a continuous problem with this. I have a table with ALL possible dates (They are actually all Week Ending Saturday Dates.)

I have another table with actual data and the Week Ending Saturday Date associated with that piece of data.

My query is a joining query that uses ALL of the possible dates. However it does not seem to be doing that. There are weeks missing. I have my SQL below
SELECT Count(NZ(B_ClosedByWeek_CARs_Tbl.CAR_Num_Full,0)) AS Closed, CAR_Possible_WEDATES_W_Leaps_tbl.PossibleWeekEndingDates AS Week INTO C3_ClosedbyWeek_KOJ_Time_Line_Count_tbl
FROM B_ClosedByWeek_CARs_Tbl RIGHT JOIN CAR_Possible_WEDATES_W_Leaps_tbl ON B_ClosedByWeek_CARs_Tbl.WEDate = CAR_Possible_WEDATES_W_Leaps_tbl.PossibleWeekEndingDates
WHERE (((B_ClosedByWeek_CARs_Tbl.Problem_Location)="KOJ Cherry St" Or (B_ClosedByWeek_CARs_Tbl.Problem_Location)="KOJ Panel Plant" Or (B_ClosedByWeek_CARs_Tbl.Problem_Location)="KOJ Wood Plant") AND ((B_ClosedByWeek_CARs_Tbl.WEDate)<Now() And (B_ClosedByWeek_CARs_Tbl.WEDate)>#5/31/2010#))
GROUP BY CAR_Possible_WEDATES_W_Leaps_tbl.PossibleWeekEndingDates
ORDER BY CAR_Possible_WEDATES_W_Leaps_tbl.PossibleWeekEndingDates;

Open in new window

0
Comment
Question by:Rex85
  • 10
  • 9
20 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 36891888
Try changing your criteria to include NULL values for WEDate.

With your right join, these will be NULL for dates that are not present.

I think this will do it  for your WHERE clause:

WHERE (((B_ClosedByWeek_CARs_Tbl.Problem_Location)="KOJ Cherry St" Or (B_ClosedByWeek_CARs_Tbl.Problem_Location)="KOJ Panel Plant" Or (B_ClosedByWeek_CARs_Tbl.Problem_Location)="KOJ Wood Plant") AND (((B_ClosedByWeek_CARs_Tbl.WEDate)<Now() And (B_ClosedByWeek_CARs_Tbl.WEDate)>#5/31/2010#) OR B_ClosedByWeek_CARs_Tbl.WEDate IS NULL))

Open in new window

0
 

Author Comment

by:Rex85
ID: 36891921
No. That didn't workl. It's Still skipping some weeks. I thought a Join with a list of possible dates would return nulls for those weeks for which no data existed...not just skip them.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36891960
It should... I believe it is your criteria that is preventing that from happening.

Can you post a sample?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:Rex85
ID: 36891964
Sorry. I should have included this. This is the query output. It is clearly skipping 8/14/2010.
Skipped-Date-1.jpg
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36891989
Understood - If you are able to post an actual sample DB, it might be easier to work with.
0
 

Author Comment

by:Rex85
ID: 36892002
These are the two tables. Sorry if cross posted.
data-Table-1.jpg
Possible-Date-Table-1.jpg
0
 

Author Comment

by:Rex85
ID: 36892038
I can't really post the db. These tables are made by make table queries that query Linked tables that are part of a very large db.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36892048
Sorry - I think I'm not being clear... I understand the problem you are having with the missing weeks.

Are you able to post an actual sample database (not just screenshots)?  
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36892067
Ah- okay...

I'll try to work with this as-is later on if no one else picks it up.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36892084
For now, run this test...

Take out the date criteria altogether (I realize this will not yield the correct results).


Do the missing weeks show up with the date criteria gone?
0
 

Author Comment

by:Rex85
ID: 36892153
Yes. You are correct. It is the WHERE clause. I'm not sure how, or if, I can get there from here. I may need to split the data table into a separate table for each location. As soon as I stipulate the location inclusion in the WHERE, it skips the blanks because they aren't associated with the blank dates....because they're blank.
blank-dates-no-criteria.jpg
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36892185
Give this a try...

Instead of using the WEDate from the table that has missing weeks, use the PossiblWeekEndingDate from the table with all of the dates as your criteria.  Like this:

((CAR_Possible_WEDATES_W_Leaps_tbl.PossibleWeekEndingDates)<Now() And (CAR_Possible_WEDATES_W_Leaps_tbl.PossibleWeekEndingDates)>#5/31/2010#))
0
 
LVL 18

Expert Comment

by:lludden
ID: 36892874
Try this:

SELECT      Count(NZ(B_ClosedByWeek_CARs_Tbl.CAR_Num_Full,0)) AS Closed,
            CAR_Possible_WEDATES_W_Leaps_tbl.PossibleWeekEndingDates AS Week
INTO C3_ClosedbyWeek_KOJ_Time_Line_Count_tbl
FROM CAR_Possible_WEDATES_W_Leaps_tbl
      LEFT OUTER JOIN B_ClosedByWeek_CARs_Tbl ON B_ClosedByWeek_CARs_Tbl.WEDate = CAR_Possible_WEDATES_W_Leaps_tbl.PossibleWeekEndingDates
WHERE (B_ClosedByWeek_CARs_Tbl.Problem_Location="KOJ Cherry St"
      Or B_ClosedByWeek_CARs_Tbl.Problem_Location="KOJ Panel Plant"
      Or B_ClosedByWeek_CARs_Tbl.Problem_Location="KOJ Wood Plant")
      AND B_ClosedByWeek_CARs_Tbl.WEDate BETWEEN #5/31/2010# AND Now()
GROUP BY CAR_Possible_WEDATES_W_Leaps_tbl.PossibleWeekEndingDates
ORDER BY CAR_Possible_WEDATES_W_Leaps_tbl.PossibleWeekEndingDates

0
 

Author Comment

by:Rex85
ID: 36893154
lludden:

Thank you, but that still skips the weeks that are nulls. (See Below)

I tried a double join, joining the data with both the possible dates and the possible locations, but Access couldn't handle it. It gave me the "ambiguous join" message that I needed to use a subquery. I have yet to successfully put one of those together.
Skipped-Dates-2.jpg
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36893276
Did you try modifying the where clause to the SQL in my last post?
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36893288
Just that part of the where clause,  nothing else.
0
 

Author Comment

by:Rex85
ID: 36893348
mbizup:

Thank you. Yes. I did. It did not make a difference. When I removed the plant location, it showed the problem.

The data is in the form of  Plant  Date CAR_Number

When I join that data with the Possible Dates, but don't restrict the Plant, I get the nulls. That is what I want. However, since it only returns one null for a given possible date, and that is not associated with a plant location, by restricting location in the WHERE clause, I filter out the nulls.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 36896492
It's taking me a bit to get my brain wrapped around your last comment, trying to visualize what you are describing.  It's one of the more challenging questions I've seen posted here in a while :)

If I'm understanding correcly, the 'missing' records you want have nothing in the Plant field and nothing in the date field, beause there is no data from your main table corresponding to the right-joined data in your "all dates" table.

You might be able to resolve this by revising your plant criteria to allow NULL plants, or maybe a combination of Null Dates and Null plants

Like this:

WHERE ((B_ClosedByWeek_CARs_Tbl.Problem_Location="KOJ Cherry St" Or B_ClosedByWeek_CARs_Tbl.Problem_Location="KOJ Panel Plant" Or B_ClosedByWeek_CARs_Tbl.Problem_Location="KOJ Wood Plant" OR B_ClosedByWeek_CARs_Tbl.Problem_Location IS NULL) etc...


Or This:

WHERE ((B_ClosedByWeek_CARs_Tbl.Problem_Location="KOJ Cherry St" Or B_ClosedByWeek_CARs_Tbl.Problem_Location="KOJ Panel Plant" Or B_ClosedByWeek_CARs_Tbl.Problem_Location="KOJ Wood Plant" OR (B_ClosedByWeek_CARs_Tbl.Problem_Location IS NULL AND B_ClosedByWeek_CARs_Tbl.WEDate IS NULL)) etc...


0
 

Author Comment

by:Rex85
ID: 36903247
mbizup: I believe your assessment is correct. I can find dates with no data (nulls) by joining the dates with data table to the possible dates table, but that returns one null per date with no data. The problem is that there are four locations, so I need to do that for each locations, essentially asking a query to tell me what data is NOT there for four different locations at each of the possible dates.

If three plants had no data for a date, but one did, I would need the one plant's data for that date and three nulls for that date, one for each separate location that had no data. It's probably possible, but I am working right now on breaking it out....doing it with a query by plant, rather than one very clever query.

I will try what you have suggested though. I would much rather reduce my queries by 75%.
0
 

Author Closing Comment

by:Rex85
ID: 36932598
Thank you for your help. I really appreciate it.

Rex
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question