473,400 Members | 2,145 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,400 software developers and data experts.

Crosstab Count with Empty Columns

Boxcar74
Ok I have another fun (sarcasm) Question I hope someone can help me with.

In a nutshell I have a Crosstab query using the COUNT function with Dates as the column heading and different categories as the rows and I can’t retrieve a full record set because some Days have no records.

My problem is that is some cases for a given day(s) that has NO data, (no records at all for the given criteria). I miss the day or day’s in my output.

So for reporting purposes (in Excel) I have to look at all the days (sometimes YTD) and add the Date and a zero value for that day.

So for example I get.
Category 6/1 6/3 6/4 6/6
Printer 3 2 3 8

I retrieve no count (hence no column) for June 2 and June 5

What I want is:

Category 6/1 6/2 6/3 6/4 6/5 6/6
Printer 3 0 2 3 0 8

Here is my SQL Code:

TRANSFORM IIf(Sum([Count]) Is Null,0,Sum([Count])) AS Expr1
SELECT [Daily]. CATEGORY
FROM [Daily]
WHERE ((([Daily].CATEGORY)="HARDWARE") AND (([Daily].DATE) Between Date()-13 And Date()-1))
GROUP BY [Daily RADAR_STORE].CATEGORY
PIVOT [Daily].DATE;
----------------------------------------------------------------------

I tried the IIF(ISNULL.. and NZ function but no luck.

Please help!!! Any suggestions would be appreciated!

Thanks,
Boxcar
Jun 19 '07 #1
14 4844
As you dont actually say how the data gets in to the table its a little dificult to sort. However, one way is to allocate a defaut vaule to the record of ZERO that way you will always have daya to account for AND you can clearly identify NIL sales/usages

Gareth
Jun 19 '07 #2
MMcCarthy
14,534 Expert Mod 8TB
Unfortunately the only way to do this would be to use an IN statement after the PIVOT to allow for all dates. I would guess that this would be unmanagable.

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM IIf(Sum([Count]) Is Null,0,Sum([Count])) AS Expr1
  2. SELECT [Daily]. CATEGORY
  3. FROM [Daily]
  4. WHERE ((([Daily].CATEGORY)="HARDWARE") AND (([Daily].DATE) Between Date()-13 And Date()-1))
  5. GROUP BY [Daily RADAR_STORE].CATEGORY
  6. PIVOT [Daily].DATE IN ("6/1", "6/2", "6/3", "6/4", "6/5");
  7.  
Jun 20 '07 #3
Yeah it looks that way :(

I tired joining a table with every date of the year.

But still no luck.

In regards to the table I am doing a COUNT of any ID number (Primary Key) and how many time it occurs on a day.

So simply put the TABLE Columns would be:
INSTANCE_ID / DATE / CATEGORY / More data etc.....

I know someone that says he has done it some way.
When I find out I'll post it. (If he can do it)

I’m pretty sure mmccarthy is right on this one.

As always thanks for the input!!
Jun 20 '07 #4
MMcCarthy
14,534 Expert Mod 8TB
If you have a table containing every day of the year you could try the following (assuming table name as tblDates)

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM IIf(Sum([Count]) Is Null,0,Sum([Count])) AS Expr1
  2. SELECT [Daily]. CATEGORY
  3. FROM tblDates LEFT JOIN [Daily] 
  4. ON tblDates.[Date] = [Daily].[DATE]
  5. WHERE ((([Daily].CATEGORY)="HARDWARE") AND ((tblDates.[DATE]) Between Date()-13 And Date()-1))
  6. GROUP BY [Daily RADAR_STORE].CATEGORY
  7. PIVOT tblDates.[Date];
  8.  
This might work
Jun 20 '07 #5
That didn't work either. I appreciate the effort though.

I set up a small test DB and even renamed the tables and field to match the query.

One thing that could have thrown you off is I posted the first SQL line incorrectly.

It should have been:
TRANSFORM Count(Daily.[ID]) AS COUNT

So this is how I tried it:
TRANSFORM IIf(Count([ID]) Is Null,0,Count([ID])) AS Expr1
SELECT Daily.Category
FROM tblDates LEFT JOIN Daily ON tblDates.Date = Daily.Date
WHERE (((Daily.Category)="HARDWARE"))
GROUP BY Daily.Category, Daily.Date
PIVOT tblDates.Date;

So I’m counting the number of times the ID happen a given day. If nothing happens that day I don’t get a column heading.

As I mention someone I know said he can do it. He said it was too hard to explain over the phone. I’m meeting with him next week.

If I get an answer, I’ll post it.

Thanks Again

-- Boxcar
Jun 21 '07 #6
MMcCarthy
14,534 Expert Mod 8TB
That didn't work either. I appreciate the effort though.

I set up a small test DB and even renamed the tables and field to match the query.

One thing that could have thrown you off is I posted the first SQL line incorrectly.

It should have been:
TRANSFORM Count(Daily.[ID]) AS COUNT

So this is how I tried it:
TRANSFORM IIf(Count([ID]) Is Null,0,Count([ID])) AS Expr1
SELECT Daily.Category
FROM tblDates LEFT JOIN Daily ON tblDates.Date = Daily.Date
WHERE (((Daily.Category)="HARDWARE"))
GROUP BY Daily.Category, Daily.Date
PIVOT tblDates.Date;

So I’m counting the number of times the ID happen a given day. If nothing happens that day I don’t get a column heading.

As I mention someone I know said he can do it. He said it was too hard to explain over the phone. I’m meeting with him next week.

If I get an answer, I’ll post it.

Thanks Again

-- Boxcar
Thanks Boxcar, that would be appreciated.
Jun 21 '07 #7
OK, the answer I got is using two queries.
A UNION Subquery then a Crosstab Query !!!

So to review for this example: I’m running a crosstab query with dates as my columns heading, but if nothing happens (no record) on that day I end up with no column for that day. (see previous post for examples)

Here my setup. I tried to simplified it.

One table with all my data called DAILY and another with every day of the year call DAYS. In this query I’m looking for a count of records that that category equals Hardware; based on ID# for the last 13 days.

SQL for UNION: (I named it "Counts_For_Crosstabs_13_Days")

SELECT [DAILY].Date, [DAILY].Category, Count([DAILY].ID) AS CountOfID
FROM [DAILY]
GROUP BY [DAILY].Date, [DAILY].Category

HAVING ((([DAILY].Date) Between Date() -13 AND Date() -1)) AND [DAILY].Category = “Hardware”

UNION SELECT Days.Day, Null AS Cat, Null AS Tot
FROM Days
WHERE (((Days.Day) Between Date()-13 And Date() -1));

This query essentially adds a Null record for any day in the last 13 days.

NOTE: on the (UNION SELECT Days.Day, Null AS Cat, Null AS Tot) the NULL AS … is so the table has the same number of columns.

Then I run a Crosstab Query: (this one sums the counts)
TRANSFORM Sum(Counts_For_Crosstabs_13_Days.CountOfID) AS SumOfCountOfID
SELECT Counts_For_Crosstabs_13_Days.Category
FROM Counts_For_Crosstabs_13_Days
GROUP BY Counts_For_Crosstabs_13_Days.Category
PIVOT Format([Date],"Short Date");
(Sorry my queries don't look nice like mmccarthy's)

Now if I run the second query I get every day for the last 13 days no matter if there are record for that day.

My solution is very convoluted. But is works for me.

I hope this makes sense and I hope it works for anybody out there!!!

-- Boxcar
Jul 3 '07 #8
One Last note:

Other options that were suggested to me:
1. Combining the two queries, where the Union query will function as a Crosstab query.

2. Use a VBA module: How to Count Values in Record or Recordset Across Fields
http://support.microsoft.com/kb/q142227/

I never got either of these to work, but give it a try.

As always thanks to all the Scripts folks that helped.

Good Luck!!!

-- Boxcar
Jul 3 '07 #9
MMcCarthy
14,534 Expert Mod 8TB
Thanks Boxcar. The union query is a nice solution as long as you have a table for all the relevant days. Is your days table a full list of all 365 days of the year?
Jul 3 '07 #10
Yes and Then some from 1/1/2007 to 12/31/2015 with everyday in between, accounting for leap year and all.

I know the db will be long gone or in a different format by then but I just fill the table when I created it. :-)

I found in the if in the first query I put: HAVING Between #1/1/2007# and Date()-1

I can use the Second (Crosstab) query to adjust the date parameters i.e. Between Date()-13 and Date()-1

But this does not work with the Category. If I specify category in the Crosstab query I lose the columns with no records.

There has to be a way to combine them (The Union and Crosstab that is).

Above my head at this point and I have it set up much more complex than I displayed. So I'm sticking with what work for now.

Thanks!!

---Boxcar
Jul 3 '07 #11
MMcCarthy
14,534 Expert Mod 8TB
Yes and Then some from 1/1/2007 to 12/31/2015.

I know the db will be long gone by then but I just fill the table when i created it.
:)
Did you allow for the leap years?
Jul 3 '07 #12
YES I just edited the post above!
Jul 3 '07 #13
MMcCarthy
14,534 Expert Mod 8TB
See what this does ...

