October 27, 2015 at 11:19 am
there are some records in exported column those are NOT Null and it is also a DATETIME data type. If you look at the attached excel Loan '123123' is actually displayed in my result set although there is only 1 row where Exported column is NULL, so this record should not have been displayed in the first place at all, when I am running the below code loan '123123' shows up.
SELECT LoanID As [Loan ID]
FROM APayments
Where DraftDate > DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND DateRequested >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND Exported IS NULL
GROUP BY LoanID
HAVING DATEDIFF(DAY, MIN(DraftDate) , MAX(DraftDate)) <=3
I believe if we could figure out why loan '123123' is showing in our result set than I think we would have our answer.
October 27, 2015 at 11:26 am
SQLPain (10/27/2015)
there are some records in exported column those are NOT Null and it is also a DATETIME data type. If you look at the attached excel Loan '123123' is actually displayed in my result set although there is only 1 row where Exported column is NULL, so this record should not have been displayed in the first place at all, when I am running the below code loan '123123' shows up.
SELECT LoanID As [Loan ID]
FROM APayments
Where DraftDate > DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND DateRequested >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND Exported IS NULL
GROUP BY LoanID
HAVING DATEDIFF(DAY, MIN(DraftDate) , MAX(DraftDate)) <=3
I believe if we could figure out why loan '123123' is showing in our result set than I think we would have our answer.
Please can I make the following suggestion (again)...as you appear to be using Getdate() as a filter. then please provide sample scripts that include specific rows that you would wish to filter out based on Getdate().......and please provide as a new script....editing earlier scripts just confuses the thread
..and please include ALL columns that re to be used in the filters....ie "Exported"
I have neither the time nor the inclination to start building scripts based on a spreadsheet. Sorry.
________________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
October 27, 2015 at 1:29 pm
Alright here you go:
Another sample Data:
Lets analyze the where(filter clause) first, one record(row) of loan 1004 should be eliminated because it has a record where Coloum Exported is NOT NULL
Loan 1005 should be eliminated because the DraftDate < getdate()
Loan 1003, one record(row) should be eliminated because RequestedDate < getdate()
CREATE TABLE TEST_TABLE_Dupe_Pay
(
LID INT,
RegPay Money,
DraftDt DATETIME,
RequestedDt DATETIME,
Exported DATETIME
)
INSERT INTO TEST_TABLE_Dupe_Pay(LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1001, '500', '2015-10-28 00:00:00.000', '2015-10-27 05:52:02.950', NULL)
INSERT INTO TEST_TABLE_Dupe_Pay(LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1001, '500', '2015-11-28 00:00:00.000', '2015-10-27 08:09:14.037', NULL)
INSERT INTO TEST_TABLE_Dupe_Pay(LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1002, '1000', '2015-11-01 00:00:00.000', '2015-10-27 09:55:09.487', NULL)
INSERT INTO TEST_TABLE_Dupe_Pay (LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1002, '1000', '2015-11-02 00:00:00.000', '2015-10-27 08:48:27.093', NULL)
INSERT INTO TEST_TABLE_Dupe_Pay (LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1002, '1000', '2015-11-03 00:00:00.000', '2015-10-27 08:51:56.153', NULL)
INSERT INTO TEST_TABLE_Dupe_Pay (LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1002, '1000', '2015-11-01 00:00:00.000', '2015-10-27 10:11:39.460', NULL)
INSERT INTO TEST_TABLE_Dupe_Pay (LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1003, '2000', '2015-11-05 00:00:00.000', '2015-10-26 10:11:39.460', NULL)
INSERT INTO TEST_TABLE_Dupe_Pay (LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1003, '2000', '2015-11-07 00:00:00.000', '2015-10-27 10:11:39.460', NULL)
INSERT INTO TEST_TABLE_Dupe_Pay (LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1004, '500', '2015-11-07 00:00:00.000', '2015-10-27 10:11:39.460', '2015-10-27 11:11:39.460')
INSERT INTO TEST_TABLE_Dupe_Pay (LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1004, '500', '2015-11-09 00:00:00.000', '2015-10-27 10:19:39.460', NULL)
INSERT INTO TEST_TABLE_Dupe_Pay (LID, RegPay, DraftDt, RequestedDt, Exported)
Values (1005, '1500', '2015-10-09 00:00:00.000', '2015-10-27 10:19:39.460', NULL)
When I am running the below code, I am getting Loan '1002, 1003, 1004' . Whereas I should be only getting Loan '1002'
SELECT LID
FROM TEST_TABLE_Dupe_Pay
Where DraftDt > DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND RequestedDt >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
AND Exported IS NULL
GROUP BY LID
HAVING DATEDIFF(d, MIN(DraftDt) , MAX(DraftDt)) <=3
Loan 1003, has RequestedDt < getdate() which should eliminate 1 record leaving behind only 1 record, which mean that there was no repetition.
Similarly Loan 1004 has 1 record (row) where the exported column has a value, which should get eliminated leaving behind only 1 record.
It seems to be working fine with Loan 1005
Are we not only grouping records which are left behind after going through the filters.
Confused ;(
October 27, 2015 at 1:47 pm
If I'm understanding this correctly ,that's just because when only 1 row is returned for a LID after the WHERE clause criteria are applied, then the 1-row LIDs trivially have a DATEDIFF between MIN and MAX that is less than 3 days, because MIN and MAX are the same date.
If that's the only problem, then adding a COUNT(*) >1 to the HAVING clause should clear it up.
Cheers!
EDIT: Also, coming up to speed on this thread, I'm wondering about the general approach. What if a LID has 3 entries, with draft dates of 11/07/2015, 12/01/2015, and 12/02/2015 (MM/DD/YYYY)? Should that loan show up because it has two entries that are within three days of each other? If so, the current approach won't work, since the difference between MIN and MAX will be greater than three days. Just wanted to make sure I understand the requirements.
October 27, 2015 at 2:06 pm
yup that was it !!!!
Thanks for clearing the min and max concept.
Viewing 5 posts - 16 through 19 (of 19 total)
You must be logged in to reply to this topic. Login to reply