Showing posts with label current. Show all posts
Showing posts with label current. Show all posts

Wednesday, March 28, 2012

Latest value for all members at a given point in time

We have a fact table with a current balance record. For this balance rows are added to the table only when the balance has changed. This way we can reduce the volume in the fact table by not saving redundant information for every point in time that we want to analyze.

In SQL it is a simple task to get a view of the lastest fact records for a given point in time. SELECT f1.* FROM factTable f1 WHERE f1.timestamp = (SELECT MAX(f2.timestamp) FROM factTable f2 WHERE f2.timestamp <= givenPointInTime AND f2.balanceKey = f1.balanceKey). In other words, the given point in time directly determines which rows should be fetched from our fact table, one row for each balance record (balanceKey). This row will of course include several dimension attributes, which represent the point in time when the balance changed.

Now, we would like to reproduce this behaviour in our Analysis Services 2005 cube. Using a time dimension, we would like to be able to select a given point in time and have the cube return all rows previous to that time, one row for each balanceKey. Not only the rows matching the given point in time. How can this be done? For the other dimension attributes, they should behave in a normal way, such that for the returned rows, you can slice using any combination of them.

Any help or ideas are appreciated,
Lars

Assuming that you're using AS 2005 Enterprise Edition, you could try the "Last Nonempty Child" aggregation:

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

>>

SQL Server 2005 Books Online

Defining Semiadditive Behavior

Semiadditive measures, which do not uniformly aggregate across all dimensions, are very common in many business scenarios. Every cube that is based on snapshots of balances over time exhibits this problem. You can find these snapshots in applications dealing with securities, account balances, budgeting, human resources, insurance policies and claims, and many other business domains.

...

LastNonEmpty

The member value is evaluated as the value of its last child along the time dimension that contains data.

>>

|||

Thanks Deepak. We are on an Enterprise Edition (pre-SP1) but we do not get the desired behaviour using the "Last Nonempty Child" aggregation. In fact, it doesn't seem to make any difference at all from using SUM. We have created an example containing a relational database with a simple fact table and an analysis services database built upon it with a measure called "Account Balance". If we browse the cube and add the measure and the Account dimension to the results pane, then filter based on the Time dimension and select the date 2005-12-31, Account number 3 should display the balance 400. Currently it does not.

The example (SQL script + XMLA script) can be found at: http://www.intellibis.se/pub/CumulativeExample.zip

Can anyone help us?

Regards,
Lars

sql

Monday, March 26, 2012

LastPeriods

Hello,

I am trying to retrieve the last 3 periods from the current month (ie Sept, Oct, Nov). I am thinking that LastPeriods function might work, but I am getting the following Error "The LASTPERIODS function expects a member expression for the 0 argument. A tuple set expression was used." Here's the code.

SELECT { LASTPERIODS (3, TAIL(nonempty([Week].[Week].[Week Name].Members)) ) } ON COLUMNS FROM [GMDSummary]

Maybe something like that ?

SELECT { LASTPERIODS (3, TAIL(nonempty([Week].[Week].[Week Name].Members)).Item(0).Item(0) ) } ON COLUMNS FROM [GMDSummary]

|||

Hello,

I ran into another issue as to I would like to order the [Week Name] by the Member Key. Currently, the LastPeriods() is returning "Week of 12/25/2006", "Week of 12/17/2006" instead of "Week of 1/22/2007" because it is order by Member Name instead I would like to order by Member Key. Can anyone help me with the code below so the LastPeriods() is indeed the last 3 week (order by Member Key) instead of by Member Name?

SELECT { LASTPERIODS (3, TAIL(nonempty([Week].[Week].[Week Name].Members)).Item(0).Item(0) ) } ON COLUMNS FROM [GMDSummary]

Much Appreciated!

-Lawrence

|||y cant we use LAG function. It is more easier right?

LastPeriods

Hello,

I am trying to retrieve the last 3 periods from the current month (ie Sept, Oct, Nov). I am thinking that LastPeriods function might work, but I am getting the following Error "The LASTPERIODS function expects a member expression for the 0 argument. A tuple set expression was used." Here's the code.

SELECT { LASTPERIODS (3, TAIL(nonempty([Week].[Week].[Week Name].Members)) ) } ON COLUMNS FROM [GMDSummary]

Maybe something like that ?

SELECT { LASTPERIODS (3, TAIL(nonempty([Week].[Week].[Week Name].Members)).Item(0).Item(0) ) } ON COLUMNS FROM [GMDSummary]

|||

Hello,

I ran into another issue as to I would like to order the [Week Name] by the Member Key. Currently, the LastPeriods() is returning "Week of 12/25/2006", "Week of 12/17/2006" instead of "Week of 1/22/2007" because it is order by Member Name instead I would like to order by Member Key. Can anyone help me with the code below so the LastPeriods() is indeed the last 3 week (order by Member Key) instead of by Member Name?

SELECT { LASTPERIODS (3, TAIL(nonempty([Week].[Week].[Week Name].Members)).Item(0).Item(0) ) } ON COLUMNS FROM [GMDSummary]

Much Appreciated!

-Lawrence

|||y cant we use LAG function. It is more easier right?

Monday, March 19, 2012

Last Modified Field

I'm creating a last modified field that I'll be putting into several of my tables in my current database and in many more to come so I want to get the most optimal way of doing this established so I don't end up propogating a problem. The field will be updated with the current time and date each time the row is modified. I created a trigger to do this, but I don't know if this is the best way. I'm not real experienced with MS SQL so I'm certainly open to suggestions. Here's the trigger code I have now.

UPDATE s
SET LastModified = getdate()
FROM secUser s
join inserted i on s.UID = i.UID

Any suggestions would be appreciated.

Thanks,
Nate BaxleyIn this case, the best choice is instead of update trigger.

Good luck!

Monday, March 12, 2012

Last Day of the Month

Hi,
is there a way to find the last day of the month ?
the input parameter is just the month. year will be the current year, start
date will always be 1st of the month. I need to find the last day of the
month
Thankshttp://www.aspfaq.com/show.asp?id=2444
Andrew J. Kelly SQL MVP
"Vishal" <vrajput77@.hotmail.com> wrote in message
news:ems8Wj$BGHA.3812@.TK2MSFTNGP15.phx.gbl...
> Hi,
> is there a way to find the last day of the month ?
> the input parameter is just the month. year will be the current year,
> start date will always be 1st of the month. I need to find the last day of
> the month
> Thanks
>|||Maybe not as elegate as other solutions, but
SELECT DATEADD(dd, -1, CAST(YEAR(DATEADD(mm, 1, GETDATE())) AS char(4)) +
RIGHT('0' + CAST(MONTH(DATEADD(mm, 1, GETDATE())) AS varchar(2)), 2) + '01')
will get you the last day of the current month, in datetime format. If you
want only the numeric portion for the day, then
SELECT DAY(DATEADD(dd, -1, CAST(YEAR(DATEADD(mm, 1, GETDATE())) AS char(4))
+ RIGHT('0' + CAST(MONTH(DATEADD(mm, 1, GETDATE())) AS varchar(2)), 2) +
'01'))
DISCLAIMER: This will not work 7,995 years from now. ;-)
"Vishal" wrote:

> Hi,
> is there a way to find the last day of the month ?
> the input parameter is just the month. year will be the current year, star
t
> date will always be 1st of the month. I need to find the last day of the
> month
> Thanks
>
>|||Speaking of more elegant solutions:
SELECT DATEADD(dd, -1, LEFT(CONVERT(char(8), DATEADD(mm, 1, GETDATE()),
112),6) + '01')
for the last day of the current month in datetime format
SELECT DAY(DATEADD(dd, -1, LEFT(CONVERT(char(8), DATEADD(mm, 1, GETDATE()),
112),6) + '01'))
for just the numeric date portion.
"Mark Williams" wrote:
> Maybe not as elegate as other solutions, but
> SELECT DATEADD(dd, -1, CAST(YEAR(DATEADD(mm, 1, GETDATE())) AS char(4)) +
> RIGHT('0' + CAST(MONTH(DATEADD(mm, 1, GETDATE())) AS varchar(2)), 2) + '01
')
> will get you the last day of the current month, in datetime format. If you
> want only the numeric portion for the day, then
> SELECT DAY(DATEADD(dd, -1, CAST(YEAR(DATEADD(mm, 1, GETDATE())) AS char(4)
)
> + RIGHT('0' + CAST(MONTH(DATEADD(mm, 1, GETDATE())) AS varchar(2)), 2) +
> '01'))
> DISCLAIMER: This will not work 7,995 years from now. ;-)
> "Vishal" wrote:
>|||Speaking of more elegant solutions:
SELECT DATEADD(dd, -1, LEFT(CONVERT(char(8), DATEADD(mm, 1, GETDATE()),
112),6) + '01')
for the last day of the current month in datetime format
SELECT DAY(DATEADD(dd, -1, LEFT(CONVERT(char(8), DATEADD(mm, 1, GETDATE()),
112),6) + '01'))
for just the numeric day of the month.
"Mark Williams" wrote:
> Maybe not as elegate as other solutions, but
> SELECT DATEADD(dd, -1, CAST(YEAR(DATEADD(mm, 1, GETDATE())) AS char(4)) +
> RIGHT('0' + CAST(MONTH(DATEADD(mm, 1, GETDATE())) AS varchar(2)), 2) + '01
')
> will get you the last day of the current month, in datetime format. If you
> want only the numeric portion for the day, then
> SELECT DAY(DATEADD(dd, -1, CAST(YEAR(DATEADD(mm, 1, GETDATE())) AS char(4)
)
> + RIGHT('0' + CAST(MONTH(DATEADD(mm, 1, GETDATE())) AS varchar(2)), 2) +
> '01'))
> DISCLAIMER: This will not work 7,995 years from now. ;-)
> "Vishal" wrote:
>|||OK, I promise this is my last post in this thread. I finally got my head on
straight
SELECT DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE())+1, 0))
SELECT DAY(DATEADD(dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE())+1, 0)))
"Vishal" wrote:

> Hi,
> is there a way to find the last day of the month ?
> the input parameter is just the month. year will be the current year, star
t
> date will always be 1st of the month. I need to find the last day of the
> month
> Thanks
>
>|||Hi,
This is what I want to do:
User enters parameters StoreNo. Month(11), Year(2005), Open Months(8).
Store Table :
Store No. INT (IDENTITY)
OpenDate (DATETIME)
When the user calls a S Proc with the above parameters, I want to check
whether the Store was opened for 8 Months from the OpenDate on the store, if
the OpenDate is 22/02/2004 then the number of months should be calculated
from the next month, not including the month the store opened.
I would like to write this in a Function, please help.
Thanks
"Vishal" <vrajput77@.hotmail.com> wrote in message
news:ems8Wj$BGHA.3812@.TK2MSFTNGP15.phx.gbl...
> Hi,
> is there a way to find the last day of the month ?
> the input parameter is just the month. year will be the current year,
> start date will always be 1st of the month. I need to find the last day of
> the month
> Thanks
>

Last Day of Current Month

Does anyone have a simpler method for this ?
I want to get the last Day of the Current Month, so my thinking is to take
one day away from the first day of next month...it works but is perhaps not
very elegant.
=cdate(format(DATEADD("d",-1,cdate("01/" &
format(System.DateTime.Now.AddMonths(1),"MM/yyyy"))),"dd/MM/yyyy"))
--
Mike StephenHi Mike,
This is the simplest thing I can think of.
=dateadd("d",-1,str(val(Mid(datestring(),1,2))+1) + "/01/" +
right(datestring(),4))
Amarnath
"Mike Stephen" wrote:
> Does anyone have a simpler method for this ?
> I want to get the last Day of the Current Month, so my thinking is to take
> one day away from the first day of next month...it works but is perhaps not
> very elegant.
> =cdate(format(DATEADD("d",-1,cdate("01/" &
> format(System.DateTime.Now.AddMonths(1),"MM/yyyy"))),"dd/MM/yyyy"))
> --
> Mike Stephen|||That's great. Thank you.
Mike.
--
Mike Stephen
"Amarnath" wrote:
> Hi Mike,
> This is the simplest thing I can think of.
> =dateadd("d",-1,str(val(Mid(datestring(),1,2))+1) + "/01/" +
> right(datestring(),4))
> Amarnath
>
> "Mike Stephen" wrote:
> > Does anyone have a simpler method for this ?
> >
> > I want to get the last Day of the Current Month, so my thinking is to take
> > one day away from the first day of next month...it works but is perhaps not
> > very elegant.
> >
> > =cdate(format(DATEADD("d",-1,cdate("01/" &
> > format(System.DateTime.Now.AddMonths(1),"MM/yyyy"))),"dd/MM/yyyy"))
> > --
> > Mike Stephen

