Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding missing dates to a range

Status
Not open for further replies.

rbh123456789

Technical User
Mar 3, 2010
80
0
0
CA
CR11.5

I have a View that looks like this:

User | eventtype | date
user1 | sickday | Dec 1 2011
user1 | meeting | Dec 1 2011
user2 | vacation | Dec 3 2011
user3 | |
user4 | sickday | Dec 1 2011

this report is supposed to:
-list each user
-display the event
-display the day of the event

i am using a crosstab, and have the rows display the user.
columns will be the dates
this report is supposed to show a 5 day date range (which they select using parameters)

Ideally, if they choose Dec 1 2011 to Dec 3, 2011 i would like to see 3 columns in my crosstab (Dec 1, 2, 3).

but, since i don't have a Dec 2 date, the report just shows Dec 1 and 3.

is there a way to show the Dec2 column, even though its not in the date field of the view?
 



What a waste, to see displayed a column or row with absolutely no data!!!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
its to show who does NOT have any events booked for that specific day.
 
I would create a temp table with the dates , cross join it with the users table and left join it with your current view. This will return all possible combinations for date-user and the available events for each combination and will guarantee you that you will have rows for all users and columns for all dates.
You are aware that you cannot show the actual event name in the crosstab value section right ?

Viewer, scheduler and manager for Crystal reports.
Send you report everywhere.
 
thanks, i can try that.

if i use the Mode function to evaluate the field, i believe it will show the eventtype; but i might have a problem if there is more than one event per day..
 
You have to decide about the value section.

About the dates temp table .... I have a function which will return a table with the dates in a period:

---------------------------------------------
CREATE FUNCTION [dbo].[DatesTable]
(
@FromDate DATE,
@ToDate DATE
)
RETURNS @datetable TABLE ([Date] DATE)
AS
BEGIN

WITH DatesTable(CurrentDate) AS
(
SELECT @FromDate AS datetime
UNION ALL
SELECT DATEADD(DAY, 1, CurrentDate) FROM DatesTable WHERE CurrentDate < @ToDate
)

INSERT INTO @datetable ([Date])
SELECT CurrentDate FROM DatesTable OPTION (MAXRECURSION 0)

RETURN;
END
---------------------------------------------

it cann be called in this way:

SELECT * FROM [dbo].[DatesTable]('12/1/2011','12/4/2011')



Here is the script that can demontrate the use of the function in your scenario :


CREATE TABLE #Users (UserID INT, UserName VARCHAR(10))
CREATE TABLE #UsersEvents (UserID INT, EventID INT,EventDate DATETIME)

INSERT INTO #Users (UserID,UserName) VALUES (1,'User1'), (2,'User2')
INSERT INTO #UsersEvents (UserID,EventID, EventDate) VALUES (1,1,'12/1/2011'), (1,3,'12/2/2011'),(2,1,'12/2/2011'), (2,2,'12/1/2011'),(2,4,'12/4/2011'), (1,4,'12/4/2011')

SELECT * FROM #UsersEvents
SELECT * FROM #Users

SELECT d.[Date],u.UserName,ue.EventID
FROM [dbo].[DatesTable]('12/1/2011','12/4/2011') d
CROSS JOIN #Users u
LEFT JOIN #UsersEvents ue ON d.[Date] = ue.EventDate AND ue.UserID=u.UserID












Viewer, scheduler and manager for Crystal reports.
Send you report everywhere.
 
Rtag - thank you very much.

I don't think the crosstab is going to work for me anymore, unfortunately.

This was a simple sounding report (at first), but its getting complicated now..
I think i will end up having to use subreports in order to get this type of style report:


any suggestions?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top