SQL SELECT 查询总计和构建总计的部分
我需要针对以下场景进行 SQL Server 查询:
表:
CREATE TABLE [dbo].[JobStatus]
(
[OID] [int] IDENTITY(1, 1)
NOT NULL,
[Name] [varchar](100) NOT NULL,
[Code] [varchar](5) NOT NULL,
[PictureID] [int] NOT NULL,
[LastModifiedAt] [timestamp] NOT NULL,
CONSTRAINT [PK_JobStatuses] PRIMARY KEY CLUSTERED ([OID] 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
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[JobStatus] ON
INSERT [dbo].[JobStatus]
([OID], [Name], [Code], [PictureID])
VALUES
(1, N'Ready to book', N'J_RTB', 13)
INSERT [dbo].[JobStatus]
([OID], [Name], [Code], [PictureID])
VALUES
(2, N'Pending', N'J_P', 14)
INSERT [dbo].[JobStatus]
([OID], [Name], [Code], [PictureID])
VALUES
(3, N'Booked', N'J_B', 15)
INSERT [dbo].[JobStatus]
([OID], [Name], [Code], [PictureID])
VALUES
(4, N'Cancelled', N'J_CAN', 16)
INSERT [dbo].[JobStatus]
([OID], [Name], [Code], [PictureID])
VALUES
(6, N'CallBack', N'J_CAL', 17)
SET IDENTITY_INSERT [dbo].[JobStatus] OFF
CREATE TABLE [dbo].[JobBatch]
(
[OID] [varchar](250) NOT NULL,
[UserID] [bigint] NOT NULL,
[BatchDate] [smalldatetime] NOT NULL,
[LastModifiedAt] [timestamp] NOT NULL,
CONSTRAINT [PK_JobBatches] PRIMARY KEY CLUSTERED ([OID] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[JobBatch] ADD CONSTRAINT [DF_JobBatch_BatchDate] DEFAULT (GETDATE()) FOR [BatchDate]
GO
CREATE TABLE [dbo].[Job]
(
[OID] [bigint] IDENTITY(1, 1)
NOT NULL,
[BatchID] [varchar](250) NOT NULL,
[PharmacyID] [bigint] NOT NULL,
[BranchID] [bigint] NOT NULL,
[CoordinatorID] [bigint] NOT NULL,
[LocumTypeID] [int] NOT NULL,
[ShiftID] [int] NOT NULL,
[JobDate] [smalldatetime] NOT NULL,
[Rate] [money] NOT NULL,
[RatePlus] [money] NOT NULL,
[StartTime] [smalldatetime] NOT NULL,
[EndTime] [smalldatetime] NOT NULL,
[PriorityID] [tinyint] NOT NULL,
[JobCode] [varchar](100) NULL,
[JobStatusID] [int] NOT NULL,
[TravelExpense] [bit] NOT NULL,
[MealAllowance] [bit] NOT NULL,
[Accommodation] [bit] NOT NULL,
[UserID] [bigint] NULL,
[RegisteredDate] [smalldatetime] NULL,
[TextSent] [bit] NOT NULL,
[BookingFee] [decimal](6, 2) NOT NULL,
[LastModifiedAt] [timestamp] NOT NULL,
CONSTRAINT [PK__Jobs__056690C222951AFD] PRIMARY KEY CLUSTERED ([OID] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Job] WITH CHECK ADD CONSTRAINT [FK_Job_JobBatch] FOREIGN KEY([BatchID])
REFERENCES [dbo].[JobBatch] ([OID])
GO
ALTER TABLE [dbo].[Job] CHECK CONSTRAINT [FK_Job_JobBatch]
GO
ALTER TABLE [dbo].[Job] WITH CHECK ADD CONSTRAINT [FK_Job_JobStatus] FOREIGN KEY([JobStatusID])
REFERENCES [dbo].[JobStatus] ([OID])
GO
ALTER TABLE [dbo].[Job] CHECK CONSTRAINT [FK_Job_JobStatus]
GO
查询:
SELECT
dbo.Job.BatchID,
COUNT(dbo.Job.OID) AS Total,
dbo.JobBatch.BatchDate
FROM
dbo.Job
INNER JOIN dbo.JobBatch ON dbo.Job.BatchID = dbo.JobBatch.OID
GROUP BY
dbo.Job.BatchID,
dbo.JobBatch.BatchDate
HAVING
(dbo.JobBatch.BatchDate > CONVERT(DATETIME, '2010-08-01 00:00:00', 102))
ORDER BY
dbo.JobBatch.BatchDate,
Total DESC
现在,有一个名为 JobStatusID 的列。它包含代表作业状态的五个值。我需要这样选择数据:
GoogleDocs Online Sample Spreadsheet
Group By 和having 需要保持完整。有什么想法吗?谢谢。
I need a SQL Server Query for the following scenario:
The Tables:
CREATE TABLE [dbo].[JobStatus]
(
[OID] [int] IDENTITY(1, 1)
NOT NULL,
[Name] [varchar](100) NOT NULL,
[Code] [varchar](5) NOT NULL,
[PictureID] [int] NOT NULL,
[LastModifiedAt] [timestamp] NOT NULL,
CONSTRAINT [PK_JobStatuses] PRIMARY KEY CLUSTERED ([OID] 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
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[JobStatus] ON
INSERT [dbo].[JobStatus]
([OID], [Name], [Code], [PictureID])
VALUES
(1, N'Ready to book', N'J_RTB', 13)
INSERT [dbo].[JobStatus]
([OID], [Name], [Code], [PictureID])
VALUES
(2, N'Pending', N'J_P', 14)
INSERT [dbo].[JobStatus]
([OID], [Name], [Code], [PictureID])
VALUES
(3, N'Booked', N'J_B', 15)
INSERT [dbo].[JobStatus]
([OID], [Name], [Code], [PictureID])
VALUES
(4, N'Cancelled', N'J_CAN', 16)
INSERT [dbo].[JobStatus]
([OID], [Name], [Code], [PictureID])
VALUES
(6, N'CallBack', N'J_CAL', 17)
SET IDENTITY_INSERT [dbo].[JobStatus] OFF
CREATE TABLE [dbo].[JobBatch]
(
[OID] [varchar](250) NOT NULL,
[UserID] [bigint] NOT NULL,
[BatchDate] [smalldatetime] NOT NULL,
[LastModifiedAt] [timestamp] NOT NULL,
CONSTRAINT [PK_JobBatches] PRIMARY KEY CLUSTERED ([OID] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[JobBatch] ADD CONSTRAINT [DF_JobBatch_BatchDate] DEFAULT (GETDATE()) FOR [BatchDate]
GO
CREATE TABLE [dbo].[Job]
(
[OID] [bigint] IDENTITY(1, 1)
NOT NULL,
[BatchID] [varchar](250) NOT NULL,
[PharmacyID] [bigint] NOT NULL,
[BranchID] [bigint] NOT NULL,
[CoordinatorID] [bigint] NOT NULL,
[LocumTypeID] [int] NOT NULL,
[ShiftID] [int] NOT NULL,
[JobDate] [smalldatetime] NOT NULL,
[Rate] [money] NOT NULL,
[RatePlus] [money] NOT NULL,
[StartTime] [smalldatetime] NOT NULL,
[EndTime] [smalldatetime] NOT NULL,
[PriorityID] [tinyint] NOT NULL,
[JobCode] [varchar](100) NULL,
[JobStatusID] [int] NOT NULL,
[TravelExpense] [bit] NOT NULL,
[MealAllowance] [bit] NOT NULL,
[Accommodation] [bit] NOT NULL,
[UserID] [bigint] NULL,
[RegisteredDate] [smalldatetime] NULL,
[TextSent] [bit] NOT NULL,
[BookingFee] [decimal](6, 2) NOT NULL,
[LastModifiedAt] [timestamp] NOT NULL,
CONSTRAINT [PK__Jobs__056690C222951AFD] PRIMARY KEY CLUSTERED ([OID] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[Job] WITH CHECK ADD CONSTRAINT [FK_Job_JobBatch] FOREIGN KEY([BatchID])
REFERENCES [dbo].[JobBatch] ([OID])
GO
ALTER TABLE [dbo].[Job] CHECK CONSTRAINT [FK_Job_JobBatch]
GO
ALTER TABLE [dbo].[Job] WITH CHECK ADD CONSTRAINT [FK_Job_JobStatus] FOREIGN KEY([JobStatusID])
REFERENCES [dbo].[JobStatus] ([OID])
GO
ALTER TABLE [dbo].[Job] CHECK CONSTRAINT [FK_Job_JobStatus]
GO
The query:
SELECT
dbo.Job.BatchID,
COUNT(dbo.Job.OID) AS Total,
dbo.JobBatch.BatchDate
FROM
dbo.Job
INNER JOIN dbo.JobBatch ON dbo.Job.BatchID = dbo.JobBatch.OID
GROUP BY
dbo.Job.BatchID,
dbo.JobBatch.BatchDate
HAVING
(dbo.JobBatch.BatchDate > CONVERT(DATETIME, '2010-08-01 00:00:00', 102))
ORDER BY
dbo.JobBatch.BatchDate,
Total DESC
Now, there is a column called JobStatusID. It holds five values representing a Job's status. I need the data selected like this:
GoogleDocs Online Sample Spreadsheet
The Group By and the Having needs to be intact. Any ideas? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)