I have what appears to be a corrupt index?
Here is what is happening. I have two table-functions which the first is a set of cases and the second is a set of aware dates. These two sets have a 1 (case) to 0 or 1 (aware date) relationship. Normally I query them like;
SELECT c.CaseID, a.AwareDate
FROM Cases(#date) AS c
LEFT JOIN AwareDates(#date) AS a ON c.CaseID = a.CaseID;
The trouble is that not all of the rows from AwareDates which match seem to be JOIN'd. If I add a join hint, they then do. say;
SELECT c.CaseID, a.AwareDate
FROM Cases(#date) AS c
LEFT MERGE JOIN AwareDates(#date) AS a ON c.CaseID = a.CaseID;
What I notice from the query plan is that adding the join hint adds a sort of the AwareDate data before the join which is not there otherwise. Also, the query planner flips the join to a RIGHT OUTER JOIN when there is no hint, and of course keeps the LEFT JOIN where the hint is present.
I've done the following with no errors detected;
DBCC UPDATEUSAGE (0) WITH INFO_MESSAGES, COUNT_ROWS;
EXECUTE sp_updatestats 'resample';
DBCC CHECKDB (0) WITH ALL_ERRORMSGS, EXTENDED_LOGICAL_CHECKS;
I'm stumped... any ideas?
Here are the UDF definitions
ALTER FUNCTION dbo.Cases( #day date ) RETURNS TABLE
WITH SCHEMABINDING
AS RETURN (
SELECT
CaseID -- other 42 columns ommitted
FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY CaseID ORDER BY UpdateDate DESC, UpdateNumber DESC) AS RecordAge,
CaseID,
Action
FROM
dbo.CaseAudit
WHERE
convert(date,UpdateDate) <= #day
) AS History
WHERE
RecordAge = 1 -- only the most current record version
AND isnull(Action,'') != N'DEL' -- only include cases that have not been deleted
)
ALTER FUNCTION dbo.AwareDates( #day date ) RETURNS TABLE
WITH SCHEMABINDING
AS RETURN (
WITH
History AS (
SELECT row_number() OVER (PARTITION BY CaseID, ContactID ORDER BY UpdateDate DESC, UpdateNumber DESC) AS RecordAge,
CaseID, InfoReceived, ReceiveDate, ResetClock, Action
FROM dbo.ContactLogAudit WITH (NOLOCK)
WHERE convert(date,UpdateDate) <= #day
),
Notes AS (
SELECT
CaseID,
convert(date,ReceiveDate,112) AS ReceiveDate,
ResetClock
FROM History
WHERE RecordAge = 1 -- only the most current record version
AND isnull(Action,'') != N'DEL' -- only include notes that have not been deleted
AND InfoReceived = N'Y' -- only include notes that have Info Rec'd checked
AND len(ReceiveDate) = 8 AND isnumeric(ReceiveDate) = 1 AND isdate(ReceiveDate) = 1 -- only include those with a valid aware date
),
Initials AS (
SELECT CaseID, min(ReceiveDate) AS ReceiveDate
FROM Notes
GROUP BY CaseID
),
Resets AS (
SELECT CaseID, max(ReceiveDate) AS ReceiveDate
FROM Notes
WHERE ResetClock = N'Y'
GROUP BY CaseID
)
SELECT
i.CaseID AS CaseID,
i.ReceiveDate AS InitialAwareDate, -- the oldest valid aware date value (must have AE Info Reveived checked and a received date)
coalesce(r.ReceiveDate,i.ReceiveDate) AS AwareDate -- either the newest valid aware date value with the Reset Clock checked, otherwise the initial aware date value
FROM Initials AS i
LEFT JOIN Resets AS r
ON i.CaseID = r.CaseID
);
I have further found that if I drop the "WITH (NOLOCK)" table hint, I get correct results. Also if add a join hint to the AwareDates UTF or even add a COLLATE Latin1_General_BIN on the LEFT JOIN relation between Initials and Resets.
Query plan row counts -- without join hint (broken)
Cases { Actual: 25,891, Estimate: 19,071.9 }
AwareDates { Actual: 24,693, Estimated: 1,463.09 }
Initials { Actual: 24,693, Estimated: 1,463.09 }
Rests { Actual: 985, Estimated: 33.2671 }
AwareDates matches 8,108 of the Cases rows in the join'd result-set
Query plan row counts -- with join hint (working)
Cases { Actual: 25,891, Estimate: 19,071.9 }
AwareDates { Actual: 24,673, Estimated: 1,837.67 }
Initials { Actual: 24,673, Estimated: 1,837.67 }
Rests { Actual: 982, Estimated: 42.6238 }
AwareDates matches 24,673 of the Cases rows in the join'd result-set
I have further whittled down the scope of the issue. I can;
SELECT * FROM AwareDate(#date);
and
SELECT * FROM AwareDate(#date) ORDER BY CaseID;
With different row counts.
You don't specify the specific version of SQL (##version), but this seems suspiciously like a bug that was fixed in Cumulative Update 6 for SQL 2008 R2 (apparently it also applies to SQL 2008).
KB 2433265
FIX: You may receive an incorrect result when you run a query that uses the
ROW_NUMBER function together with a left outer join in SQL Server 2008
The example in the article specifies DISTINCT. The article, however, is worded ambiguously -- it's not clear whether you NEED a distinct or if DISTINCT is one of the triggers.
Your example doesn't have a distinct like the article, but it appears modified for the sake of asking the question(i.e. 42 columns missing). Is there a distinct? Also in the AwareDates udf by the time i get down to the Initials CTE you do a GROUP BY which could have the same effect as a DISTINCT.
UPDATE
#Dennis from your comment I still can't tell if you're using SQL 20080 or 2008 R2.
If you're running 2008, the KB article says "The fix for this issue was first released in Cumulative Update 11 for SQL Server 2008 Service Pack 1." So, post SP1.
On the other hand, if you're using SQL 2008 R2, you are correct that this was fixed in CU 6, which was part of SP1. But this bug appears to have resurfaced. Look at Cumulative update package 4 for SQL Server 2008 R2 Service Pack 1 -- released post SP1.
970198 FIX: You receive an incorrect result when you run a
query that uses the row_number function in SQL Server 2008
or in SQL Server 2008 R2
In the associated KB article MS dropped the reference to distinct:
Consider the following scenario. You run a query against a table that has a
clustered index in Microsoft SQL Server 2008 or in Microsoft SQL Server 2008
R2. In the query, you use the row_number function. In this scenario, you
receive an incorrect result when a parallel execution plan is used for the
query. If you run the query many times, you may receive different results.
This seems to confirm my earlier reading of KB 2433265 -- the phrasing suggests distinct is just one of many conditions that can cause the behavior. It seems that a parallel execution plan is the culprit this time around.
Related
I have the following tables:
reservas_tanatosalas
I have the following query:
SELECT r.cod_reserva, MAX(r.hora_fin_utc) FROM reservas_tanatosalas r GROUP BY r.cod_tanatosala
Results: Is showing the right max value but the wrong cod_reserva. Why?
cod_reserva MAX(r.hora_fin_utc)
7 9999999999
6 9999999999
What I want to get?:
cod_reserva MAX(r.hora_fin_utc)
7 9999999999
8 9999999999
UPDATE
cod_reserva could change to varchar in the future hence is not an option to me use max on it.
Do not use GROUP BY for this. Your query is malformed and won't run in the most recent versions of MySQL (with the default settings) or almost any database.
select r.*
from r
where r.hora_fin_utc = (select max(r2.hora_fin_utc)
from reservas_tanatosalas r2
where r2.cod_tanatosala = r.cod_tanatosala
);
Think about the problem as a filtering problem, not an aggregation problem. You want to filter the data so only the most recent row shows up in the result set.
If performance is an issue, then you want an index on (cod_tanatosala, hora_fin_utc).
This is the your query:
This is the query:
SELECT r.cod_reserva, MAX(r.cod_tanatosala) FROM reservas_tanatosalas r
GROUP BY r.cod_tanatosala
HAVING MAX(r.hora_fin_utc)
This is saying:
produce one row for each value of cod_tanatosala
return the maximum value of cod_tanatosala
ERROR HERE: Don't know what to do with cod_reserva. It is not the argument to an aggregation function or in the GROUP BY.
The HAVING is simply stating that MAX(r.hora_fin_utc) is neither 0 nor NULL. Not very useful.
You are grouping the resultset using a different column, while your select statement refers to a different one. Following should definitely work, please let me know if it doesn't:
SELECT
r.cod_reserva, MAX(r.hora_fin_utc)
FROM
reservas_tanatosalas r
GROUP BY
r.cod_reserva
HAVING
MAX(r.hora_fin_utc)
I ran the same query as yours,
SELECT MAX(r.cod_reserva), MAX(r.hora_fin_utc) FROM reservas_tanatosalas r GROUP BY r.cod_tanatosala
But I used an aggregate function MAX() on r.cod_reserva as well because without it gives an error "this is incompatible with sql_mode=only_full_group_by" and I got it working, you can test it with MAX(r.cod_reserva).
Firstly in most databases including new version of Mysql this code will issue error as you are grouping by a column that is not part of select. Following query will give you what you are after for the dataset you have shown
select (select t.cod_reserva from reservas_tanatosalas t
where t.hora_fin_utc = a.max_hora_fin_utc
and a.cod_tanatosala = t.cod_tanatosala) cod_reserva,
a.max_hora_fin_utc
from (
SELECT x.cod_tanatosala, MAX(x.hora_fin_utc)max_hora_fin_utc
FROM reservas_tanatosalas x group by x.cod_tanatosala
)a;
Pretty new to MySQL here. I have the following query that isn't quite working the way I want: **SELECT round(sum(temp_pop.pop_total * demographics.coefficient)) as demand, pop_proj.pop_total from pop_proj, d.age, d.coefficient, d.educationalattainment from demographics d JOIN (SELECT year,sum(pop_total), age FROM pop_proj GROUP BY year, age) AS temp_pop WHERE d.age = CASE WHEN temp_pop.age < 18 then '00 to 17' WHEN temp_pop.age > 64 then '65 to 80+' ELSE '18 to 64' end;**
The sources are the subquery shown above in the syntax which I'm trying to join with a table called "demographics" with only three columns that shows education level (educational attainment), an age range (age) - shown in the case statement, and a coefficient, used in the calculation at the beginning of the query. The pop_proj table provides a year, age, and population total (pop_total column). I'm trying to use temp_pop as an alias for the subquery. I'm fairly sure the case is written out correctly. However, when I run the query, it tells me this: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql> SELECT round(sum(temp_pop.pop_total * demographics.coefficient)) as deman' at line 1
I also want to group the results by year and education level, I just haven't added that in there yet.
Previously I had it written only slightly differently and it was telling me it didn't recognize the column name pop_total, but haven't gotten a result without an error yet. I may be totally off on how write this query, but hoping I'm getting close. I would appreciate some help! Thanks in advance!
It's difficult to understand what you're really after but this may get you a step closer.
The below makes the assumption that you're relating demographics to pop_total only on the contrived age range string in your CASE which is suspicious and may not be valid.
This doesn't attempt to group the results, but that should be straightforward once you can see a working detailed output that meets your needs.
SELECT d.age, d.coefficient, d.educationalattainment, temp_pop.pop_total
FROM demographics d JOIN (
SELECT `year`, age, sum(pop_total) as pop_total
FROM pop_proj
GROUP BY `year`, age
) temp_pop ON d.age =
CASE WHEN temp_pop.age < 18 THEN '00 to 17'
WHEN temp_pop.age > 64 THEN '65 to 80+'
ELSE '18 to 64' END
;
I am working on project management tool and while generating reports through admin end the loading time for the data from the database is very much > 5 minutes. There are few points which I know would help me to increase the performance but right now I need to have the help in SELECT query
SELECT timesheet_client.organisation as client_name, timesheet_project.title as project_name,
timesheet_task.name as task, CONCAT(timesheet_user.first_name, ' ', timesheet_user.last_name) as resource_name,
timesheet_user.bill_factor, timesheet_client.client_type, sum(spent) as spent, sum(delivered_hours) as delivered_hours,
sum(billable_hours) as billable_hours, comments, color, lock_color, updated_by, updated_by_date, timesheet_user_psdb.grp_id,
timesheet_user_psdb.client_id, timesheet_user_psdb.proj_id, timesheet_user_psdb.task_id, timesheet_user_psdb.uid,
timesheet_user_grp.grp_name
FROM timesheet_user_psdb,timesheet_user, timesheet_client,
timesheet_project,timesheet_task,timesheet_user_grp
WHERE timesheet_user.username=timesheet_user_psdb.uid and
timesheet_client.client_id=timesheet_user_psdb.client_id and timesheet_project.proj_id=timesheet_user_psdb.proj_id and
timesheet_task.task_id = timesheet_user_psdb.task_id and timesheet_user_grp.grp_id=timesheet_user_psdb.grp_id and month =3
AND year = 2017 and month!='' and timesheet_user_psdb.client_id=326
GROUP BY timesheet_user_psdb.task_id,timesheet_user_psdb.uid
ORDER BY timesheet_client.client_type desc,timesheet_client.organisation,timesheet_user_psdb.proj_id,
timesheet_user_psdb.task_id,timesheet_user.uid,timesheet_user_psdb.task_id;
I have already used an index on all the primary keys.
EXPLAIN Output:
Help for this would be highly appreciable.
Give this a try:
SELECT
TC.ORGANISATION AS CLIENT_NAME,
TP.TITLE AS PROJECT_NAME,
TT.NAME AS TASK,
CONCAT(TU.FIRST_NAME, ' ', TU.LAST_NAME) AS RESOURCE_NAME,
TU.BILL_FACTOR,
TC.CLIENT_TYPE, SUM(SPENT) AS SPENT, -- You should specify which table this comes from
SUM(DELIVERED_HOURS) AS DELIVERED_HOURS, -- You should specify which table this comes from
SUM(BILLABLE_HOURS) AS BILLABLE_HOURS, -- You should specify which table this comes from
COMMENTS, -- You should specify which table this comes from
COLOR, -- You should specify which table this comes from
LOCK_COLOR, -- You should specify which table this comes from
UPDATED_BY, -- You should specify which table this comes from
UPDATED_BY_DATE, -- You should specify which table this comes from
TUP.GRP_ID,
TUP.CLIENT_ID,
TUP.PROJ_ID,
TUP.TASK_ID,
TUP.UID,
TUG.GRP_NAME
FROM
TIMESHEET_USER AS TU
LEFT OUTER JOIN
TIMESHEET_USER_PSDB AS TUP
ON TU.USERNAME = TUP.UID
LEFT OUTER JOIN
TIMESHEET_USER_GRP AS TUG
ON TUP.GRP_ID = TUG.GRP_ID
LEFT OUTER JOIN
TIMESHEET_CLIENT AS TC
ON TUP.CLIENT_ID = TC.CLIENT_ID
LEFT OUTER JOIN
TIMESHEET_PROJECT AS TP
ON TUP.PROJ_ID = TP.PROJ_ID
LEFT OUTER JOIN
TIMESHEET_TASK TT
ON TUP.TASK_ID = TT.TASK_ID
WHERE
MONTH = 3 AND -- You should specify which table this comes from
YEAR = 2017 AND -- You should specify which table this comes from
MONTH != '' AND -- You should specify which table this comes from
TUP.CLIENT_ID = 326
GROUP BY
TUP.TASK_ID,
TUP.UID
ORDER BY
TC.CLIENT_TYPE DESC,
TC.ORGANISATION,
TUP.PROJ_ID,
TUP.TASK_ID,
TU.UID,
TUP.TASK_ID;
Doing the EXPLAIN should shed some light on it.
You might see a performance gain by doing the table joins explicitly in the FROM clause instead of inside the WHERE (https://dev.mysql.com/doc/refman/5.7/en/join.html). By doing explicit joins (e.g. LEFT OUTER, etc...) you will not only improve the readability of the query, but may be able to use less expensive joins where needed. This also affects how the query is executed as each clause is executed in a specific order (MySQL query / clause execution order).
I have two servers running MySQL. Both are on windows. One, is my local machime (Windows 7, MySQL 5.6.25, 32bit) and the other is my production webserver (Windows 2012, MySQL 5.7.11-log, 64bit (that's what show variables showed me).
The data is identical between the two. I backed the data up from the windows 7 (using MySQL Workbench) and restored it on the 2012 machine.
I am running a query on both machine but I am getting different results. I have two tables, projects and projectsnotes with a 1:m relationship between them related on projects.id to projectsnotes.idProject. Each note is marked with a date (dComment). The goal of the query is to retrieve project information and the latest comment only.
Here's the query:
select space(1) as cAction,
p.id,
p.iNum,
p.cStatus,
p.cName,
p.cDesc,
ifnull(pl.cNickName, 'UNASSIGNED') as cProjectLeader,
IFNULL(concat (
date_format(pn.dComment, '%Y-%m-%d'),
': ',
pn.cComment
), '') as cComment,
date_format(p.dRequested, '%Y-%m-%d') as dRequested,
date_format(p.dRequired, '%Y-%m-%d') as dRequired,
format(p.nPercentComplete, 2) as nPercentComplete,
p.tLastUpdated,
p.bCompleted,
p.idProjectLeader
from projects p
left outer join projectleaders pl on p.idProjectLeader = pl.id
left outer join (
select idProject,
dComment,
cComment
from projectnotes
order by dComment desc,
tLastUpdated desc
) pn on p.id = pn.idProject
where p.cInstallCode = 'ITM'
and cStatus in ('Pending', 'Active', 'On Hold', 'Completed', 'Cancelled')
and bCompleted = 0
group by iNum
order by iNum;
Now, here's the weird part. When I run this on my Windows 7 machine, I get the right value for cComment. Specifically:
2017-03-28: Text from note replace
That is the latest note. When I run it on the 2012 server:
2016-05-17: Text from this note replaced too
If I run the subquery alone on the 2012 server, I get the right values (namely, a list of all the notes in the reverse order.
Oh, and this note is neither the first nor the last in the notes for this project.
So I am honestly wondering what is going on. Any thoughts on this would be greatly appreciated.
Thanks in advance.
This is expected behavior.
select ...
from projects p
left outer join projectleaders pl on p.idProjectLeader = pl.id
left outer join (...) pn on p.id = pn.idProject
where ...
group by iNum
order by iNum;
Due to MySQL's peculiar handling of GROUP BY, it will not report an error on this query. However, you must keep in mind that, since you use no aggregates, and the GROUP BY will eliminate lots of rows, the rows that are kept in the final result set are determined by rather obscure criteria...
For example:
SELECT a,b FROM t GROUP BY a
Which b will be returned? In some MySQL versions, this will be the first value of b that is found in table t. If table t is ordered in a certain way, this can be exploited. But I would definitely not trust that behavior to stay unchanged between versions... Also, remember MySQL is free to change your join order...
OK. I think I have a solution to this. Instead of doing it with a join I wrote a function that returned the value I needed as follows:
DROP FUNCTION if exists f_lastprojectnote;
DELIMITER $$
CREATE FUNCTION f_lastprojectnote(tidProject varchar(36))
RETURNS varchar(1000) DETERMINISTIC
BEGIN
DECLARE cRetVal VARCHAR(1000);
SELECT concat(date_format(pn.dComment, '%Y-%m-%d'), ': ', pn.cComment) INTO cRetVal
FROM projectnotes pn
WHERE idProject = tidProject
ORDER BY dComment DESC, tLastUpdated DESC
LIMIT 1;
RETURN cRetVal;
END$$
DELIMITER ;
It works...
Why is it that when I run the query in Management Studio with the following where clause, it returns the data correctly when the cash_date equals the parameter, FromDate.
FILMTRAN.cash_date >= (#FromDate) AND
However, when I copy the same query to SSRS it no longer includes the data where the #FromDate equals the cash_date. The user enters in the parameter in SSRS. I even did a test where I displayed the full date including the time and did an if statement that returned "yay" if they matched or "no" if they did not. Even according to SSRS, they match, but than why is my data not being returned when it occured on the same day?! I am dumbfounded this is not working, when it clearly works in management studio.
I have even tried adding a dateadd function into my sql statement, which again responds correctly in management studio but not ssrs.
Edit: I tested the >= using a much simpler 3 line query and it worked as intended but it is still not working for my query, which works in management studio but not SSRS. So now I am thinking there is some very specific bug associated with something in my longer query which I have attached. To test my code in management studio I declare the variables as seen below. However, I delete this code when I import my query into SSRS and instead create the parameters within SSRS(just as I have always done, with no problems).
DECLARE #FromDate AS DATETIME
SET #FromDate = '1-Oct-2013'
DECLARE #ToDate AS DATETIME
SET #ToDate = '8-Apr-2015'
DECLARE #Loan AS VARCHAR(20)
SET #Loan = 'TAX_B'
SELECT distinct LOANMAST.loan,
CARDINDX.DESCRIPTION AS BorrowerName,
CARDINDX.contact_name,
CARDINDX.in_care_of,
--loan_ipt.interest,
CARDINDX.mail_address1,
CARDINDX.mail_address2,
CARDINDX.mail_city,
CARDINDX.mail_state,
CARDINDX.mail_country,
LOANMAST.closed_date,
PROPERTY.STATE,
CARDINDX.MAIL_ZIP_CODE,
PROPERTY.address1,
PROPERTY.address2,
PROPERTY.city,
PROPERTY.ZIPCODE,
PROPERTY.country,
FILMTRAN.cash_date,
CASE FILMTRAN.TRAN_SUBTYPE WHEN 'REGINST' THEN 'Regular Collections'
WHEN 'PAC' THEN 'PAC'
WHEN 'CORREST' THEN 'Group Collection'
WHEN 'ESCROWTAX' THEN 'Tax Disbursement'
WHEN 'SUNDRY' THEN 'Tax Refund'
WHEN 'FULL PAY' THEN 'Payoff'
END AS TRANSUBTYPE,
FILMTRAN.TAX_ESCROW,
FILMTRAN.TRANSACTION079,
JK.taxescrowbalance,
CASE FILMTRAN.TRANSACTION079 WHEN 'COLLECTION' THEN FILMTRAN.TAX_ESCROW
WHEN 'DISBURSEMENT' THEN 0
END AS TAXESCROWCOLL,
CASE FILMTRAN.TRANSACTION079 WHEN 'DISBURSEMENT' THEN FILMTRAN.TAX_ESCROW *-1
WHEN 'COLLECTION' THEN 0
END AS TAXESCROWDIS
FROM LOANMAST LEFT OUTER JOIN BORROWER ON LOANMAST.loan=BORROWER.loan
LEFT OUTER JOIN LOANCOLL ON LOANMAST.loan=LOANCOLL.loan
LEFT OUTER JOIN PROPERTY ON LOANCOLL.code=PROPERTY.prop_code
LEFT OUTER JOIN BALHIST ON BALHIST.loan=LOANMAST.loan
LEFT OUTER JOIN CARDINDX ON BORROWER.SHORT_NAME=CARDINDX.SHORT_NAME
LEFT OUTER JOIN LOAN_IPT ON LOAN_IPT.short_name=CARDINDX.short_name
LEFT OUTER JOIN FILMTRAN ON FILMTRAN.loan=LOANMAST.loan
JOIN (
SELECT distinct FILMTRAN.loan, FILMTRAN.cash_date, FILMTRAN.transaction079 ,FILMTRAN.tax_escrow,
sum(FT.taxescrowcoll)+sum(FT.taxescrowdis)+isnull(BH.esc_tax_bal_p, 0) AS "TaxEscrowBalance"
from
FILMTRAN
LEFT OUTER JOIN (SELECT loan, accounting_date, esc_tax_bal_p,
rank () over (order by accounting_date desc) AS date_rank
FROM BALHIST
where loan = #Loan) BH ON BH.loan=FILMTRAN.loan
JOIN
(
SELECT loan, cash_date,
FILMTRAN.transaction079,
CASE FILMTRAN.TRANSACTION079 WHEN 'COLLECTION' THEN TAX_ESCROW
WHEN 'DISBURSEMENT' THEN 0
END AS TAXESCROWCOLL,
CASE FILMTRAN.TRANSACTION079 WHEN 'DISBURSEMENT' THEN TAX_ESCROW *-1
WHEN 'COLLECTION' THEN 0
END AS TAXESCROWDIS FROM FILMTRAN
WHERE loan = #Loan)
FT ON FT.loan=FILMTRAN.loan
where FT.cash_date <= FILMTRAN.cash_date
AND (BH.date_rank ='1' OR BH.accounting_date IS NULL)
AND (len(FT.transaction079) <= len(FILMTRAN.transaction079) OR FT.cash_date < FILMTRAN.cash_date)
GROUP BY FILMTRAN.loan,FILMTRAN.cash_date, FILMTRAN.transaction079, FILMTRAN.TAX_ESCROW, BH.esc_tax_bal_p) JK ON JK.loan=FILMTRAN.loan AND JK.cash_date = FILMTRAN.cash_date AND JK.transaction079=FILMTRAN.transaction079
WHERE
FILMTRAN.RVRS_REASON <> 'ERROR_CORR' AND
Loancoll.prim_ind = 'Y' AND
LOANMAST.loan_status <> '1_INQUIRY' AND
LOANMAST.loan_status <> '2_APP_ISS' AND
LOANMAST.loan_status <> '3_APP_ACC' AND
LOANMAST.loan_status <> '4_APPROVED' AND
LOANMAST.loan_status <> '5_COMMITTED' AND
LOANMAST.loan_status <> '9_DELN_SLF' AND
LOANMAST.loan_status <> '9_REJ_BRWR' AND
FILMTRAN.cash_date >= (#FromDate)
AND FILMTRAN.cash_date <= (#ToDate)
AND LOANMAST.loan = #Loan
AND FILMTRAN.tax_escrow <> 0
You have a date formatting issue.
Hopefully you are calling a stored procedure to generate your dataset.
If not, you should be!
To see what is happening you might want to output the dates that are getting passed through into a table - then have a look at that table once you have run the report. Obviously you will drop this table once your debugging is complete.
Make sure you store the dates in this table as text rather than DateTime.
Have a look at the diffrences when you run from SSRS and Management Studio.
I was unable to replicate this issue. In my system, using SSRS-2008, the results in SSRS are the same as in SQl-Server. I'm using a where clause with an SSRS parameter and a >= operator. I tested with both Text and Date/Time parameter types, and both worked fine.
I would suggest that you make sure that the datasource is the same as what you are querying in SQL server. Also, try entering the date without a time, if you are entering a time in the parameter.
For further assistance, please post your complete query (or a facsimile) and information on your table and parameter settings in SSRS.
EDIT:
Just taking a look at your query here, and I have a feeling that this may be the problem area:
where FT.cash_date <= FILMTRAN.cash_date
AND (BH.date_rank ='1' OR BH.accounting_date IS NULL)
AND (len(FT.transaction079) <= len(FILMTRAN.transaction079) OR FT.cash_date < FILMTRAN.cash_date)
Possibly could be stemming from confusion over the use of Filmtran without aliasing within the subquery, when it is already included in the main query? I would run the query without the two sub-queries in the From clause and see if the dates are as you'd expect, the add back in the subqueries one by one to determine the location of the issue.
Ok so the solution to my problem was dumber than I would like to admit. So in addition to stating my solution I will also suggest potential problems/solutions that relate to dates in SSRS/Management Studio:
Check the filters on the dataset within SSRS! This was my problem since I used a report template that had filters already on the dataset, even though I usually do all filters by hand in SQL management studio. (See picture below)
Make sure the time is correct, not just the date. For example, 10/1/2013 might appear but the actual value behind that date is something like 10/1/2013 5:43 PM. So then doing <= will not work if you are trying to include values that happened after 10/1/2013 5:43 PM but before 10/2/2013.
Make sure the regional date settings are the same for SSRS and SQL Server. For example, if one machine views dates in the European format of DD/MM/YYYY then it could cause problems if another user's machine is expecting the date to be in MM/DD/YYYY format.