问题
I have data in Access Database which contains data for multiple days. But it sometime have missing data for some dates.
In example, I have data for
myDate Location Price
11/1/2013 South 10
11/1/2013 West 20
11/1/2013 East 10
11/2/2013 South 10
11/2/2013 West 20
11/2/2013 East 10
11/4/2013 South 10 <---- 11/3/2013 Data Missing
11/4/2013 West 30
11/4/2013 East 10
The way I tried to solve it was to find missing date in Access Database, and filled it with Null value using calender table.
myDate Location Price
11/1/2013 South 10
11/1/2013 West 20
11/1/2013 East 10
11/2/2013 South 10
11/2/2013 West 20
11/2/2013 East 10
11/3/2013 <---- Null values
11/4/2013 South 10
11/4/2013 West 30
11/4/2013 East 10
But when I use crosstab query to get get price for each location, 11/3/2013 is skipped because it doesn't have any location related to it.
myDate South West East
11/1/2013 10 20 10
11/2/2013 10 20 10 <---- 11/3/2013 Data skipped
11/4/2013 10 30 10
What I want my solution to be is following:
myDate South West East
11/1/2013 10 20 10
11/2/2013 10 20 10
11/3/2013
11/4/2013 10 30 10
I thought I would be able to use NZ function when I'm calling this data from Access, and whenever I find null value, I can just put #N/A error in Excel spreadsheet so I would be able to catch what data is missing and fill it in later.
How would I accomplish this? Am I doing the right way or is there better way?
Any help or hint would be greatly appreciated.
回答1:
Here's the way I would do it.
Don't include empty dates in the data table. Go back to the way you had it before, where the table only includes valid data.
Create a crosstab query on the table. This will not include the dates that have no data. (From your example:)
myDate South West East
11/1/2013 10 20 10
11/2/2013 10 20 10 <---- 11/3/2013 Data skipped
11/4/2013 10 30 10
Save the crosstab query in your Access database. I'll call it qryXtab. Create a new query left joining your calendar table to qryXtab.
SELECT tblCalendar.cdate, South, West, East
FROM tblCalendar
LEFT JOIN qryXtab
ON tblCalendar.cdate = qryXtab.myDate
ORDER BY tblCalendar.cdate
Use this new query to populate your Excel file. It will include blank rows for dates that have no data.
来源:https://stackoverflow.com/questions/20862358/crosstab-query-getting-null-data-for-missing-data-from-access-db