SQL SELECT 查询总计和构建总计的部分

发布于 2024-09-28 21:33:44 字数 3933 浏览 2 评论 0原文

我需要针对以下场景进行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2024-10-05 21:33:44
SELECT 
  dbo.Job.BatchID,
  COUNT(dbo.Job.OID) AS Total,
  COUNT(CASE WHEN JobStatusID = 1 THEN 1 END) AS [Ready to book],
  COUNT(CASE WHEN JobStatusID = 2 THEN 1 END) AS [Pending],
  COUNT(CASE WHEN JobStatusID = 3 THEN 1 END) AS [Booked],
  COUNT(CASE WHEN JobStatusID = 4 THEN 1 END) AS [Cancelled],
  COUNT(CASE WHEN JobStatusID = 6 THEN 1 END) AS [CallBack],
  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, '2000-01-01 00:00:00', 102))
ORDER BY
  dbo.JobBatch.BatchDate,
  Total DESC
SELECT 
  dbo.Job.BatchID,
  COUNT(dbo.Job.OID) AS Total,
  COUNT(CASE WHEN JobStatusID = 1 THEN 1 END) AS [Ready to book],
  COUNT(CASE WHEN JobStatusID = 2 THEN 1 END) AS [Pending],
  COUNT(CASE WHEN JobStatusID = 3 THEN 1 END) AS [Booked],
  COUNT(CASE WHEN JobStatusID = 4 THEN 1 END) AS [Cancelled],
  COUNT(CASE WHEN JobStatusID = 6 THEN 1 END) AS [CallBack],
  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, '2000-01-01 00:00:00', 102))
ORDER BY
  dbo.JobBatch.BatchDate,
  Total DESC
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文