Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Friday, March 30, 2012

Named Set with TOPCOUNT Not Working Correctly

I need to create reports that contain our top 15 customers at any given point in time. I created the following Named Set using the TopCount function in SSAS 2005:

TOPCOUNT([Customer].[Customer Type-Name-Door Hierarchy].[Customer Name].members, 15, [Measures].[Net Ship Dollars])

When I bring this set into the client (Excel 2007 pivot table), its shows our top 15 customers for all time. When I add a date filter for just 2007, it doesn't update the list to just show the top customers for 2007. It just keeps the same "all time" group.

When I create this named set in a browser query, it works correctly. That is, it shows the top 15 customers for 2007. Is there any way I can get the Named Set created in SSAS to show the correct data based on the filtered date in the client?

Here is the browser query:

WITHSET

[Top Performers]

AS

'TOPCOUNT([Customer].[Customer Type-Name-Door Hierarchy].[Customer Name].members, 15, [Measures].[Net Ship Dollars])'

SELECT

{[Measures].[Net Ship Dollars]} ONCOLUMNS,

{[Top Performers]}ONROWS

FROM Shipments

Where ([Date].[Calendar Year].&[2007])

Thank you.

David

Are you using AS 2005 SP2? This problem has been fixed there.|||

The problem here is because the named set in the MDX Script gets resolved when it is first parsed at which time all the other dimensions will be set to their default members (which is usually the "All" member). Using the EXISTING statement in your set definition should force it to be re-evaluated with the current context each time it is used.

eg

TOPCOUNT(EXISTING [Customer].[Customer Type-Name-Door Hierarchy].[Customer Name].members, 15, [Measures].[Net Ship Dollars])

|||I am using SP2 but still experiencing the problem.|||

Darren,

I tried this but the same problem is occuring. Apparently, the default member of the time hierarchy determines what customer members to return. When I changed the default member to 2007, the named set showed the top customers for 2007. I came across another post which confirmed this as a problem. Does anybody have any workarounds? Thyank you.

David

|||

Hi David,

It seems that I did not read your full question and missed some information. So, just want to clarify. Named sets in AS 2005 are static named sets, which means that they are calculated just once in the context of the current member awailable at that moment. They are not reevaluated during execution of the select statement. Daren is right in the fact that named sets created in the cube scope or session scope (using Create statement) are evaluated when the CREATE statement is executed, therefore TopCount is calculated in the context of the default member. Sets created in the query scope using With clause are evaluated right after the WHERE clause and therefore take current member defined by the where clause in concideration (this is why your query with WITH statement works as you expect and query with named set from CREATE statement return different results). There are it's own pros on having static named sets (mostly performance), but in your case you should try to avoid using TopCount in the named set, if you want the members on other axis to affect results of the topcount.

Irina

P.S.

>>> I came across another post which confirmed this as a problem.

Could you please point me to this thread?

|||

Irina Gorbach wrote:

Named sets in AS 2005 are static named sets, which means that they are calculated just once in the context of the current member awailable at that moment. They are not reevaluated during execution of the select statement.

Of course! sorry for leading you slightly astray. The EXISTING statement will work if you have a set in a calculation

eg

