Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Simple Crosstab Procedure with Power Expand / Collapse
Author
Message
Posted Sunday, April 05, 2009 10:27 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 28, 2009 12:25 PM
Points: 2, Visits: 38
Comments posted to this topic are about the item Simple Crosstab Procedure with Power
Post #690605
Posted Tuesday, May 05, 2009 9:11 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, July 13, 2012 10:13 AM
Points: 1, Visits: 23
I don't have experience using crosstabs, but shouldn't the results in your example be:
TYPE RED BLUE GREEN
A 2 0 0
B 1 0 0
C 0 1 0
D 0 0 1
Post #710348
Posted Tuesday, May 05, 2009 10:55 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: 2 days ago @ 1:27 PM
Points: 1,343, Visits: 372
I can't get past the syntax errors. I wish ServerCentral would fix their script upload feature because getting the scripts to work has proven to be a royal pain.

-- Mark D Powell --

Finally, got it working. I had to retype the lead into several of the lines which display as little boxes in the posting and had become invisible when I copied and pasted via Word. Notepad shows the garbage. Once removed the procedure compiled and appears to work.
Post #710474
Posted Thursday, July 02, 2009 10:05 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, October 27, 2012 8:38 AM
Points: 10, Visits: 55
I am having a problem getting this to do what I want... First problem, I cannot seem to get the procedure to take a query as an argument in parenthesis as stated.

To test, I just wrapped 'Table1' with '(SELECT * FROM Table1)'

-- Incorrect syntax near ')'.

The second problem is more complex and I will post another reply.
Post #746761
Posted Thursday, July 02, 2009 11:03 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, October 27, 2012 8:38 AM
Points: 10, Visits: 55
The second problem I am having is more complex. I am trying to figure out 2 things.
1) How to get the column headings (and totals)
2) How to have a report use dynamic columns

Below is a sample table I created:
CREATE TABLE [dbo].[TimeSheetTest](
[ID] [int] IDENTITY(1,1) NOT NULL,
[EmployeeCode] [varchar](50) NOT NULL,
[ProjectCode] [varchar](50) NOT NULL,
[HoursWorked] [decimal](18, 2) NOT NULL,
[DateWorked] [datetime] NOT NULL,
CONSTRAINT [PK_TimeSheetTest] PRIMARY KEY CLUSTERED
(
[ID] ASC
) ON [PRIMARY]
) ON [PRIMARY]

Then, add the following data:
INSERT INTO TimeSheetTest 
SELECT 'Adam', 'P1', 8, '2009-01-01'
UNION ALL SELECT 'Adam', 'P2', 1, '2009-01-01'
UNION ALL SELECT 'Bob', 'P1', 5, '2009-01-01'
UNION ALL SELECT 'Bob', 'P2', 3, '2009-01-01'
UNION ALL SELECT 'Adam', 'P1', 8, '2009-01-03'
UNION ALL SELECT 'Adam', 'P1', 8, '2009-01-05'
UNION ALL SELECT 'Bob', 'P1', 7, '2009-01-05'
UNION ALL SELECT 'Bob', 'P2', 1, '2009-01-05'

Now, using the basic syntax for getting the xtab:
exec UberCrosstab 
'EmployeeCode, ProjectCode',
'DateWorked',
'TimeSheetTest',
'HoursWorked',
'SUM'

I get the following results (I dropped the time 12:00AM from below for readability):
EmployeeCode     ProjectCode       Jan 1 2009    Jan 3, 2009    Jan 5, 2009
Adam P1 8.00 8.00 8.00
Bob P1 5.00 NULL 7.00
Adam P2 1.00 NULL NULL
Bob P2 3.00 NULL 1.00

This part works as expected. However, I want to display Thu 01/01, Sat 01/03, Mon 01/05

so, I created a field as follows:
SELECT *, LEFT(DATENAME(dw, DateWorked), 3) + ' ' + CONVERT(VARCHAR(5), DateWorked, 1) AS Header
FROM TimeSheetTest

Of course (since I cannot get the (query) to work), I do the following:

SELECT *, LEFT(DATENAME(dw, DateWorked), 3) + ' ' + CONVERT(VARCHAR(5), DateWorked, 1) AS Header
INTO #temp
FROM TimeSheetTest

exec UberCrosstab
'EmployeeCode, ProjectCode',
'Header',
'#temp',
'HoursWorked',
'SUM'

drop table #temp

Now, the table appears with headings of what I want (Mon 01/05, Sat 01/03, Thu 01/01), but the problem is they are sorted by the text (M in Monday comes before S - Saturday, etc).

[highlight=#ffff11]
Q. How do I sort it the way I want?
[/highlight]

I am also trying to get all the dates in a date range, not just those that have data. This is not a problem, other than the data comes out sorted by Fri, Mon, Sat, Sun, Thurs, Tues, Wed.
SET NOCOUNT ON

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
DECLARE @ctr INT
DECLARE @diff INT

DECLARE @days TABLE (
WorkDay DATETIME PRIMARY KEY,
WorkDayName VARCHAR(50),
Counter INT
)

SELECT @StartDate = '2009-01-01'
SELECT @EndDate = '2009-01-07'
SELECT @diff = DATEDIFF(d, @StartDate, @EndDate)

SELECT @ctr = 0
WHILE @ctr <= @diff BEGIN
INSERT INTO @days (
WorkDay,
WorkDayName,
Counter
) VALUES (
@StartDate + @ctr,
LEFT(DATENAME(dw, @StartDate + @ctr), 3) + '. ' + LEFT(CONVERT(VARCHAR(8), @StartDate + @ctr, 1), 5),
@ctr + 1
)
SET @ctr = @ctr + 1
END

SELECT *
INTO #temp

FROM @days d
LEFT OUTER JOIN TimeSheetTest t
ON d.WorkDay = t.DateWorked

exec UberCrosstab
'EmployeeCode, ProjectCode',
'WorkDayName',
'#temp',
'HoursWorked',
'SUM'

drop table #temp

Part 2 of the problem is to create a report that will have dynamic fields in the crosstab, and a total afterwards. I have not tried it, but I am assuming I can create another table variable or query that totals across for the period (in this case, one week) and join that with this data. That will get me the totals.

The tricky part is the workdays. It might be 7. It might be 14 (paid every 2 weeks). It might be a little more if paid twice a month (1-15, 16-31 for some months, 16-28 or 29 for February, etc). It could be 28-31 days (depending again on the month).

[highlight=#ffff11]
Please tell me there is a way other than to create 31 columns and hide the ones that are not used.
[/highlight]

Post #746779
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse