Showing posts with label specific. Show all posts
Showing posts with label specific. Show all posts

Thursday, March 29, 2012

Copy member data to another member

Hi,

I need to copy all data from one member to another member from the same dimension.

To be more specific, I have a scenario dimension with these members:

->Budget
->Forecast

And I want to start my Forecast with the Budget data from last year * 1.05. Because I will change the values from forecast I can't just put a formula (forecast = budget *1.05, if I do that the cube will always show the formula values and not some new values that I want to enter).

Is there a easy way to do that? Any tip will be great.

Thanks,
Handerson

You should be able to achieve what you are trying to do using SCOPE and FREEZE in the cube MDX script. There are actually some good examples in the Adventure Works cube script. Open the script and do a find on "quota". This will take you to a section in the script where there are several examples that are used to populate a measure called "Sales Amount Quota" in the Adventure Works cube.

HTH,

- Steve

|||

Steve,

I have installed the Adventure Works cube but I can't find the scripts that you sad, where I can find then? I'm using SQL Server 2005.

I looked the Scope and Freeze command, you sugest that I do that:

Forecast01 = Budget * 1.05

Freeze(Forecast01)

After that the users can change de Budget values without change the Forecast, ok. But the users can change the Forecast value using write-back?

And every month I will generate a new Forecast, like this:

Forecast02 = Forecast01

Freeze(Forecast02)

Next month I will do:

Forecast03 = Forecast02

Freeze(Forecast03)

And so on... It'll be performatic? Because of that I was trying to find a way to copy the data instead of build a formule.

Thanks,

Handerson

|||

Handerson,

Here is the script segment that I was referring to. With regard to your question, I thought that you were starting from a base period and then trying to leverage the data that was there into future periods. If you truly need to copy data then you would probably be better off doing this at the source. You can still leverage MDX to show future periods that do not have data using the Scope and Freeze functions. You can also check to see if there is an existing value before you compute a projected value.

/*--

| Sales Quota Allocation |

--*/

/*-- Allocate equally to quarters in H2 FY 2005 */

Scope

(

[Date].[Fiscal Year].&[2005],

[Date].[Fiscal].[Fiscal Quarter].Members,

[Measures].[Sales Amount Quota]

) ;

This = ParallelPeriod

(

[Date].[Fiscal].[Fiscal Year], 1,

[Date].[Fiscal].CurrentMember

) * 1.35 ;

/*-- Allocate equally to months in FY 2002 --*/

Scope

(

[Date].[Fiscal Year].&[2002],

[Date].[Fiscal].[Month].Members

) ;

This = [Date].[Fiscal].CurrentMember.Parent / 3 ;

End Scope ;

/*-- Weighted allocation to remaining months */

// Pin quarterly values prior to assigning weights for months

// This is done in order to avoid overwriting the quarterly values

// once weights are entered for monthly values.

Freeze

(

[Date].[Fiscal].[Fiscal Quarter].Members,

[Measures].[Sales Amount Quota]

) ;

// Scope on month level in FY 2003 and onwards

Scope

(

[Date].[Fiscal Year].&[2003] : Null,

[Date].[Fiscal].[Month].Members

) ;

// Compute weights based on reseller sales ratio in previous year

This =

(

ParallelPeriod // Fetch reseller sales amount in previous year

(

[Date].[Fiscal].[Fiscal Year], 1,

[Date].[Fiscal].CurrentMember

),

[Measures].[Reseller Sales Amount]

)

/

(

ParallelPeriod // Divide monthly value by quarterly value to obtain ratio

(

[Date].[Fiscal].[Fiscal Year], 1,

[Date].[Fiscal].CurrentMember.Parent

),

[Measures].[Reseller Sales Amount]

) ;

// Allocate quarterly values to months according to weight

This = [Measures].CurrentMember * [Date].[Fiscal].Parent ;

End Scope ;

End Scope ;

sqlsql

Tuesday, March 27, 2012

Copy ID from one table to another as foreign key

Hi,
I have two tables, one, called tblDoctor, with
docNo, docName . . .
the other, called tblPatient with
pntNo, pntName . . .
I want to insert a specific docNo value from one row in tblDoctor into
a specific row in tblPatient using a stored procedure within ADO.
Can i have an SQL statement along the lines of:
INSERT INTO tblPatient (docNo)
WHERE pntName = "a value"
SELECT docNo
FROM tblDoctor WHERE docName = "another value"
If not, has anyone got any suggestions on alternative methods of
accomplishing the same thing?
ThanksTry,
update tblPatient
set docNo = (SELECT docNo FROM tblDoctor WHERE docName = "another value")
WHERE pntName = "a value"
Be sure that you get just one docNo for a specific docName, if not sql
server will give an error. A trick can be to use "TOP 1" in the select
statement, but this is not the correct way to do this.
AMB
"Assimalyst" wrote:

