Showing posts with label returns. Show all posts
Showing posts with label returns. Show all posts

Monday, March 19, 2012

Mysterious mystery -- 2002-11-08/2002-11-09

I have function that returns a table of information about properties. The
data comes from three different tables -- addresses (called PropertyID),
property characteristics, and events concerning those properties (sales,
appraisals, etc.), plus a table that maps one representation of property
types into another. The records are selected on the basis of location
(longitude & latitude), property type, event type, and a range of
event dates (upper and lower date specified). There are tens of millions
of records of all types, and almost any location, property type, event type
and date range will yield records.

The heart of it is a cursor that selects records from joins on this basis:

SELECT <a bunch of fields>
FROM Property d
JOIN PropTypeMap ptm ON ptm.PropertyTypeID = d.PropertyTypeID
JOIN PropertyID a ON a.PropID = d.PropID
JOIN Event e1 ON e1.PropID = d.PropID
LEFT OUTER JOIN Event e2 ON e2.PropID = d.PropID
WHERE
d.LastSaleDate >= @.LoDate
AND a.GeoLongitude BETWEEN @.LowerLon AND @.UpperLon
AND a.GeoLatitude BETWEEN @.LowerLat AND @.UpperLat
AND ptm.PropCategory = @.PropType
AND a.GeoMatch <= @.MinGeoQuality

AND e1.EventTypeID = @.SaleEventType
AND e1.TransactionType = 'R'
AND e1.EventDt BETWEEN @.LoDate AND @.HiDate
AND e1.EventAmt > 0

AND e2.EventTypeID = @.AssessmentEventType
AND e2.EventDt <= @.HiDate
AND e2.EventAmt > 0

Each property has one PropertyID record, one Property record, and N Event
records (average perhaps five).

What is the mystery? If @.HiDate, which is the upper end of the time window,
is 2002-11-08 or earlier, nothing is returned. If it's 2002-11-09 or later,
oodles of records are found. I get the same query plan for either one, and
based on the content of the data, they should return almost exactly the same
set of records -- exactly the same set in almost all cases, in fact.

Is 2002-11-08/09 some sort of magic dividing point? I have replicated this
on the large database and on a smaller test version on another SQL Server.
(SQL Server 2000) I dropped the indexes and tried it, and
the same thing happened. This is driving me crazy![posted and mailed, please reply in news]

Jim Geissman (jim_geissman@.countrywide.com) writes:
> What is the mystery? If @.HiDate, which is the upper end of the time
> window, is 2002-11-08 or earlier, nothing is returned. If it's
> 2002-11-09 or later, oodles of records are found. I get the same query
> plan for either one, and based on the content of the data, they should
> return almost exactly the same set of records -- exactly the same set in
> almost all cases, in fact.
> Is 2002-11-08/09 some sort of magic dividing point? I have replicated
> this on the large database and on a smaller test version on another SQL
> Server. (SQL Server 2000) I dropped the indexes and tried it, and the
> same thing happened. This is driving me crazy!

Without access to the database, or a script that reproduces the problem,
there is much left for me, but to guess.

And the obvious guess is that the rows you find are from 2002-11-08, but
have a time porttion, and thus are > '2002-11-08 00:00:00'.

However, I notice one thing with your query which may not be correct:

LEFT OUTER JOIN Event e2 ON e2.PropID = d.PropID
WHERE ...
AND e2.EventTypeID = @.AssessmentEventType
AND e2.EventDt <= @.HiDate
AND e2.EventAmt > 0

The idea with a left outer join is that you include rows in the left
table, even if there is no matching rows in the right table. The
columns in the right table are then set to NULL.

Now, the FROM clause is (logically) evaluated before the WHERE clause,
which acts on the filter on the table defined by the FROM clause. This
means that you will filter away those rows with NULL in e2.EventTypeID
that comes from the outer join. Your join is now effectively an inner
join.

There are two ways to handle this:
1) Add conditions with OR clauses or coalesce to handle NULL in the WHERE
clause.
2) Move the conditions to the ON clause.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||Thanks, Erland. You hit the nail on the head. I was naively assuming that
the right side of the Left Outer Join didn't matter, but when I removed the
second condition in the excerpt below, the records all appeared. As you
said, it was because the FROM was being evaluated before the WHERE. Moving
the condition to ON fixed it. Thanks. I wonder if I have any other
scripts that make this mistake...

