Reputation: 18013
I have a table containing some data which needs to be displayed in a certain format on screen.
The table structures are as follows:
CREATE TABLE [dbo].[JobSheet](
[JobSheetID] [bigint] IDENTITY(1,1) NOT NULL,
[SheetNumber] [bigint] NULL,
[JobSheetDate] [datetime] NULL,
[ContractID] [bigint] NULL,
[ContractCode] [varchar](50) NULL,
[ContractTitle] [varchar](100) NULL,
[ProjectID] [bigint] NULL,
[ProjectCode] [varchar](50) NULL,
[ProjectTitle] [varchar](100) NULL,
[JobID] [bigint] NULL,
[JobCode] [varchar](50) NULL,
[JobTitle] [varchar](100) NULL,
[SageDatabaseID] [bigint] NULL,
[SageDatabaseName] [varchar](50) NULL,
[EnteredByID] [bigint] NULL,
[EnteredByUsername] [varchar](50) NULL,
[DocumentStatus] [varchar](50) NULL,
[Deleted] [bit] NULL,
[Reference] [varchar](50) NULL,
[UpdatedDate] [datetime] NULL,
[UpdatedUser] [varchar](50) NULL,
[SentToSage] [bit] NULL,
[UpdateStatus] [varchar](50) NULL,
CONSTRAINT [PK_JobSheet] PRIMARY KEY CLUSTERED
(
[JobSheetID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[JobSheetLine](
[JobSheetLineID] [bigint] IDENTITY(1,1) NOT NULL,
[JobSheetID] [bigint] NULL,
[LineType] [varchar](50) NULL,
[TSHumanResourceID] [bigint] NULL,
[SageDatabaseID] [bigint] NULL,
[Surname] [varchar](50) NULL,
[Initial] [varchar](1) NULL,
[TimeOn] [datetime] NULL,
[TimeOff] [datetime] NULL,
[Deleted] [bit] NULL,
[B1] [decimal](18, 5) NULL,
[B15] [decimal](18, 5) NULL,
[B2] [decimal](18, 5) NULL,
[HB] [decimal](18, 5) NULL,
[S1] [decimal](18, 5) NULL,
[S15] [decimal](18, 5) NULL,
[S2] [decimal](18, 5) NULL,
[HS] [decimal](18, 5) NULL,
[O1] [decimal](18, 5) NULL,
[T] [decimal](18, 5) NULL,
[TS] [decimal](18, 5) NULL,
[TT] [decimal](18, 5) NULL,
[TP] [decimal](18, 5) NULL,
[OT] [decimal](18, 5) NULL,
[OS] [decimal](18, 5) NULL,
[D] [decimal](18, 5) NULL,
[POA] [decimal](18, 5) NULL,
[PT1] [decimal](18, 5) NULL,
[PT2] [decimal](18, 5) NULL,
[CustomCostRate1] [decimal](18, 5) NULL,
[CustomCostRate1ID] [bigint] NULL,
[CustomCostRate2] [decimal](18, 5) NULL,
[CustomCostRate2ID] [bigint] NULL,
[CustomCostRate3] [decimal](18, 5) NULL,
[CustomCostRate3ID] [bigint] NULL,
[CustomCostRate4] [decimal](18, 5) NULL,
[CustomCostRate4ID] [bigint] NULL,
[CustomCostRate5] [decimal](18, 5) NULL,
[CustomCostRate5ID] [bigint] NULL,
[CustomCostRate6] [decimal](18, 5) NULL,
[CustomCostRate6ID] [bigint] NULL,
[UpdatedDate] [datetime] NULL,
[UpdatedUser] [varchar](50) NULL,
[RejectReason] [varchar](255) NULL,
[CurrentStepApprovalCount] [bigint] NULL,
[CustomRouteID] [bigint] NULL,
[CustomRoute] [bit] NULL,
[CurrentStep] [bigint] NULL,
[WaitingForType] [varchar](50) NULL,
[WaitingForID] [bigint] NULL,
[RequestedByID] [bigint] NULL,
[LineStatus] [varchar](50) NULL,
CONSTRAINT [PK_JobSheetLine] PRIMARY KEY CLUSTERED
(
[JobSheetLineID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
The data is entered on a screen like this which may show the format of the JobSheetLineTable better:
The required format I need to get the data out is like the screenshot below:
The Data needs to be filtered by employee (RequestedByID in the JobSheetLine table) and each unique, Contract and Project, and Cost Rate (the cost rates run from B1, B15, B2 etc all the way to CustomCostRate6).
The total number of hours for each cost rate needs to be per day.
I have got as far as the query below until I relized this is not going to work. I think the problem is caused due to the cost rates being Columns in the JobSheetLine table, but the required output has the cost rate as a single column and rates and values appear on the rows.
I looked into using the PIVOT keyword in SQL but I couldn't work out what I was doing with it.
Here is the query I have come up with so far (which wont work)
DECLARE @Deleted AS BIT
DECLARE @RequestedByID AS BIGINT
DECLARE @WeekStartDate AS DATETIME
DECLARE @WeekEndDate AS DATETIME
DECLARE @WaitingForUserID AS BIGINT
DECLARE @WaitingForUserTypeID AS BIGINT
DECLARE @WaitingForTypeUser AS VARCHAR(50)
DECLARE @WaitingForTypeUserType AS VARCHAR(50)
SET @Deleted = 0
SET @RequestedByID = 2
SET @WeekStartDate = '2011/05/23 00:00'
SET @WeekEndDate = '2011/05/29 00:00'
SELECT
[JobSheet].[JobSheetDate],
[JobSheet].[ContractID],
[JobSheet].[ContractCode],
[JobSheet].[ContractTitle],
[JobSheet].[ProjectID],
[JobSheet].[ProjectCode],
[JobSheet].[ProjectTitle],
[JobSheet].[JobID],
[JobSheet].[JobCode],
[JobSheet].[JobTitle],
[JobSheet].[SageDatabaseID],
ISNULL(SUM([JobSheetLine].[B1]),0) AS B1,
ISNULL(SUM([JobSheetLine].[B15]),0) AS B15,
ISNULL(SUM([JobSheetLine].[B2]),0) AS B2,
ISNULL(SUM([JobSheetLine].[HB]),0) AS HB,
ISNULL(SUM([JobSheetLine].[S1]),0) AS S1,
ISNULL(SUM([JobSheetLine].[S15]),0) AS S15,
ISNULL(SUM([JobSheetLine].[S2]),0) AS S2,
ISNULL(SUM([JobSheetLine].[HS]),0) AS HS,
ISNULL(SUM([JobSheetLine].[O1]),0) AS O1,
ISNULL(SUM([JobSheetLine].[T]),0) AS T,
ISNULL(SUM([JobSheetLine].[TS]),0) AS TS,
ISNULL(SUM([JobSheetLine].[TT]),0) AS TT,
ISNULL(SUM([JobSheetLine].[TP]),0) AS TP,
ISNULL(SUM([JobSheetLine].[OT]),0) AS OT,
ISNULL(SUM([JobSheetLine].[OS]),0) AS OS,
ISNULL(SUM([JobSheetLine].[D]),0) AS D,
ISNULL(SUM([JobSheetLine].[POA]),0) AS POA,
ISNULL(SUM([JobSheetLine].[PT1]),0) AS PT1,
ISNULL(SUM([JobSheetLine].[PT2]),0) AS PT2,
ISNULL(SUM([JobSheetLine].[CustomCostRate1]),0) AS CustomCostRate1,
ISNULL(SUM([JobSheetLine].[CustomCostRate2]),0) AS CustomCostRate2,
ISNULL(SUM([JobSheetLine].[CustomCostRate3]),0) AS CustomCostRate3,
ISNULL(SUM([JobSheetLine].[CustomCostRate4]),0) AS CustomCostRate4,
ISNULL(SUM([JobSheetLine].[CustomCostRate5]),0) AS CustomCostRate5,
ISNULL(SUM([JobSheetLine].[CustomCostRate6]),0) AS CustomCostRate6,
ISNULL(SUM([JobSheetLine].[B1] +
[JobSheetLine].[B15] +
[JobSheetLine].[B2] +
[JobSheetLine].[HB] +
[JobSheetLine].[S1] +
[JobSheetLine].[S15] +
[JobSheetLine].[S2] +
[JobSheetLine].[HS] +
[JobSheetLine].[O1] +
[JobSheetLine].[T] +
[JobSheetLine].[TS] +
[JobSheetLine].[TT] +
[JobSheetLine].[TP] +
[JobSheetLine].[OT] +
[JobSheetLine].[OS] +
[JobSheetLine].[D] +
[JobSheetLine].[POA] +
[JobSheetLine].[PT1] +
[JobSheetLine].[PT2] +
[JobSheetLine].[CustomCostRate1] +
[JobSheetLine].[CustomCostRate2] +
[JobSheetLine].[CustomCostRate3] +
[JobSheetLine].[CustomCostRate4] +
[JobSheetLine].[CustomCostRate5] +
[JobSheetLine].[CustomCostRate6]),0) AS TotalHours
FROM [JobSheet]
INNER JOIN [JobSheetLine]
ON [JobSheetLine].[JobSheetID]=[JobSheet].[JobSheetID]
WHERE
[JobSheet].[Deleted]=@Deleted
AND [JobSheet].[JobSheetDate] >= @WeekStartDate
AND [JobSheet].[JobSheetDate] <= @WeekEndDate
AND [JobSheetLine].[Deleted]=@Deleted
AND [JobSheetLine].[RequestedByID]=@RequestedByID
GROUP BY
[JobSheet].[JobSheetDate],
[JobSheet].[ContractID],
[JobSheet].[ContractCode],
[JobSheet].[ContractTitle],
[JobSheet].[ProjectID],
[JobSheet].[ProjectCode],
[JobSheet].[ProjectTitle],
[JobSheet].[JobID],
[JobSheet].[JobCode],
[JobSheet].[JobTitle],
[JobSheet].[SageDatabaseID]
Is it possible to achieve the output I want with the structure of the data as it is at the moment? I would rather do all the calculation in SQL as its faster but I can do some of the calculation in C# as this is an asp.net project.
Upvotes: 0
Views: 205
Reputation: 3205
As suggested in my comment to the original question, I belive normalization is a good long-term solution.
Upvotes: 1
Reputation: 18013
The Answer to this question was suggested by Simen S. The Tables needed to be normalised in to the format below:
Upvotes: 2