我有一个包含一些数据的表格,需要在屏幕上以特定格式显示。
表格结构如下:
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
在这样的屏幕上输入数据,可以更好地显示JobSheetLineTable的格式:
我需要获取数据所需的格式如下面的截图:
需要员工过滤数据(JobSheetLine表中的RequestedByID) 以及每个独特的合同和项目以及成本费率(成本费率从B1,B15,B2等一直到CustomCostRate6)。
每个费用率的总小时数需要每天。
我已经到了下面的查询,直到我重新开始这不会起作用。我认为问题是由于成本率是JobSheetLine表中的Columns引起的,但是所需的输出将成本率作为单个列,并且行和值显示在行上。
我研究了在SQL中使用PIVOT关键字,但是我无法弄清楚我在做什么。
这是我到目前为止提出的查询(无法正常工作)
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]
现在可以用数据结构实现我想要的输出吗?我宁愿在SQL中进行所有计算,因为它更快,但我可以在C#中进行一些计算,因为这是一个asp.net项目。
答案 0 :(得分:2)
Simen S.建议回答这个问题。需要将表格标准化为以下格式:
答案 1 :(得分:1)
正如我对原始问题的评论中所建议的那样,我认为正常化是一个很好的长期解决方案。