> However, I notice one thing with your query which may not be correct:
> LEFT OUTER JOIN Event e2 ON e2.PropID = d.PropID
> WHERE ...
> AND e2.EventTypeID = @.AssessmentEventType
> AND e2.EventDt <= @.HiDate
> AND e2.EventAmt > 0
> The idea with a left outer join is that you include rows in the left
> table, even if there is no matching rows in the right table. The
> columns in the right table are then set to NULL.
> Now, the FROM clause is (logically) evaluated before the WHERE clause,
> which acts on the filter on the table defined by the FROM clause. This
> means that you will filter away those rows with NULL in e2.EventTypeID
> that comes from the outer join. Your join is now effectively an inner
> join.
> There are two ways to handle this:
> 1) Add conditions with OR clauses or coalesce to handle NULL in the WHERE
> clause.
> 2) Move the conditions to the ON clause.|||Jim Geissman (jim_geissman@.countrywide.com) writes:
> Thanks, Erland. You hit the nail on the head. I was naively assuming
> that the right side of the Left Outer Join didn't matter, but when I
> removed the second condition in the excerpt below, the records all
> appeared. As you said, it was because the FROM was being evaluated
> before the WHERE. Moving the condition to ON fixed it. Thanks. I
> wonder if I have any other scripts that make this mistake...

A very common mistake to make. I recall that I did it myself a few
times when I switched to the new syntax from the old *= crap.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Friday, March 9, 2012

MySQL function equivalent

Hi,
In MySQL, from_unixtime(lonvalue) returns the date format in 'YYYY-MM-DD HH:MM:SS'. Is there any equivalent available in SQL Server, to view the date in man readable format, when the input is an long value in milliseconds.
Eg :
SELECT FROM_UNIXTIME(875996580);
Thanks,
Smitha
Take a look at the CONVERT function in Books Online. It has different styles that you can use. Optionally you can format the string using various datepart calls.

select CONVERT(varchar, CURRENT_TIMESTAMP, 121) -- odbc canonical format|||select CONVERT(varchar, '875996580', 121);
Output : 875996580

As I had said earlier, the input is a long value (in milliseconds)|||There is no native function that will understand the Unix format directly. You have to write your own TSQL UDF to do the conversion based on the format.|||

isnt 'from_unixtime(longvalue)' return a date that is longvalue seconds from 1/1/1970 ? not milleseconds?

select dateadd(s, 875996580, '1/1/1970')
yields:
1997-10-04 20:23:00.000

Wednesday, March 7, 2012

My store procedure returns 2 grids HELP

How do I access the 2nd grid/table in my report?
Basically I have a SP that has a SUM in one grid. The second grid
cotains the data I want to display in RS. Is there a way to get to the
2nd grid without altering the store procedure(Its being used on another
report)?
regards,
Stas K.I just found a similar topic where it says I cannot do this.
Any ideas?|||No, RS can only take a single resultset.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Sorcerdon" <sorcerdon@.gmail.com> wrote in message
news:1136923749.664965.81380@.z14g2000cwz.googlegroups.com...
> How do I access the 2nd grid/table in my report?
> Basically I have a SP that has a SUM in one grid. The second grid
> cotains the data I want to display in RS. Is there a way to get to the
> 2nd grid without altering the store procedure(Its being used on another
> report)?
> regards,
> Stas K.
>

Saturday, February 25, 2012

My report wont work :-) with a date param