> Hi,
> I have two tables, one, called tblDoctor, with
> docNo, docName . . .
> the other, called tblPatient with
> pntNo, pntName . . .
> I want to insert a specific docNo value from one row in tblDoctor into
> a specific row in tblPatient using a stored procedure within ADO.
> Can i have an SQL statement along the lines of:
> INSERT INTO tblPatient (docNo)
> WHERE pntName = "a value"
> SELECT docNo
> FROM tblDoctor WHERE docName = "another value"
> If not, has anyone got any suggestions on alternative methods of
> accomplishing the same thing?
> Thanks
>|||I've guessed what your tables should look like from your question and I used
some artistic licence to add a docNo Foreign Key to the Patient table. That
is of course that I understand what you are trying to achieve.
CREATE TABLE tblPatient (patientNo int, pntName varchar(10), docNo int)
CREATE TABLE tblDoctor (docNo int, docName varchar(10))
UPDATE tblPatient SET DocNo = D.docNo
FROM (SELECT docNo FROM tblDoctor WHERE docName = 'another value') D
WHERE pntName = 'a value'
Hope this offers some thought to your problem
Andy
"Assimalyst" wrote:

> Hi,
> I have two tables, one, called tblDoctor, with
> docNo, docName . . .
> the other, called tblPatient with
> pntNo, pntName . . .
> I want to insert a specific docNo value from one row in tblDoctor into
> a specific row in tblPatient using a stored procedure within ADO.
> Can i have an SQL statement along the lines of:
> INSERT INTO tblPatient (docNo)
> WHERE pntName = "a value"
> SELECT docNo
> FROM tblDoctor WHERE docName = "another value"
> If not, has anyone got any suggestions on alternative methods of
> accomplishing the same thing?
> Thanks
>|||Thank you for the replies, i've tried both suggestions, but having no
luck. The tblPatient.doctorNo remains blank.
Below is the relevant code and stored procedure, i'm getting no errors,
and the code runs through the debugger without a hitch.
string strSgyNo = System.Convert.ToString(intSgyNo);
string strDocFName = fNameTxtBx.Text;
string strDocLName = lNameTxtBx.Text;
string strPatientCode = patientCodeLbl.Text;
string strDOB = dobLbl.Text;
// Use stored procedure to input doctorNo as foreign key in tblPatient
SqlCommand cmd = new SqlCommand("proc_AddDocToPnt", conn);
cmd.CommandType = CommandType.StoredProcedure;
// Add parameters to command, which will be passed to the stored
procedure
cmd.Parameters.Add(new SqlParameter("@.docFName", strDocFName));
cmd.Parameters.Add(new SqlParameter("@.docLName", strDocLName));
cmd.Parameters.Add(new SqlParameter("@.sgyNo", strSgyNo));
cmd.Parameters.Add(new SqlParameter("@.pntCode", strPatientCode));
cmd.Parameters.Add(new SqlParameter("@.pntDOB", strDOB));
Stored Procedure:
CREATE PROCEDURE proc_AddDocToPnt
(@.docFName varchar(20),
@.docLName varchar(30),
@.sgyNo int,
@.pntCode varchar(15),
@.pntDOB datetime
)
AS
UPDATE tblPatient
SET doctorNo = D.doctorNo
FROM(SELECT doctorNo
FROM tblReferringDoctorContacts
WHERE (docFName = @.docFName)
AND (docLName=@.docLName)
AND (sgyNo=@.sgyNo)) D
WHERE ((pntUnitID = @.pntCode) AND (pntDOB = @.pntDOB))
GO
Can anyone spot a problem?
Thanks again.|||Hi
Run these 2 queries. Both should return results using Query Analyser
DECLARE @.docFName varchar(20)
DECLARE @.docLName varchar(30)
DECLARE @.sgyNo int
DECLARE @.pntCode varchar(15)
DECLARE @.pntDOB datetime
--Set the values that would be passed using your code
SET @.docFName = ''
SET @.docLName ''
SET @.sgyNo = 0
SET @.pntCode ''
SET @.pntDOB '1980-01-01'
SELECT doctorNo
FROM tblReferringDoctorContacts
WHERE (docFName = @.docFName)
AND (docLName=@.docLName)
AND (sgyNo=@.sgyNo)
SELECT * FROM tblPatient WHERE ((pntUnitID = @.pntCode) AND (pntDOB =
pntDOB))
If you don't get any results from either query, then the query to update the
tblPatient will results in no changes being made because 1 or both of the
criterion isn't matched.
Using Query Analyser (included with MS SQL2K) or Visual Studio to test your
procedure / queries may offer more information from the output of your
queries better than running from your code.
Good Luck :)
Andy Furnival
"Assimalyst" wrote:

> Thank you for the replies, i've tried both suggestions, but having no
> luck. The tblPatient.doctorNo remains blank.
> Below is the relevant code and stored procedure, i'm getting no errors,
> and the code runs through the debugger without a hitch.
> string strSgyNo = System.Convert.ToString(intSgyNo);
> string strDocFName = fNameTxtBx.Text;
> string strDocLName = lNameTxtBx.Text;
> string strPatientCode = patientCodeLbl.Text;
> string strDOB = dobLbl.Text;
> // Use stored procedure to input doctorNo as foreign key in tblPatient
> SqlCommand cmd = new SqlCommand("proc_AddDocToPnt", conn);
> cmd.CommandType = CommandType.StoredProcedure;
> // Add parameters to command, which will be passed to the stored
> procedure
> cmd.Parameters.Add(new SqlParameter("@.docFName", strDocFName));
> cmd.Parameters.Add(new SqlParameter("@.docLName", strDocLName));
> cmd.Parameters.Add(new SqlParameter("@.sgyNo", strSgyNo));
> cmd.Parameters.Add(new SqlParameter("@.pntCode", strPatientCode));
> cmd.Parameters.Add(new SqlParameter("@.pntDOB", strDOB));
>
> Stored Procedure:
> CREATE PROCEDURE proc_AddDocToPnt
> (@.docFName varchar(20),
> @.docLName varchar(30),
> @.sgyNo int,
> @.pntCode varchar(15),
> @.pntDOB datetime
> )
> AS
> UPDATE tblPatient
> SET doctorNo = D.doctorNo
> FROM(SELECT doctorNo
> FROM tblReferringDoctorContacts
> WHERE (docFName = @.docFName)
> AND (docLName=@.docLName)
> AND (sgyNo=@.sgyNo)) D
> WHERE ((pntUnitID = @.pntCode) AND (pntDOB = @.pntDOB))
> GO
> Can anyone spot a problem?
> Thanks again.
>|||Thanks again,
I've run that, it seems it is finding the doctorNo from
blReferringDoctorContacts, but is not ablke to find the row from
tblPatient because the insetrts are not working properly.
If the number entered into the form is say 19/04/1945, the database
stores the date as 19/04/2001? Always changes the year to 2001!'
Very strange, but hence it cannot find the record. Think i'll need to
sort this before proceding with the above.
Thanks again|||OK solved that, i'd declared the pntDOB ad a varchar instead of a
datetime in the stored procedure oops!
Since tried again though, and i'm still not getting a row selected from
tblPatient.
i've set @.pntCode = 'CO123' and @.pntDOB = '10/03/1950' a row exists in
the tblPatient where pntUnitID = CO123 and pntDOB = 10/03/1950, but it
is not being found any ideas why?
Thanks again|||The problem seems to be with the date, although it is displayed in the
table as 10/03/1950 it is actually stored as 1950-03-10. if i change
@.pntDOB to 1950-03-10 it works. :)|||Dates in SQL can be ambigious. Therefore 10/03/1950 could be interpreted as
either 10-March-1950 or 3-October-1950.
When using datatimes in sql use the data format yyyy-mm-dd so set your
@.pndDOB = '1950-03-10'
To see how your dates are being interpreted run the code with your @.pndDOB
set.
CONVERT(SMALLDATETIME, @.pndDOB, 112) -- 112 is the yyyy-mm-dd format
Thanks
Andy
"Assimalyst" wrote:

> OK solved that, i'd declared the pntDOB ad a varchar instead of a
> datetime in the stored procedure oops!
> Since tried again though, and i'm still not getting a row selected from
> tblPatient.
> i've set @.pntCode = 'CO123' and @.pntDOB = '10/03/1950' a row exists in
> the tblPatient where pntUnitID = CO123 and pntDOB = 10/03/1950, but it
> is not being found any ideas why?
> Thanks again
>|||Thank you again.
Forgive my stupidity but i'm not sure how to implement what you're
suggesting.
Do you mean to change the query SET to
SET @.pntDOB = CONVERT(SMALLDATETIME, @.pntDOB, 112)
I've tried this, and get now matched rows. I've also tried
SET @.pntDOB = CONVERT(SMALLDATETIME, 10/03/1950, 112)
and again get no matches. the only way i've found so far is to manually
type
SET @.pntDOB = '1950-03-10' but i really need to implement this convert.
Can you elaborate?
Thankssqlsql

Sunday, February 12, 2012

Converting Oracle Encode Function into T-SQL syntax

I am looking for the correct syntax to convert Oracle encode function into T-SQl syntax. Please provide specific syntax and examples.
Thank you.Don't you mean 'decode' function?|||did you see my reply on this thread (http://dbforums.com/showthread.php?s=&threadid=444782) ?

rudy