Expand|Select|Wrap|Line Numbers
  1. TRANSFORM Count([DAILY].ID) AS CountOfID
  2. SELECT [DAILY].Category, 
  3. FROM Days LEFT JOIN [DAILY]
  4. ON Days.Day = [DAILY].Date
  5. GROUP BY [DAILY].Date, [DAILY].Category
  6. WHERE (((Days.Day) Between Date()-13 And Date() -1)) 
  7. AND [DAILY].Category = “Hardware”
  8. PIVOT Days.Day;
  9.  
Jul 3 '07 #14
I tired one like that before. With no luck.

I tried again with your format still no luck. When I get more time I will play with it more.

In therory it should work.

Thanks Again !!

-- Boxcar
Jul 9 '07 #15

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

Similar topics

8
by: Donna Sabol | last post by:
First, I should start by saying I am creating a database to be used by some very impatient, non-computer literate people. It needs to be seameless in it's operation from their point of view. I...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
1
by: Richard Hollenbeck | last post by:
Hello Newsgroup. You have all been very helpful in the past and I thank you. I try to ask relevant questions so that they don't just benefit me, but also benefit the group. I'm currently...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
2
by: scott.k.fraley | last post by:
....and the SELECT thats trying to pull from said Query doesn't like it one bit! ;) I'm working on this project (in Access 2002) and there is a report who's RecordSource is the following...
8
by: Penny | last post by:
(Access 2003 Multiuser Split DB, Windows XP Pro) Hi All, I would really appreciate just some basic tips on how to make a Crosstab Form based on a Crosstab Query. The query always has the same...
2
by: jason.teen | last post by:
Hi All, I am having trouble creating a crosstab query. In my original data I have two columns, One called "Categorized" and one called "Mapped' in which those columns can hold values of "true"...
13
by: salad | last post by:
Operating in A97. I didn't receive much of a response conserning Pivot tables in Access. Pivot tables are nice, but a CrossTab will work for me too. Using a Pivot table, one is actually...
4
by: mattlightbourn | last post by:
Hi all, I have a problem which has been driving me nuts. Crosstab queries! I have a database witch a few different tables to do with garment manufacturing. I have a table for a client...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
0
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...
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...
0
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,...
0
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...

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.