如何在where子句中使用多个条件?

时间:2016-07-31 11:42:21

标签: sql-server

我是SQL Server的新手。我正在使用SQL Server 2008 R2。我创建了两个名为adding_hangerallot的表,如下所示

CREATE TABLE [dbo].[adding_hanger]
(
    [End_Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Hanger_Location] [char](10) NOT NULL,
    [Hanger_Capacity] [int] NOT NULL,
    [Hanger_Id]  AS ((CONVERT([varchar](100),substring([Hanger_Location],(1),(3)),0)+'101')+CONVERT([varchar](100),[End_Id],0)) PERSISTED NOT NULL,
    [Manager_Name] [varchar](15) NOT NULL,
    [Manager_Id]  AS ((CONVERT([varchar](4),substring([Social_Security_No],(8),(4)),0)+'31')+CONVERT([varchar](100),[End_Id],0)) PERSISTED NOT NULL,
    [Manager_Password]  AS ((CONVERT([varchar](3),substring([Manager_Name],(1),(3)),0)+'@')+CONVERT([varchar](3),substring([Hanger_Location],(1),(3)),0)),
    [Social_Security_No] [varchar](15) NOT NULL,
    [Date_of_Birth] [datetime] NOT NULL,
    [Gender] [varchar](8) NOT NULL,
    [Mobile_No] [varchar](50) NOT NULL,
    [Email_Address] [varchar](30) NOT NULL,
    [House_No] [varchar](10) NOT NULL,
    [Address_Line_1] [varchar](30) NOT NULL,
    [Address_id]  AS ((CONVERT([varchar](100),substring([City],(1),(3)),0)+'31')+CONVERT([varchar](100),[End_Id],0)) PERSISTED NOT NULL,
    [City] [char](15) NOT NULL,
    [State] [char](15) NOT NULL,
    [Country] [char](15) NOT NULL,
    [Pin_No] [int] NOT NULL
)

CREATE TABLE [dbo].[allot]
(
    [Fromdate] [datetime] NULL,
    [todate] [datetime] NULL,
    [hangarlocation] [char](10) NULL,
    [hangarno] [varchar](100) NULL,
    [planeid] [varchar](100) NULL
)

如何获取在给定输入日期内给定的范围内未分配的可用挂钩的详细信息,如果已经分配,​​也不会超过挂钩的容量?

2 个答案:

答案 0 :(得分:1)

首先,你的“机库”拼写不一致,应予以解决。

我假设机库容量代表您可以在该机库中存储的分配数量?如果是这样,机库容量可能会在给定时间范围内变化,因为添加/删除了分配(根据FromDate和ToDate),因此您的问题不明确。如果你想要一个在给定时间内肯定有空间的机库,我会使用:

declare @from_date datetime, @end_date datetime, @i int

-- Set your start and end dates (replace the dates below with the correct ones)
set @from_date = '2016-07-01'
set @to_date = '2016-07-31'
set @i = 1  -- counter

-- Create a table of dates
select @from_date as date into #dates
while dateadd(day,@i,@from_date) <= @to_date
begin
  insert into #dates values (dateadd(day,@i,@from_date))
end

-- Create table of date, hangar_id, hangar_capacity
select date, hangar_id, hangar_capacity
into #dates_by_hangar
from adding_hangar
inner join #dates on 1 = 1

-- Table showing hangars with at least one free space on every date in the range
select hangar_id from (
  -- 2. Table showing hangar space free on each date
  select a.*, a.hanger_capacity - isnull(b.allots_in_hangar,0) as hangar_space_free
  from #dates_by_hangar a
  left outer join (
    -- 1. Table showing number of allots in each hangar on a given date
    select date, hangar_no, sum(case when b.hangar_no is not null then 1 else 0 end) as allots_in_hangar
    from #dates a
    left outer join allot b
    on a.date between b.fromdate and b.todate
    group by date, hangar_no) b
  on a.hangar_id = b.hangar_no
  and a.date = b.date
)
group by hangar_no
having max(hangar_space_free) >= 1

答案 1 :(得分:0)

SELECT ah.* from [adding_hanger] AS ah 
INNER JOIN [allot] ON allot.hangarlocation = ah.Hanger_Location 
WHERE (allot.Fromdate < <input_from_date> OR allot.todate > <input_to_date>) 
OR (allot.Fromdate >= <input_from_date> AND allot.todate <= <input_to_date> AND ah.Hanger_Capacity <= <input_capacity>)