Last 3 Months

I need a formula for the first day of the month, 3 months prior to the current date.
Any help?Local DateVar d := date(2006,2,13);
Dateserial(Year(d), Month(d) - 3, 1)

Friday, February 24, 2012

Large scale databases - common practice

Hi,
We are designing a large-scale database application. The current one will
receive information from many inputs constantly.
We expect it to grow in one year to 60GB (on average of 4-5GB/Mo).
The database then (after one year) will have a cleaning procedure that will
delete information and the size of the database will not increase
significantly.
Here are a few questions:
1. What are the guide lines to design and implement such a database? Where
can I find information on the web on such cases?
2. From database creation point of view - should we use a few files that
will create the database or should we use only one ? If we use a few files,
what happends to clustered indexes? Where can I find more reading material?
3. The database has the management tables of the application and the data
tables. One data table will hold most of the information in that database. Do
you think I need to divide te table? if so, how do we do it? What is the
common practice in such cases where a huge table holds so much info
(Currently we have a similar database with 27M records in).
how do you build efficient queries? How do you manage the data in the
separate tables? How do you manage indexes? and again where can I find
material on this?
We know to look inside the BOL but we are looking for common practice
solutions.
TIA!!
Noam
Please don't post independently in separate newsgroups. You can add
multiple newsgroups to the header and then all the answers appear as one.
See my reply in the other newsgroup.
Andrew J. Kelly SQL MVP
"Noam" <Noam@.discussions.microsoft.com> wrote in message
news:2DCD3F8D-DFCF-46F7-BB1C-30355D49B321@.microsoft.com...
> Hi,
> We are designing a large-scale database application. The current one will
> receive information from many inputs constantly.
> We expect it to grow in one year to 60GB (on average of 4-5GB/Mo).
> The database then (after one year) will have a cleaning procedure that
will
> delete information and the size of the database will not increase
> significantly.
> Here are a few questions:
> 1. What are the guide lines to design and implement such a database? Where
> can I find information on the web on such cases?
> 2. From database creation point of view - should we use a few files that
> will create the database or should we use only one ? If we use a few
files,
> what happends to clustered indexes? Where can I find more reading
material?
> 3. The database has the management tables of the application and the data
> tables. One data table will hold most of the information in that database.
Do
> you think I need to divide te table? if so, how do we do it? What is the
> common practice in such cases where a huge table holds so much info
> (Currently we have a similar database with 27M records in).
> how do you build efficient queries? How do you manage the data in the
> separate tables? How do you manage indexes? and again where can I find
> material on this?
> We know to look inside the BOL but we are looking for common practice
> solutions.
> TIA!!
> Noam
>
|||Thank you for your fast reply. This is the first time I posted here and I
will do as you suggest next time!.
Thanks.
Noam
"Andrew J. Kelly" wrote:

> Please don't post independently in separate newsgroups. You can add
> multiple newsgroups to the header and then all the answers appear as one.
> See my reply in the other newsgroup.
>
> --
> Andrew J. Kelly SQL MVP
>
> "Noam" <Noam@.discussions.microsoft.com> wrote in message
> news:2DCD3F8D-DFCF-46F7-BB1C-30355D49B321@.microsoft.com...
> will
> files,
> material?
> Do
>
>

Large scale databases - common practice

