Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5978635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:32:39+00:00 2026-05-22T21:32:39+00:00

I have a table containing some data which needs to be displayed in a

  • 0

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:
enter image description here

The required format I need to get the data out is like the screenshot below:
enter image description here

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:32:39+00:00Added an answer on May 22, 2026 at 9:32 pm

    As suggested in my comment to the original question, I belive normalization is a good long-term solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose (to simplify) I have a table containing some control vs. treatment data: Which,
I have a table containing data about some users. Many of them use our
I have a table containing data which I do not want to wrap onto
I have a table containing user input which needs to be optimized. I have
I have a number of tables containing some basic (business related) mapping data. What's
I have a table containing some names and their associated ID, along with a
Here's the situation I'm in. I have a table containing peoples' information. Some of
I have a table, containing rows, contaning cells - and some of them contain
I have a Table containing some information that I need. All these rows also
I have a table containing hierarchical data. There are currently ~8 levels in this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.