在查询中,我想检查员工(表 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