Hi,
We are designing a large-scale database application. The current one will
receive information from many inputs constantly.
We expect it to grow in one year to 60GB (on average of 4-5GB/Mo).
The database then (after one year) will have a cleaning procedure that will
delete information and the size of the database will not increase
significantly.
Here are a few questions:
1. What are the guide lines to design and implement such a database? Where
can I find information on the web on such cases?
2. From database creation point of view - should we use a few files that
will create the database or should we use only one ? If we use a few files,
what happends to clustered indexes? Where can I find more reading material?
3. The database has the management tables of the application and the data
tables. One data table will hold most of the information in that database. Do
you think I need to divide te table? if so, how do we do it? What is the
common practice in such cases where a huge table holds so much info
(Currently we have a similar database with 27M records in).
how do you build efficient queries? How do you manage the data in the
separate tables? How do you manage indexes? and again where can I find
material on this?
We know to look inside the BOL but we are looking for common practice
solutions.
TIA!!
NoamPlease don't post independently in separate newsgroups. You can add
multiple newsgroups to the header and then all the answers appear as one.
See my reply in the other newsgroup.
Andrew J. Kelly SQL MVP
"Noam" <Noam@.discussions.microsoft.com> wrote in message
news:2DCD3F8D-DFCF-46F7-BB1C-30355D49B321@.microsoft.com...
> Hi,
> We are designing a large-scale database application. The current one will
> receive information from many inputs constantly.
> We expect it to grow in one year to 60GB (on average of 4-5GB/Mo).
> The database then (after one year) will have a cleaning procedure that
will
> delete information and the size of the database will not increase
> significantly.
> Here are a few questions:
> 1. What are the guide lines to design and implement such a database? Where
> can I find information on the web on such cases?
> 2. From database creation point of view - should we use a few files that
> will create the database or should we use only one ? If we use a few
files,
> what happends to clustered indexes? Where can I find more reading
material?
> 3. The database has the management tables of the application and the data
> tables. One data table will hold most of the information in that database.
Do
> you think I need to divide te table? if so, how do we do it? What is the
> common practice in such cases where a huge table holds so much info
> (Currently we have a similar database with 27M records in).
> how do you build efficient queries? How do you manage the data in the
> separate tables? How do you manage indexes? and again where can I find
> material on this?
> We know to look inside the BOL but we are looking for common practice
> solutions.
> TIA!!
> Noam
>|||Thank you for your fast reply. This is the first time I posted here and I
will do as you suggest next time!.
Thanks.
Noam
"Andrew J. Kelly" wrote:
> Please don't post independently in separate newsgroups. You can add
> multiple newsgroups to the header and then all the answers appear as one.
> See my reply in the other newsgroup.
>
> --
> Andrew J. Kelly SQL MVP
>
> "Noam" <Noam@.discussions.microsoft.com> wrote in message
> news:2DCD3F8D-DFCF-46F7-BB1C-30355D49B321@.microsoft.com...
> > Hi,
> >
> > We are designing a large-scale database application. The current one will
> > receive information from many inputs constantly.
> >
> > We expect it to grow in one year to 60GB (on average of 4-5GB/Mo).
> >
> > The database then (after one year) will have a cleaning procedure that
> will
> > delete information and the size of the database will not increase
> > significantly.
> >
> > Here are a few questions:
> > 1. What are the guide lines to design and implement such a database? Where
> > can I find information on the web on such cases?
> > 2. From database creation point of view - should we use a few files that
> > will create the database or should we use only one ? If we use a few
> files,
> > what happends to clustered indexes? Where can I find more reading
> material?
> > 3. The database has the management tables of the application and the data
> > tables. One data table will hold most of the information in that database.
> Do
> > you think I need to divide te table? if so, how do we do it? What is the
> > common practice in such cases where a huge table holds so much info
> > (Currently we have a similar database with 27M records in).
> > how do you build efficient queries? How do you manage the data in the
> > separate tables? How do you manage indexes? and again where can I find
> > material on this?
> >
> > We know to look inside the BOL but we are looking for common practice
> > solutions.
> >
> > TIA!!
> >
> > Noam
> >
>
>