简体   繁体   English

多条款条件

[英]Multiple Where Clause Conditions

I have the following syntax for Multiple Where Clause Conditions. 对于多重where子句条件,我具有以下语法。 The syntax is not being accepted by sql. sql不接受语法。 Can someone please point me in the right direction on how to get the multiple conditions working. 有人可以为我指出正确的方向,说明如何使多个条件正常工作。

   Declare @PopulationDate Datetime = '02-01-16';

DECLARE @Month1 AS integer;
DECLARE @Year1 as integer;
DECLARE @Month2 as integer;
Declare @Year2 as integer;
DECLARE @Month3 as integer;
Declare @Year3 as integer;
DECLARE @Month4 as integer;
Declare @Year4 as integer;
DECLARE @lastyear as integer;
Declare @CurrentMonth as DAte;
Set @CurrentMonth =  CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@PopulationDate)-1),@PopulationDate),101);


       set @Month1 = Month(DATEADD(MONTH, -4, @PopulationDate));
       set @Year1  = Year(DATEADD(Month, -4, @PopulationDate));
       set @Month2 = Month(DATEADD(MONTH, -3, @PopulationDate));
       set @Year2  = Year(DATEADD(MONTH, -3, @PopulationDate));
       set @Month3 = Month(DATEADD(MONTH, -2, @PopulationDate));
       set @Year3  = Year(DATEADD(MONTH, -2, @PopulationDate));
       set @Month4 = Month(DATEADD(MONTH, -1, @PopulationDate));
       set @Year4  = Year(DATEADD(MONTH, -1, @PopulationDate));

Declare @StartDate as Date 
= DATEADD(MONTH, -4, @PopulationDate);
 Declare @newdate as Date;--- pulls Start Date for probationer if in range of months being looked into. 
SET @newdate = CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@StartDate)-1),@StartDate),101);---- sets date 

with payment_year as
(
SELECT payr.ISN
      ,payr.PIN_NBR
      ,payr.PIN_SUF
      ,payr.PAYMENT_YEAR
      ,payr.RECORD_TYPE
      ,payr.PROB_LEVEL
      ,payr.PROB_OFFICER_NBR
  FROM [JVCS_CHILD_SUP_RDB].[dbo].[SCPR_PROBATION_ADA] payr
  where payr.record_type = 'mp'
  --order by PIN_NBR
)
, payment_Dates as
(
SELECT padt.ISN
      ,padt.PIN_NBR
      ,padt.PIN_SUF
      ,padt.PAYMENT_YEAR
      ,padt.RECORD_TYPE
      ,padt.PROB_LEVEL
      ,padt.PROB_OFFICER_NBR
      ,padt.PROB_DATE_ON
      ,padt.PROB_DATE_OFF
  from [JVCS_CHILD_SUP_RDB].[dbo].[SCPR_PROBATION_ADA] padt 
  where padt.RECORD_TYPE = 'pa'
  )
, Payments as
  (
  select py.isn
  ,py.PIN_NBR
  ,py.pin_suf
  ,py.PAYMENT_YEAR
  ,pd.PROB_DATE_ON
  ,pd.PROB_DATE_OFF
  ,pg.PAYMENT_GRP_INDEX
  ,pg.PAYMENT_MONTHLY_AMOUNT
  ,pg.PAYMENT_OWED

  from payment_year py
  left join payment_Dates pd on py.PIN_NBR = pd.PIN_NBR and py.PIN_SUF = pd.PIN_SUF
  right join [JVCS-CHILD-SUP-RDB].JVCS_CHILD_SUP_RDB.dbo.SCPR_PROBATION_ADA_PAYMENT_GRP pg on py.ISN = pg.isn
  where pd.PROB_DATE_OFF > @PopulationDate
  and pd.PROB_DATE_ON < @PopulationDate
  or py.PAYMENT_YEAR = Month(DATEADD(MONTH, -4, @PopulationDate));
  or py.PAYMENT_YEAR = Year(DATEADD(Month, -4, @PopulationDate));
  and py.PAYMENT_YEAR = Month(DATEADD(MONTH, -3, @PopulationDate));
  and py.PAYMENT_YEAR = Year(DATEADD(MONTH, -3, @PopulationDate));
  and py.PAYMENT_YEAR = Month(DATEADD(MONTH, -2, @PopulationDate));
  and py.PAYMENT_YEAR = Year(DATEADD(MONTH, -2, @PopulationDate));
  and py.PAYMENT_YEAR = Month(DATEADD(MONTH, -1, @PopulationDate));
  and py.PAYMENT_YEAR = Year(DATEADD(MONTH, -1, @PopulationDate));

 )

 Select * from Payments 
 order by PIN_NBR, PIN_SUF

--select month(@PopulationDate)
--select month(@StartDate)
--=Declare @PopulationDate as Date = DATEADD(Year, -1, @Populationdate); 

--Select @populationDate as PopDate, @startDate as StartDate, @Month1 as m1 , @Year1 as y1,  @Month2 as m2, @Year2 as y2, @Month3 as m3, @Year3 as y3, @Month4 as m4, @Year4 as y4

If your objective is to get the past 90 days based PAYMENT_YEAR (which actually holds a datetime and not just a year), then there's no need to compare month and year for the past 3 months. 如果您的目标是获取过去90天的PAYMENT_YEAR(实际上包含日期时间,而不仅仅是一年),则无需比较过去3个月的月份和年份。 You can use DATEADD() . 您可以使用DATEADD()

Here is a simple SQL Fiddle example . 这是一个简单的SQL Fiddle示例

With your specific query, your WHERE condition should look something like: 对于您的特定查询,您的WHERE条件应该类似于:

WHERE pd.PROB_DATE_OFF > @PopulationDate
  AND pd.PROB_DATE_ON < @PopulationDate
  AND y.PAYMENT_YEAR >= DATEADD(DAY,-90,GETDATE())

This will return records that meet all 3 conditions. 这将返回满足所有3个条件的记录。 If you need records that meet the first two conditions OR are within the past 30 days, use: 如果您需要满足前两个条件或在过去30天内的记录,请使用:

WHERE (pd.PROB_DATE_OFF > @PopulationDate
  AND pd.PROB_DATE_ON < @PopulationDate)
  OR y.PAYMENT_YEAR >= DATEADD(DAY,-90,GETDATE())
where pd.PROB_DATE_OFF > @PopulationDate
  and pd.PROB_DATE_ON < @PopulationDate
    --allows months and years to be pulled for payment data (Dominqiue A. Anderson January 2016) 
    --- second bracket makes statement true
    and((pg.PAYMENT_GRP_INDEX = @Month1 
    and py.PAYMENT_YEAR = @Year1)
    or (pg.PAYMENT_GRP_INDEX = @Month2
    and py.PAYMENT_YEAR = @Year2) 
    or (pg.PAYMENT_GRP_INDEX = @Month3
    and py.PAYMENT_YEAR = @Year3)
    or (pg.PAYMENT_GRP_INDEX = @Month4
    and py.PAYMENT_YEAR = @Year4))  --- second bracket makes statement true   
 )

--  order by pd.PIN_NBR, pd.PIN_SUF, pg.PAYMENT_GRP_INDEX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM