2013-09-26 24 views
2
SELECT CTT.BAN, `Company`, `CID`, `FName`, `MInit`, 
    `LName`, `OName`, `Address`, `City`, `State`, 
    `PostalCode`, `ActiveDate`, `ClosedDate`, `Draft`, 
    `Credit`, `BillingCycle`, `BillingFreq`, `Suspended`, 
    `Paperless` , BTT.Bal 
FROM CustomerT CTT 
    JOIN BalanceT BTT 
ON (CTT.BAN = BTT.BAN) 
WHERE `Paperless` != '1' 
    AND `BankDraft` != -1 
    AND `CreditCard` != -1 
    AND (`BillingCycle` = '1' OR `BillingCycle` = '0') 
    AND `Bal` > 2 
    AND (`AccountClosedDate` IS NULL OR 
      DATE(`AccountClosedDate`) >= (NOW() - INTERVAL 180 DAY) ) 

一切與這個查詢,但180日期peice我已經嘗試了幾個東西從這個網站,沒有運氣。我只需要在表中包括最近6個月的閉帳額。無法獲得180天的SQL日期工作

+0

是不是應該**> = **(NOW() - 間隔180天) - 我的意思是AccountClosedDate大於,不少於180天前? –

回答

3

嘗試這與DATE_ADD

SELECT CTT.BAN, `Company`, `CID`, `FName`, `MInit`, `LName`, `OName`, `Address`, `City`, `State`, `PostalCode`, `ActiveDate`, `ClosedDate`, `Draft`, `Credit`, `BillingCycle`, `BillingFreq`, `Suspended`, `Paperless` , BTT.Bal 
FROM CustomerT CTT 
JOIN BalanceT BTT ON 
(CTT.BAN = BTT.BAN) 
WHERE `Paperless` != '1' 
AND `BankDraft` != -1 
AND `CreditCard` != -1 
AND (`BillingCycle` = '1' OR `BillingCycle` = '0') 
AND `Bal` > 2 
AND (`AccountClosedDate` IS NULL OR DATE(`AccountClosedDate`) >=DATE_ADD(CURDATE(), INTERVAL -180 DAY)) 

或使用(CURDATE() - INTERVAL 180 DAY)

SELECT CTT.BAN, `Company`, `CID`, `FName`, `MInit`, `LName`, `OName`, `Address`, `City`, `State`, `PostalCode`, `ActiveDate`, `ClosedDate`, `Draft`, `Credit`, `BillingCycle`, `BillingFreq`, `Suspended`, `Paperless` , BTT.Bal 
FROM CustomerT CTT 
JOIN BalanceT BTT ON 
(CTT.BAN = BTT.BAN) 
WHERE `Paperless` != '1' 
AND `BankDraft` != -1 
AND `CreditCard` != -1 
AND (`BillingCycle` = '1' OR `BillingCycle` = '0') 
AND `Bal` > 2 
AND (`AccountClosedDate` IS NULL OR DATE(`AccountClosedDate`) >=(CURDATE() - INTERVAL 180 DAY)) 
+0

OP有'<=', not '> ='。很可能這是實際的錯誤。在那種情況下,他原來的查詢應該只是這個小小的修正。 –

+0

@AndreiTanas OP問***我只需要在表格中包括最近6個月的已關閉帳戶***,並且也完全看到OP編輯的問題 –

+0

,這就是爲什麼他在我評論我相信後編輯了它 –