0

在查询中,我想检查员工(表 EMPLOYEE)在过去一年中每个月是否至少工作了一个期间(期间在表 PERIOD 中)。之后,我想获得员工每个月工作的天数。谁能提供解决方案/解释?

员工表:

USE [MetukisDB]
GO

/****** Object:  Table [dbo].[TblEmployee]    Script Date: 05/06/2013 23:10:17 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[TblEmployee](
    [employeeNumber] [char](8) NOT NULL,
    [employeePrivateName] [varchar](20) NOT NULL,
    [employeeFamilyName] [varchar](20) NOT NULL,
    [city] [varchar](20) NULL,
    [street] [varchar](20) NULL,
    [houseNo] [int] NULL,
    [phoneNumber] [char](10) NULL,
    [birthDate] [datetime] NULL,
    [startWorkingDate] [datetime] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [employeeNumber] 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].[TblEmployee]  WITH CHECK ADD CHECK  (([employeeNumber] like '[1-9][1-9][1-9][1-9][1-9][1-9][1-9][1-9]'))
GO

ALTER TABLE [dbo].[TblEmployee]  WITH CHECK ADD CHECK  (([houseNo]>(0)))
GO

ALTER TABLE [dbo].[TblEmployee]  WITH CHECK ADD CHECK  (([phoneNumber] like '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'))
GO

周期表:

USE [MetukisDB]
GO

/****** Object:  Table [dbo].[TblPeriod]    Script Date: 05/06/2013 23:11:46 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TblPeriod](
    [Number] [int] NOT NULL,
    [fromDate] [datetime] NULL,
    [toDate] [datetime] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [Number] 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

ALTER TABLE [dbo].[TblPeriod]  WITH CHECK ADD  CONSTRAINT [fromDate_check] CHECK  (([fromDate]<=[toDate]))
GO

ALTER TABLE [dbo].[TblPeriod] CHECK CONSTRAINT [fromDate_check]
GO

ALTER TABLE [dbo].[TblPeriod]  WITH CHECK ADD  CONSTRAINT [Number] CHECK  (([Number]>(0)))
GO

ALTER TABLE [dbo].[TblPeriod] CHECK CONSTRAINT [Number]
GO

预定表:

USE [MetukisDB]
GO

/****** Object:  Table [dbo].[TblScheduled]    Script Date: 05/07/2013 15:17:59 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[TblScheduled](
    [jobNumber] [int] NOT NULL,
    [employeeNumber] [char](8) NOT NULL,
    [machineNumber] [int] NOT NULL,
    [Number] [int] NOT NULL,
 CONSTRAINT [scheduled_PK] PRIMARY KEY CLUSTERED 
(
    [jobNumber] ASC,
    [employeeNumber] ASC,
    [machineNumber] ASC,
    [Number] 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].[TblScheduled]  WITH CHECK ADD  CONSTRAINT [certified_period_FK] FOREIGN KEY([jobNumber], [employeeNumber], [machineNumber])
REFERENCES [dbo].[TblCertified] ([jobNumber], [employeeNumber], [machineNumber])
ON UPDATE CASCADE
GO

ALTER TABLE [dbo].[TblScheduled] CHECK CONSTRAINT [certified_period_FK]
GO

ALTER TABLE [dbo].[TblScheduled]  WITH CHECK ADD  CONSTRAINT [Number_FK] FOREIGN KEY([Number])
REFERENCES [dbo].[TblPeriod] ([Number])
ON UPDATE CASCADE
GO

ALTER TABLE [dbo].[TblScheduled] CHECK CONSTRAINT [Number_FK]
GO

这是我到目前为止所拥有的:

select TblEmployee.employeeNumber
from TblEmployee
where(DATEDIFF(year, TblEmployee.birthDate, GETDATE())>25)
select TblSnack.productNumber, TblProduces.machineNumber, TblCertified.employeeNumber 
from TblSnack inner join TblProduces on TblProduces.productNumber= TblSnack.productNumber
inner join TblCertified on TblCertified.machineNumber= TblProduces.machineNumber inner join 
TblChocolate on TblChocolate.productNumber=TblProduces.productNumber
where(TblSnack.containGluten=0) and (TblSnack.containSoy=0)
group by TblProduces.machineNumber, TblCertified.employeeNumber, tblsnack.productNumber
having (COUNT( TblChocolate.productNumber) > 1)
select TblScheduled.Number, TblScheduled.employeeNumber
from TblScheduled
where TblScheduled.Number= (SELECT number 
FROM TblPeriod 
WHERE (MONTH(TblPeriod.fromDate)) = month (dateAdd (month,-1,GETDATE())))
group by TblScheduled.Number, TblScheduled.employeeNumber
4

1 回答 1

1

Try something like this for your first question:

-- Employees who have worked at least one period each month in the past year
SELECT t.employeeNumber,
t.employeeFamilyName
FROM
( SELECT TblEmployee.employeeNumber,
TblEmployee.employeeFamilyName,
COUNT(MONTH(TblPeriod.fromDate)) AS MonthCount
FROM TblEmployee 
JOIN TblScheduled
ON TblScheduled.employeeNumber = TblEmployee.employeeNumber
JOIN TblPeriod 
ON TblPeriod.Number = TblScheduled.Number
WHERE TblPeriod.fromDate >= DATEADD(YEAR,-1,GETDATE())
GROUP BY TblEmployee.employeeNumber,
TblEmployee.employeeFamilyName,
MONTH(TblPeriod.fromDate) ) AS t
WHERE t.MonthCount = 12
GROUP BY t.employeeNumber,
t.employeeFamilyName

And something like this for the second question:

-- Average number of the days the employee has worked each month
SELECT t.employeeNumber,
t.employeeFamilyName,
AVG(t.ScheduleCount) AS AverageCount
FROM
( SELECT TblEmployee.employeeNumber,
TblEmployee.employeeFamilyName,
MONTH(TblPeriod.fromDate) AS PeriodMonth,
COUNT(TblScheduled.Number) AS ScheduleCount
FROM TblEmployee 
JOIN TblScheduled
ON TblScheduled.employeeNumber = TblEmployee.employeeNumber
JOIN TblPeriod 
ON TblPeriod.Number = TblScheduled.Number
WHERE TblPeriod.fromDate >= DATEADD(YEAR,-1,GETDATE())
GROUP BY TblEmployee.employeeNumber,
TblEmployee.employeeFamilyName,
MONTH(TblPeriod.fromDate) ) AS t
GROUP BY t.employeeNumber,
t.employeeFamilyName

Note how this joins the three tables using foreign keys and primary keys. The GROUP BY allows us to, e.g., count entries in TblScheduled.

于 2013-05-07T12:26:40.167 回答