多个条款条款

时间:2015-12-30 16:30:42

标签: sql sql-server tsql where-clause

我有多个Where子句条件的以下语法。 sql不接受语法。有人可以指出我正确的方向,如何让多个条件工作。

   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

2 个答案:

答案 0 :(得分:0)

如果您的目标是在过去90天内获得PAYMENT_YEAR(实际上持有日期时间而不仅仅是一年),则无需比较过去3个月的月份和年份。您可以使用DATEADD()

Here is a simple SQL Fiddle example

使用您的特定查询,您的WHERE条件应如下所示:

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

这将返回满足所有3个条件的记录。如果您需要符合前两个条件或在过去30天内的记录,请使用:

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

答案 1 :(得分:0)

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