Showing posts with label member. Show all posts
Showing posts with label member. Show all posts

Friday, March 23, 2012

Retrieving User-Defined Member Properties using PROPERTIES keyword

I am using an example from ‘SQL Server 2005 Books Online’, which explain how to retrieve User-Defined Member Properties.

Using the PROPERTIES Keyword to Retrieve User-Defined Member Properties:

DIMENSION PROPERTIES [Dimension.]Level.<Custom_Member_Property>

The PROPERTIES keyword appears after the set expression of the axis specification. For example, the following MDX query the PROPERTIES keyword retrieves the List Price and Dealer Price user-defined member properties and appears after the set expression that identifies the products sold in January:

SELECT

CROSSJOIN([Ship Date].[Calendar].[Calendar Year].Members,

[Measures].[Sales Amount]) ON COLUMNS,

NON EMPTY Product.Product.MEMBERS

DIMENSION PROPERTIES

Product.Product.[List Price],

Product.Product.[Dealer Price]ON ROWS

FROM [Adventure Works]

WHERE ([Date].[Month of Year].[January])

After running the above MDX query, I don’t see any [List Price] or [Dealer Price] and the result is exactly like running the following MDX query:

SELECT

CROSSJOIN([Ship Date].[Calendar].[Calendar Year].Members,

[Measures].[Sales Amount]) ON COLUMNS,

NON EMPTY Product.Product.MEMBERS ON ROWS

FROM [Adventure Works]

WHERE ([Date].[Month of Year].[January])

How can I retrieve User-Defined Member Properties?

Thanks,

Yones

I found the problem, which is related to the way data is returned after execution of an MDX query. It is returned differently in Analysis Services 2000 and 2005. For example using an XMLReader, elements names are returned as follow:

AS 2000:

clXmlReader.Name:"List Price"

clXmlReader.value:"List Price value"

clXmlReader.Name:"Dealer Price"

clXmlReader.value:"Dealer Price value"

AS 2005:

clXmlReader.Name:"_x005B_ Product _x005D_._x005B_Product_x005D_._x005B_Product_x005D_._x005B_ List Price _x005D_"

clXmlReader.value:"List Price value"

clXmlReader.Name:"_x005B_ Product _x005D_._x005B_ Product _x005D_._x005B_ Product _x005D_._x005B_ Dealer Price _x005D_"

clXmlReader.value:"Dealer Price value"

Tuesday, March 20, 2012

Retrieving FirstChild, LastChild from a Distant Parent

I have a Time Dimension, [Time].[Fiscal Year] which has three levels: Year, Qtr, Month.

My situation requires that I get the First member of the first Qtr (month) and the last member of the last Qtr (month) given only the Year. I am close to the solution, if this is possible, and really need some help to finish this. Here is a speific scenario:

From my application, user selects "Fiscal Year" as one of the fiters. Year = 2007 (No additional level is given to select).
The calculation I am trying to form is for % growth ((Measures for FY 2007 - Measures for FY 2006) / Measures for FY 2006).

My problem is that the the total periods for FY 2007 do not yet equal the total periods for 2006. So what I want to do is calculate the growth by summing the periods that exist in 2007 at the month level. Then summing those same periods for the previous period. I can do this by getting the first existing member of the first month and the last existing member of the last month. To expand on the scenario:

FY 2007 has 6 Months of data:

FY 2007
Q1
July
Aug
Sep
Q2
Oct
Nov
Dec

FY 2006 has all 12 Months of Data (Q1, Q2, Q3, Q4)

Given only FY 2007, I need to retrieve Member July and Dec.

The closest I have come to this is:

SET [FirstMonth] AS
'{(Descendants([Time].[Fiscal Year].[2007].FirstChild, 2, SELF))}'

SET [LastMonth] AS
'{(Descendants([Time].[Fiscal Year].[2007].LastChild, 2, SELF))}'

This gives me:

[FirstMonth]

July
Aug
Sep

[LasstMonth]

Oct
Nov
Dec

When running in Select statement. Can this even be accomplished with MDX?

Take a look at OpeningPeriod() and ClosingPeriod() MDX functions - something like:

OpeningPeriod([Time].[Fiscal Year].[Month]) and ClosingPeriod([Time].[Fiscal Year].[Month])

http://msdn2.microsoft.com/en-us/library/ms145992.aspx

>>

SQL Server 2005 Books Online

OpeningPeriod (MDX)

Updated: 17 July 2006

Returns the first sibling among the descendants of a specified level, optionally at a specified member.

...

>>

|||Nice! Thank you very much.

Monday, March 12, 2012

Retrieving data from an attached mdf file

I attach my SQL Server Express data file to my host. I would like to copy all of my member information back to my local computer. How can I do this? My host won't allow my to physically copy the data file over.

My host is discountasp.net.

Thanks,
Jeff

Port 80 is obviously open, so create a web service to read the data. Lock the site to respond only to your external IP address to avoid anybody else being able to get at the data.

Friday, March 9, 2012

Retrieving all users in db with specified AGE range

Hello, I have a table called Member in my database that I use to store information about users including the date of birth for each person. I have a search function in my application that is supposed to look through the Member table and spit out a list of users with a user-inputted age range (min and max ages). Now, I could have stored ages instead of dob in the table, but I would think that's bad practice since age changes and would need continuous recomputing (which is db intensive) as opposed to dob which stays the same.

So what I'm thinking is getting the min and max user inputted ages, convert them to dob values (of type DateTime) in the application. And then, to query the db and return a list of all users in the Member whose dob falls in between those two dates (is a BETWEEN even possible with DateTime values?).