Hi All,
I have inherited a report that at the Dataset level works fine. It takes a
date param eg. 02-Dec-2005 and returns 3 rows as I would expect.
The report page has the date param defined as Datetime and when I enter the
date ie. 02-Dec-2005 it returns 0 rows. I have tried inputting different date
formats
to no avail. kinda puzzled.
Any clues as to the cause of the problem.
Thanx in advancewell,i had kind of this problem.
I never use parm as datetime always as string and inside the dataset i use
CONVERT(datetime,@.FromTime,103) it works 4 me...
"Scotchy" wrote:
> Hi All,
> I have inherited a report that at the Dataset level works fine. It takes a
> date param eg. 02-Dec-2005 and returns 3 rows as I would expect.
> The report page has the date param defined as Datetime and when I enter the
> date ie. 02-Dec-2005 it returns 0 rows. I have tried inputting different date
> formats
> to no avail. kinda puzzled.
> Any clues as to the cause of the problem.
> Thanx in advance
>|||It sounds to me like your query parameter is not hooked up to your report
parameter. In the dataset click on the ..., parameters tab. Your query
parameters should be listed on the left and the report parameter they map to
on the right.
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Scotchy" <Scotchy@.discussions.microsoft.com> wrote in message
news:B64FE999-9EA6-40C0-896E-984BBDC4BDC4@.microsoft.com...
> Hi All,
> I have inherited a report that at the Dataset level works fine. It takes a
> date param eg. 02-Dec-2005 and returns 3 rows as I would expect.
> The report page has the date param defined as Datetime and when I enter
> the
> date ie. 02-Dec-2005 it returns 0 rows. I have tried inputting different
> date
> formats
> to no avail. kinda puzzled.
> Any clues as to the cause of the problem.
> Thanx in advance
>|||Thanks Bruse, but no, all params are mapped as required. Something else going
on here I think, might try a string param instead , although Ive not had this
issue before in Date param'd reps. Guess thats the problem when inheriting
others work. Thanx for your and others input. Much appreciated
"Bruce L-C [MVP]" wrote:
> It sounds to me like your query parameter is not hooked up to your report
> parameter. In the dataset click on the ..., parameters tab. Your query
> parameters should be listed on the left and the report parameter they map to
> on the right.
>
> --
> Bruce Loehle-Conger
> MVP SQL Server Reporting Services
> "Scotchy" <Scotchy@.discussions.microsoft.com> wrote in message
> news:B64FE999-9EA6-40C0-896E-984BBDC4BDC4@.microsoft.com...
> > Hi All,
> >
> > I have inherited a report that at the Dataset level works fine. It takes a
> > date param eg. 02-Dec-2005 and returns 3 rows as I would expect.
> >
> > The report page has the date param defined as Datetime and when I enter
> > the
> > date ie. 02-Dec-2005 it returns 0 rows. I have tried inputting different
> > date
> > formats
> > to no avail. kinda puzzled.
> >
> > Any clues as to the cause of the problem.
> >
> > Thanx in advance
> >
>
>

Monday, February 20, 2012

my new function not found

I issue this command in QA:
CREATE FUNCTION encrypt_pair (@.data VARCHAR(14) , @.key char(30) )
RETURNS VARCHAR (60) AS
BEGIN
DECLARE @.encryptedvars VARCHAR(60)
EXEC master..xp_aes_encrypt @.data,@.key,@.encryptedvars OUTPUT
RETURN @.encryptedvars
END

If I run it again, I get an error that it already exists (duh).
Next I run:

select student_id, student_ssn,
encrypt_pair(student_ssn,'000000000000000000000000 00000000') from
student_ssn

and I get the error:
Server: Msg 195, Level 15, State 10, Line 1
'encrypt_pair' is not a recognized function name.
I don't get whats wrong!
Thanks
RobObject Owner...Object Owner

CREATE FUNCTION dbo.encrypt_pair (@.data VARCHAR(14) , @.key char(30) )
RETURNS VARCHAR (60) AS
BEGIN
DECLARE @.encryptedvars VARCHAR(60)
EXEC master..xp_aes_encrypt @.data,@.key,@.encryptedvars OUTPUT
RETURN @.encryptedvars
END

select student_id, student_ssn,
dbo.encrypt_pair(student_ssn,'00000000000000000000 000000000000') from
student_ssn

--
----------
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland

IM: mike@.epprecht.net

MVP Program: http://www.microsoft.com/mvp

Blog: http://www.msmvps.com/epprecht/

"rcamarda" <rcamarda@.cablespeed.com> wrote in message
news:1128344611.711934.195110@.g43g2000cwa.googlegr oups.com...
>I issue this command in QA:
> CREATE FUNCTION encrypt_pair (@.data VARCHAR(14) , @.key char(30) )
> RETURNS VARCHAR (60) AS
> BEGIN
> DECLARE @.encryptedvars VARCHAR(60)
> EXEC master..xp_aes_encrypt @.data,@.key,@.encryptedvars OUTPUT
> RETURN @.encryptedvars
> END
> If I run it again, I get an error that it already exists (duh).
> Next I run:
> select student_id, student_ssn,
> encrypt_pair(student_ssn,'000000000000000000000000 00000000') from
> student_ssn
> and I get the error:
> Server: Msg 195, Level 15, State 10, Line 1
> 'encrypt_pair' is not a recognized function name.
> I don't get whats wrong!
> Thanks
> Rob|||Thanks Mike, that thought never crossed my mind!