CREATE MEMBER CurrentCube.Measures.CustomersInTop10Pcnt AS COUNT(TOPPERCENT(EXISTING Customer.Customer.Members, 150 Measures.Sales)

This would re-evaluate the set used in the toppercent function based on the current slicing conditions, which is similar, but different.

|||so is this the solution then?

TOPCOUNT(EXISTING [Customer].[Customer Type-Name-Door Hierarchy].[Customer Name].members, 15, [Measures].[Net Ship Dollars])

or is there another way?

Also, how do you get it to sort correctly in excel, if you pull over the named set it just shows you the top X sorted by the SET Expression, whereas if you do an MDX query it sorts it by the Numeric Expression

Named Set with TOPCOUNT Not Working Correctly

I need to create reports that contain our top 15 customers at any given point in time. I created the following Named Set using the TopCount function in SSAS 2005:

TOPCOUNT([Customer].[Customer Type-Name-Door Hierarchy].[Customer Name].members, 15, [Measures].[Net Ship Dollars])

When I bring this set into the client (Excel 2007 pivot table), its shows our top 15 customers for all time. When I add a date filter for just 2007, it doesn't update the list to just show the top customers for 2007. It just keeps the same "all time" group.

When I create this named set in a browser query, it works correctly. That is, it shows the top 15 customers for 2007. Is there any way I can get the Named Set created in SSAS to show the correct data based on the filtered date in the client?

Here is the browser query:

WITH SET

[Top Performers]

AS

'TOPCOUNT([Customer].[Customer Type-Name-Door Hierarchy].[Customer Name].members, 15, [Measures].[Net Ship Dollars])'

SELECT

{[Measures].[Net Ship Dollars]} ON COLUMNS,

{[Top Performers]}ON ROWS

FROM Shipments

Where ([Date].[Calendar Year].&[2007])

Thank you.

David

Are you using AS 2005 SP2? This problem has been fixed there.|||

The problem here is because the named set in the MDX Script gets resolved when it is first parsed at which time all the other dimensions will be set to their default members (which is usually the "All" member). Using the EXISTING statement in your set definition should force it to be re-evaluated with the current context each time it is used.

eg

TOPCOUNT(EXISTING [Customer].[Customer Type-Name-Door Hierarchy].[Customer Name].members, 15, [Measures].[Net Ship Dollars])

|||I am using SP2 but still experiencing the problem.|||

Darren,

I tried this but the same problem is occuring. Apparently, the default member of the time hierarchy determines what customer members to return. When I changed the default member to 2007, the named set showed the top customers for 2007. I came across another post which confirmed this as a problem. Does anybody have any workarounds? Thyank you.

David

|||

Hi David,

It seems that I did not read your full question and missed some information. So, just want to clarify. Named sets in AS 2005 are static named sets, which means that they are calculated just once in the context of the current member awailable at that moment. They are not reevaluated during execution of the select statement. Daren is right in the fact that named sets created in the cube scope or session scope (using Create statement) are evaluated when the CREATE statement is executed, therefore TopCount is calculated in the context of the default member. Sets created in the query scope using With clause are evaluated right after the WHERE clause and therefore take current member defined by the where clause in concideration (this is why your query with WITH statement works as you expect and query with named set from CREATE statement return different results). There are it's own pros on having static named sets (mostly performance), but in your case you should try to avoid using TopCount in the named set, if you want the members on other axis to affect results of the topcount.

Irina

P.S.

>>> I came across another post which confirmed this as a problem.

Could you please point me to this thread?

|||

Irina Gorbach wrote:

Named sets in AS 2005 are static named sets, which means that they are calculated just once in the context of the current member awailable at that moment. They are not reevaluated during execution of the select statement.

Of course! sorry for leading you slightly astray. The EXISTING statement will work if you have a set in a calculation

eg

CREATE MEMBER CurrentCube.Measures.CustomersInTop10Pcnt AS COUNT(TOPPERCENT(EXISTING Customer.Customer.Members, 150 Measures.Sales)

This would re-evaluate the set used in the toppercent function based on the current slicing conditions, which is similar, but different.

|||so is this the solution then?

TOPCOUNT(EXISTING [Customer].[Customer Type-Name-Door Hierarchy].[Customer Name].members, 15, [Measures].[Net Ship Dollars])

or is there another way?

Also, how do you get it to sort correctly in excel, if you pull over the named set it just shows you the top X sorted by the SET Expression, whereas if you do an MDX query it sorts it by the Numeric Expression
sql

Wednesday, March 28, 2012

Named Pipes and Clusters

Can the Named Pipes protocol be removed from a SQL Cluster
(SQL2000/Win2003)? And it still function properly, of course...
-Arnie Rowland
Hi Arnie,
No it cannot, see KB article:
831127 Named Pipes Support Cannot Be Removed on a Virtual Server That Is
http://support.microsoft.com/?id=831127
Dave Whitney
SQL Support
This posting is provided "AS IS" with no warranties, and confers no rights.

Monday, March 26, 2012

Named Instances

During the install SQL 2000 askes to use the default to name a instance. Is
it better to name the instance or use the default? What is the function of
instances in SQL?Hello,
With Microsoft SQL Server 2000, you have the option of installing multiple
copies, or instances of SQL Server on one computer. When setting up a new
installation of SQL Server 2000 or maintaining an existing installation,
you can specify it as:
A default instance of SQL Server.
This instance is identified by the network name of the computer on which it
is running. Applications using client software from earlier versions of SQL
Server can connect to a default instance. SQL Server version 6.5 or SQL
Server version 7.0 servers can operate as default instances. However, a
computer can have only ONE version functioning as the default instance at a
time.
A named instance of SQL Server.
This instance is identified by the network name of the computer plus an
instance name, in the format <computername>\<instancename>. Applications
must use SQL Server 2000 client components to connect to a named instance.
A computer can run any number of named instances of SQL Server
concurrently. A named instance can run at the same time as an existing
installation of SQL Server version 6.5 or SQL Server version 7.0. The
instance name cannot exceed 16 characters.
The maximum number of instances supported in SQL Server 2000 is 16. For
more information, refer to the following articles:
Multiple Instances of SQL Server
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/architec/8_
ar_cs_9i5u.asp
Working with Named and Multiple Instances of SQL Server 2000
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/instsql/in_
runsetup_2xmb.asp
Multiple Instances
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlmag2k/
html/MultipleInstances.asp
I hope the information is helpful.
Sophie Guo
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
=====================================================When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================This posting is provided "AS IS" with no warranties, and confers no rights.|||If the server later changes name or joins a domain will th instance name
change as well. What I'm doing is replacing are existing SQL server with a
new SQL Server. I've named the new server newsql and made it a member of a
workgroup. After I move the databses over I would then change change the
name on the old server and then make the new server the name of the old
server and join it to the domain. Will the instance name chnage when the
server's name changes?
"Sophie Guo [MSFT]" wrote:
> Hello,
> With Microsoft SQL Server 2000, you have the option of installing multiple
> copies, or instances of SQL Server on one computer. When setting up a new
> installation of SQL Server 2000 or maintaining an existing installation,
> you can specify it as:
> A default instance of SQL Server.
> This instance is identified by the network name of the computer on which it
> is running. Applications using client software from earlier versions of SQL
> Server can connect to a default instance. SQL Server version 6.5 or SQL
> Server version 7.0 servers can operate as default instances. However, a
> computer can have only ONE version functioning as the default instance at a
> time.
> A named instance of SQL Server.
> This instance is identified by the network name of the computer plus an
> instance name, in the format <computername>\<instancename>. Applications
> must use SQL Server 2000 client components to connect to a named instance.
> A computer can run any number of named instances of SQL Server
> concurrently. A named instance can run at the same time as an existing
> installation of SQL Server version 6.5 or SQL Server version 7.0. The
> instance name cannot exceed 16 characters.
> The maximum number of instances supported in SQL Server 2000 is 16. For
> more information, refer to the following articles:
> Multiple Instances of SQL Server
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/architec/8_
> ar_cs_9i5u.asp
> Working with Named and Multiple Instances of SQL Server 2000
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/instsql/in_
> runsetup_2xmb.asp
> Multiple Instances
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsqlmag2k/
> html/MultipleInstances.asp
>
> I hope the information is helpful.
> Sophie Guo
> Microsoft Online Partner Support
> Get Secure! - www.microsoft.com/security
> =====================================================> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> =====================================================> This posting is provided "AS IS" with no warranties, and confers no rights.
>|||Hello,
After renaming a server, we can perform the following steps to rename SQL
server instance:
1. Restart the server and then start the SQL Server service. This should
automatically get the *new* Server name and we only need to run the
following procedures to
update syservers system table:
-- For a default instance
EXEC sp_dropserver ''old_name''
GO
EXEC sp_addserver ''new_name'', local
GO
-- For a named instance
EXEC sp_dropserver ''original_name\original_instance_name''
GO
EXEC sp_addserver ''new_server_name\instance_name'', local
GO
2. After running the sp_addserver stored procedure, we must restart the SQL
Server service for the change to take effect. The following article
describes this
scenario in more detail:
303774 BUG: "Renaming A Server" Topic in SQL Server Books Online is
Incomplete
http://support.microsoft.com/?id=303774
I hope the information is helpful.
Sophie Guo
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
=====================================================When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=====================================================This posting is provided "AS IS" with no warranties, and confers no rights.

Named Instances

During the install SQL 2000 askes to use the default to name a instance. I
s
it better to name the instance or use the default? What is the function of
instances in SQL?Hello,
With Microsoft SQL Server 2000, you have the option of installing multiple
copies, or instances of SQL Server on one computer. When setting up a new
installation of SQL Server 2000 or maintaining an existing installation,
you can specify it as:
A default instance of SQL Server.
This instance is identified by the network name of the computer on which it
is running. Applications using client software from earlier versions of SQL
Server can connect to a default instance. SQL Server version 6.5 or SQL
Server version 7.0 servers can operate as default instances. However, a
computer can have only ONE version functioning as the default instance at a
time.
A named instance of SQL Server.
This instance is identified by the network name of the computer plus an
instance name, in the format <computername>\<instancename>. Applications
must use SQL Server 2000 client components to connect to a named instance.
A computer can run any number of named instances of SQL Server
concurrently. A named instance can run at the same time as an existing
installation of SQL Server version 6.5 or SQL Server version 7.0. The
instance name cannot exceed 16 characters.
The maximum number of instances supported in SQL Server 2000 is 16. For
more information, refer to the following articles:
Multiple Instances of SQL Server
http://msdn.microsoft.com/library/d...-us/architec/8_
ar_cs_9i5u.asp
Working with Named and Multiple Instances of SQL Server 2000
http://msdn.microsoft.com/library/d...-us/instsql/in_
runsetup_2xmb.asp
Multiple Instances
http://msdn.microsoft.com/library/d...-us/dnsqlmag2k/
html/MultipleInstances.asp
I hope the information is helpful.
Sophie Guo
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
========================================
=============
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
========================================
=============
This posting is provided "AS IS" with no warranties, and confers no rights.|||If the server later changes name or joins a domain will th instance name
change as well. What I'm doing is replacing are existing SQL server with a
new SQL Server. I've named the new server newsql and made it a member of a
workgroup. After I move the databses over I would then change change the
name on the old server and then make the new server the name of the old
server and join it to the domain. Will the instance name chnage when the
server's name changes?
"Sophie Guo [MSFT]" wrote:

> Hello,
> With Microsoft SQL Server 2000, you have the option of installing multiple
> copies, or instances of SQL Server on one computer. When setting up a new
> installation of SQL Server 2000 or maintaining an existing installation,
> you can specify it as:
> A default instance of SQL Server.
> This instance is identified by the network name of the computer on which i
t
> is running. Applications using client software from earlier versions of SQ
L
> Server can connect to a default instance. SQL Server version 6.5 or SQL
> Server version 7.0 servers can operate as default instances. However, a
> computer can have only ONE version functioning as the default instance at
a
> time.
> A named instance of SQL Server.
> This instance is identified by the network name of the computer plus an
> instance name, in the format <computername>\<instancename>. Applications
> must use SQL Server 2000 client components to connect to a named instance.
> A computer can run any number of named instances of SQL Server
> concurrently. A named instance can run at the same time as an existing
> installation of SQL Server version 6.5 or SQL Server version 7.0. The
> instance name cannot exceed 16 characters.
> The maximum number of instances supported in SQL Server 2000 is 16. For
> more information, refer to the following articles:
> Multiple Instances of SQL Server
> http://msdn.microsoft.com/library/d...ft.com/security
> ========================================
=============
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ========================================
=============
> This posting is provided "AS IS" with no warranties, and confers no rights
.
>|||Hello,
After renaming a server, we can perform the following steps to rename SQL
server instance:
1. Restart the server and then start the SQL Server service. This should
automatically get the *new* Server name and we only need to run the
following procedures to
update syservers system table:
-- For a default instance
EXEC sp_dropserver ''old_name''
GO
EXEC sp_addserver ''new_name'', local
GO
-- For a named instance
EXEC sp_dropserver ''original_name\original_instance_name''
GO
EXEC sp_addserver ''new_server_name\instance_name'', local
GO
2. After running the sp_addserver stored procedure, we must restart the SQL
Server service for the change to take effect. The following article
describes this
scenario in more detail:
303774 BUG: "Renaming A Server" Topic in SQL Server Books Online is
Incomplete
http://support.microsoft.com/?id=303774
I hope the information is helpful.
Sophie Guo
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
========================================
=============
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
========================================
=============
This posting is provided "AS IS" with no warranties, and confers no rights.

Named Instances

During the install SQL 2000 askes to use the default to name a instance. Is
it better to name the instance or use the default? What is the function of
instances in SQL?
Hello,
With Microsoft SQL Server 2000, you have the option of installing multiple
copies, or instances of SQL Server on one computer. When setting up a new
installation of SQL Server 2000 or maintaining an existing installation,
you can specify it as:
A default instance of SQL Server.
This instance is identified by the network name of the computer on which it
is running. Applications using client software from earlier versions of SQL
Server can connect to a default instance. SQL Server version 6.5 or SQL
Server version 7.0 servers can operate as default instances. However, a
computer can have only ONE version functioning as the default instance at a
time.
A named instance of SQL Server.
This instance is identified by the network name of the computer plus an
instance name, in the format <computername>\<instancename>. Applications
must use SQL Server 2000 client components to connect to a named instance.
A computer can run any number of named instances of SQL Server
concurrently. A named instance can run at the same time as an existing
installation of SQL Server version 6.5 or SQL Server version 7.0. The
instance name cannot exceed 16 characters.
The maximum number of instances supported in SQL Server 2000 is 16. For
more information, refer to the following articles:
Multiple Instances of SQL Server
http://msdn.microsoft.com/library/de...us/architec/8_
ar_cs_9i5u.asp
Working with Named and Multiple Instances of SQL Server 2000
http://msdn.microsoft.com/library/de...us/instsql/in_
runsetup_2xmb.asp
Multiple Instances
http://msdn.microsoft.com/library/de...us/dnsqlmag2k/
html/MultipleInstances.asp
I hope the information is helpful.
Sophie Guo
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
================================================== ===
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
================================================== ===
This posting is provided "AS IS" with no warranties, and confers no rights.
|||If the server later changes name or joins a domain will th instance name
change as well. What I'm doing is replacing are existing SQL server with a
new SQL Server. I've named the new server newsql and made it a member of a
workgroup. After I move the databses over I would then change change the
name on the old server and then make the new server the name of the old
server and join it to the domain. Will the instance name chnage when the
server's name changes?
"Sophie Guo [MSFT]" wrote:

> Hello,
> With Microsoft SQL Server 2000, you have the option of installing multiple
> copies, or instances of SQL Server on one computer. When setting up a new
> installation of SQL Server 2000 or maintaining an existing installation,
> you can specify it as:
> A default instance of SQL Server.
> This instance is identified by the network name of the computer on which it
> is running. Applications using client software from earlier versions of SQL
> Server can connect to a default instance. SQL Server version 6.5 or SQL
> Server version 7.0 servers can operate as default instances. However, a
> computer can have only ONE version functioning as the default instance at a
> time.
> A named instance of SQL Server.
> This instance is identified by the network name of the computer plus an
> instance name, in the format <computername>\<instancename>. Applications
> must use SQL Server 2000 client components to connect to a named instance.
> A computer can run any number of named instances of SQL Server
> concurrently. A named instance can run at the same time as an existing
> installation of SQL Server version 6.5 or SQL Server version 7.0. The
> instance name cannot exceed 16 characters.
> The maximum number of instances supported in SQL Server 2000 is 16. For
> more information, refer to the following articles:
> Multiple Instances of SQL Server
> http://msdn.microsoft.com/library/de...us/architec/8_
> ar_cs_9i5u.asp
> Working with Named and Multiple Instances of SQL Server 2000
> http://msdn.microsoft.com/library/de...us/instsql/in_
> runsetup_2xmb.asp
> Multiple Instances
> http://msdn.microsoft.com/library/de...us/dnsqlmag2k/
> html/MultipleInstances.asp
>
> I hope the information is helpful.
> Sophie Guo
> Microsoft Online Partner Support
> Get Secure! - www.microsoft.com/security
> ================================================== ===
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ================================================== ===
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
|||Hello,
After renaming a server, we can perform the following steps to rename SQL
server instance:
1. Restart the server and then start the SQL Server service. This should
automatically get the *new* Server name and we only need to run the
following procedures to
update syservers system table:
-- For a default instance
EXEC sp_dropserver ''old_name''
GO
EXEC sp_addserver ''new_name'', local
GO
-- For a named instance
EXEC sp_dropserver ''original_name\original_instance_name''
GO
EXEC sp_addserver ''new_server_name\instance_name'', local
GO
2. After running the sp_addserver stored procedure, we must restart the SQL
Server service for the change to take effect. The following article
describes this
scenario in more detail:
303774 BUG: "Renaming A Server" Topic in SQL Server Books Online is
Incomplete
http://support.microsoft.com/?id=303774
I hope the information is helpful.
Sophie Guo
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
================================================== ===
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
================================================== ===
This posting is provided "AS IS" with no warranties, and confers no rights.

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

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!