How is the best way to go about this? There are many sites out there that return users with user specified age ranges. Is there a best way to do this that's the least taxing on the db?

TIA.

You can use the DateDiff function to get there age

http://msdn2.microsoft.com/en-us/library/ms189794.aspx

|||

Thanks for the reply. But what about my database table implementation... is that the right way to go? i'm assuming having an age field is poor practice and I should just have a dob field.

And for the next step, how would I determine the list of users who are say, between the ages of 40 and 55? The DateDiff requires that I know 2 dates ahead of time and that would force me to do a table scan (would be time costly if the table gets huge), computing all dob values in the table with the current time to see if a user falls between 40 and 55. Is there a way that I can convert age into a DateTime? If so, I could compute the DateTimes for the min and max values that are inputted by the user. And then I can maybe do like a "WHERE dob BETWEEN min AND max"...... something to that effect to retrieve the necessary rows. BETWEEN works for integers, not sure for DateTime.

Sorry for the stupid questions, I'm a relative newbie. TIA.

|||

Anybody?Smile

Ok, I think this is the way to go. Just have a DOB field in the Member table. Having an age field is bad practice, I'd think. Then, when a user wants to search for all members within a specified age range (a min and a max age value in years), I would translate the age values into a DateTime format (current time minus age to derive DOB) on the application side. Then I would run an SQL query along the lines of "SELECT * FROM Member WHERE dobDateMin BETWEEN dobDateMax". This should return all members whose DOBs fall within the age range.

The question is, how do I calculate a DOB in DateTime given an age in years? Pseudo-speaking, it would be like.... take the current system time, minus the age in years, and derive the date in DateTime format, so I can feed it into the SQL statement.

Am I on the right track with this? How do all the sites that allow searches for age ranges do this?

TIA

|||

Yes, you would use the dateadd function for this.

SELECT *

FROM MyTable

WHERE DoB Between DATEADD(year,0-@.MaxAge,floor(cast(getutcdate() as float))) AND DATEADD(year,0-@.MinAge,floor(cast(getutcdate() as float)+1))

Assuming that DoB is a datetime field in the MyTable table, and there is an index on that field, it will do an index scan range to return your results. GetUTCDate() may not give you the date you are looking for, adjust as necessary.

|||

Thank you - that seems like what I'm looking for and I appreciate the example so I could visualize how that could be used here. I would have liked to do all of the calculating on the app side to avoid database overhead, but I'll take what I can get. Yes, the DOB field is already indexed to avoid a costly table scan. So do you think other sites use this approach when trying to return results of users within an age range? I'll try it out and see if it works. Thanks.

***** BTW, just out of curiosity, why did you cast the third parameter into float and then floor it? Is this so that you can get pinpoint accuracy at the date level? If so, since the third field accepted a datetime, don't we have to recast it again into a datetime (or smalldatetime)? Like ....

WHERE dob BETWEEN DATEADD(year,0-@.MaxAge,CAST(FLOOR(CAST(getdate() AS float)) AS datetime)) AND DATEADD(year,0-@.MinAge,CAST(FLOOR(CAST(getdate() AS float)+1) AS datetime))

|||

It's a quick hack. Datetimes when converted to floats are in a format that the date portion is stored in a whole numbers, and the time portion as a fraction of a number. By flooring it, we lose the time portion (or more accurately, we get the very smallest time for that date -- exactly midnight).

As for casting it back again, SQL Server will do that as an implicit conversion (It doesn't need to be stated, but you can if you want).

As for where to calculate the dates, I would do it on the server side. It's not really a hard calculation to make for SQL Server, and it abstracts the implementation of the age search to SQL Server. Meaning, if at a later time, we decide that we need to (for whatever reason, performance, scalability, integration) change how we do the search the application code doesn't need to change. We could for example, add an age column to the member table, run a batch process at night that goes through and updates all the user's age, and change the query (assuming it's in a stored procedure, or depends on a view) and the application(s) wouldn't even notice except for possibly better performance. All depends on your environment really. If you aren't using stored procedures then it won't really buy you very much though.

Tuesday, February 21, 2012

Retrieve DATA from relation db from within a Cube (calculated member)

Hello,

I need the ability to retrieve data element that is stored in a relational DB from within a cube (calculated measure). Is there a way to do this? If so, can someone please indicate how? If not, is there a way to store some data which is really independant of any measure or dimension in a cube?

I appreciate your help in advance.

Thanks,

Not sure what your question is. The cube you build contans data Analysis Server reads from relational database. By accessing cube you access copy of the data retreived from relational database.

If you would like Analysis Server not to store copy of relational data, you can define storage mode for AS objects as ROLAP. In this case MDX queries will generate SQL queries ( if data is not cached ).This mode obviously gets you poor query performance compared to MOLAP storage mode, which is default mode.

Edward.
--
This posting is provided "AS IS" with no warranties, and confers no rights.

Retrieve DATA from relation db from within a Cube (calculated member)

Hello,

I need the ability to retrieve data element that is stored in a relational DB from within a cube (calculated measure). Is there a way to do this? If so, can someone please indicate how? If not, is there a way to store some data which is really independant of any measure or dimension in a cube?

I appreciate your help in advance.

Thanks,

Not sure what your question is. The cube you build contans data Analysis Server reads from relational database. By accessing cube you access copy of the data retreived from relational database.

If you would like Analysis Server not to store copy of relational data, you can define storage mode for AS objects as ROLAP. In this case MDX queries will generate SQL queries ( if data is not cached ).This mode obviously gets you poor query performance compared to MOLAP storage mode, which is default mode.

Edward.
--
This posting is provided "AS IS" with no warranties, and confers no rights.