Showing posts with label number. Show all posts
Showing posts with label number. Show all posts

Thursday, March 29, 2012

Calling Stored Procedure to get results into another stored procedure

I have a complex stored procedure which in the end, has a select statement and returns a number of rows... such as: Select * from Temp_Table.

I want to access these records in another stored procedure and do some maniulation on the rows, such as sum them or perform other calculations.

How can I access these rows via a select statement or just insert them into a temp table in the 2nd stored procedure?

Thanks so much

Stored Procedures can't be referenced in the FROM clause.

But you can use them as input to an INSERT statement.

Code Snippet

createtable #t1(a int, b int, c int)

insertinto #t1

exec dbo.myproc

|||

One way to do this is to create the temp table in the calling procedure and then load it in the called procedure:

ifobject_id('spy')isnotnulldropproc spy

go

createproc spy as

insert #x values(1)

return

go

ifobject_id('spx')isnotnulldropproc spx

go

createproc spx as

createtable #x(x1 int)

exec spy

select*from #x

return

go

exec spx

Ron Rice

|||

Thanks for the response...

I converted the Stored Proc to an inline table function. This seems to do the trick.

Tks

Thursday, March 22, 2012

Calling one Sp from Another

Hi,
I have a stored procedure (say sp_1) that returns a number.
A simplified form of sp_1 could be
Create procedure sp_1 as
Select 1
Go
Now, I would like to call sp_1 from another sp (say sp_2)
and store the value in a variable. I tried this
Create procedure sp_2 as
Declare @.xyz as integer
Set @.xyz = (exec sp_1)
This does not work.
Could somebody point me the mistake?
Thanks,
RamThis is a multi-part message in MIME format.
--=_NextPart_000_01C5_01C3CDFB.55950220
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Try;
create proc sp1
( @.parm int output)
as
select @.parm = 1
go
create proc sp2
as
declare @.p as int
exec sp1 @.p output
go
Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
"Ram" <anonymous@.discussions.microsoft.com> wrote in message
news:04b401c3ce22$4b1c9480$a001280a@.phx.gbl...
Hi,
I have a stored procedure (say sp_1) that returns a number.
A simplified form of sp_1 could be
Create procedure sp_1 as
Select 1
Go
Now, I would like to call sp_1 from another sp (say sp_2)
and store the value in a variable. I tried this
Create procedure sp_2 as
Declare @.xyz as integer
Set @.xyz = (exec sp_1)
This does not work.
Could somebody point me the mistake?
Thanks,
Ram
--=_NextPart_000_01C5_01C3CDFB.55950220
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

Try;
create proc sp1
( @.parm int =output)
as
select @.parm =3D 1
go
create proc sp2
as
declare @.p as int
exec sp1 @.p output
go
-- Tom
---T=homas A. Moreau, BSc, PhD, MCSE, MCDBASQL Server MVPColumnist, SQL =Server ProfessionalToronto, ON Canadahttp://www.pinnaclepublishing.com/sql">www.pinnaclepublishing.com=/sql
"Ram" wrote in message news:04b401c3ce22$4b=1c9480$a001280a@.phx.gbl...Hi,I have a stored procedure (say sp_1) that returns a number.A =simplified form of sp_1 could beCreate procedure sp_1 asSelect =1GoNow, I would like to call sp_1 from another sp (say sp_2) and store the =value in a variable. I tried thisCreate procedure sp_2 as Declare @.xyz =as integerSet @.xyz =3D (exec sp_1)This does not =work.Could somebody point me the =mistake?Thanks,Ram

--=_NextPart_000_01C5_01C3CDFB.55950220--|||Hi,
Create a Linked server from server 2 and then call the SP_1
Thanks
Hari
MCDBA
"Ram" <anonymous@.discussions.microsoft.com> wrote in message
news:04b401c3ce22$4b1c9480$a001280a@.phx.gbl...
> Hi,
> I have a stored procedure (say sp_1) that returns a number.
> A simplified form of sp_1 could be
> Create procedure sp_1 as
> Select 1
> Go
> Now, I would like to call sp_1 from another sp (say sp_2)
> and store the value in a variable. I tried this
> Create procedure sp_2 as
> Declare @.xyz as integer
> Set @.xyz = (exec sp_1)
> This does not work.
> Could somebody point me the mistake?
>
> Thanks,
> Ram
>|||This is a multi-part message in MIME format.
--=_NextPart_000_01F3_01C3CDFC.4D684E80
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: 7bit
Why would you need a linked server for this?
--
Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
"Hari Prasad" <hari_prasad_k@.hotmail.com> wrote in message
news:#aYouVizDHA.2308@.TK2MSFTNGP11.phx.gbl...
Hi,
Create a Linked server from server 2 and then call the SP_1
Thanks
Hari
MCDBA
"Ram" <anonymous@.discussions.microsoft.com> wrote in message
news:04b401c3ce22$4b1c9480$a001280a@.phx.gbl...
> Hi,
> I have a stored procedure (say sp_1) that returns a number.
> A simplified form of sp_1 could be
> Create procedure sp_1 as
> Select 1
> Go
> Now, I would like to call sp_1 from another sp (say sp_2)
> and store the value in a variable. I tried this
> Create procedure sp_2 as
> Declare @.xyz as integer
> Set @.xyz = (exec sp_1)
> This does not work.
> Could somebody point me the mistake?
>
> Thanks,
> Ram
>
--=_NextPart_000_01F3_01C3CDFC.4D684E80
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

Why would you need a linked server for =this?
-- Tom
---T=homas A. Moreau, BSc, PhD, MCSE, MCDBASQL Server MVPColumnist, SQL =Server ProfessionalToronto, ON Canadahttp://www.pinnaclepublishing.com/sql">www.pinnaclepublishing.com=/sql
"Hari Prasad" Hi,> I have a stored procedure (say sp_1) that returns a =number.> A simplified form of sp_1 could be>> Create procedure sp_1 =as> Select 1> Go>> Now, I would like to call =sp_1 from another sp (say sp_2)> and store the value in a variable. I =tried this>> Create procedure sp_2 as> Declare @.xyz as integer> Set @.xyz =3D (exec sp_1)>> This does not work.>> Could somebody point me the mistake?>>> Thanks,> Ram>

--=_NextPart_000_01F3_01C3CDFC.4D684E80--|||Hi,
You can use output parameter.
create procedure sp_1 @.x int output
as
select @.x=1
go
-- executing above sp.
declare @.xx int
exec sp_1 @.xx output
print @.xx
--
- Vishal|||This is a multi-part message in MIME format.
--=_NextPart_000_00B2_01C3CE55.AA81C840
Content-Type: text/plain;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
Sorry Tom, you are correct, I misunderstood SP2 as server 2.
Thanks
Hari
MCDBA
"Tom Moreau" <tom@.dont.spam.me.cips.ca> wrote in message =news:#CBH8YizDHA.1744@.TK2MSFTNGP12.phx.gbl...
Why would you need a linked server for this?
-- Tom
---
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinnaclepublishing.com/sql
"Hari Prasad" <hari_prasad_k@.hotmail.com> wrote in message =news:#aYouVizDHA.2308@.TK2MSFTNGP11.phx.gbl...
Hi,
Create a Linked server from server 2 and then call the SP_1
Thanks
Hari
MCDBA
"Ram" <anonymous@.discussions.microsoft.com> wrote in message
news:04b401c3ce22$4b1c9480$a001280a@.phx.gbl...
> Hi,
> I have a stored procedure (say sp_1) that returns a number.
> A simplified form of sp_1 could be
>
> Create procedure sp_1 as
> Select 1
> Go
>
> Now, I would like to call sp_1 from another sp (say sp_2)
> and store the value in a variable. I tried this
>
> Create procedure sp_2 as
> Declare @.xyz as integer
> Set @.xyz =3D (exec sp_1)
>
> This does not work.
>
> Could somebody point me the mistake?
>
>
> Thanks,
> Ram
>
--=_NextPart_000_00B2_01C3CE55.AA81C840
Content-Type: text/html;
charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
&

Sorry Tom, you are correct, I =misunderstood SP2 as server 2.
Thanks
Hari
MCDBA
"Tom Moreau" = wrote in message news:#CBH8YizDHA.1744=@.TK2MSFTNGP12.phx.gbl...
Why would you need a linked server =for this?
-- Tom

=---T=homas A. Moreau, BSc, PhD, MCSE, MCDBASQL Server MVPColumnist, SQL =Server ProfessionalToronto, ON Canadahttp://www.pinnaclepublishing.com/sql">www.pinnaclepublishing.com=/sql


"Hari Prasad" Hi,> I have a stored procedure (say sp_1) that returns a number.> A simplified form of sp_1 could be>> =Create procedure sp_1 as> Select 1> Go>> Now, I =would like to call sp_1 from another sp (say sp_2)> and store the =value in a variable. I tried this>> Create procedure sp_2 =as> Declare @.xyz as integer> Set @.xyz =3D (exec =sp_1)>> This does not work.>> Could somebody point me the mistake?>>> Thanks,> Ram>

--=_NextPart_000_00B2_01C3CE55.AA81C840--|||This will not work
> Create procedure sp_2 as
> Declare @.xyz as integer
> Set @.xyz = (exec sp_1)
because the sproc sp_1 will get you the return value.
You should always have a
RETURN some_integer
at the end of each sproc to inform the caller about the status.
so
> Create procedure sp_1 as
> Select 1
> RETURN 20 << added this
> Go
Then
> Declare @.xyz as integer
> EXEC @.xyz = (exec sp_1)
@.xyz will be 20 and not 1.
There is another way besides output params you can use to store the
resultset from the sproc:
CREATE PROC sp_1
AS
SELECT 'ABCD', 10
UNION
SELECT 'EFGH', 20
RETURN @.@.ERROR
GO
CREATE TABLE TAB1 (F1 VARCHAR(10), F2 INT)
GO
-- **************************
INSERT INTO TAB1
EXEC sp_1
GO
-- **************************
SELECT * FROM TAB1
GO
"Ram" <anonymous@.discussions.microsoft.com> wrote in message
news:04b401c3ce22$4b1c9480$a001280a@.phx.gbl...
> Hi,
> I have a stored procedure (say sp_1) that returns a number.
> A simplified form of sp_1 could be
> Create procedure sp_1 as
> Select 1
> Go
> Now, I would like to call sp_1 from another sp (say sp_2)
> and store the value in a variable. I tried this
> Create procedure sp_2 as
> Declare @.xyz as integer
> Set @.xyz = (exec sp_1)
> This does not work.
> Could somebody point me the mistake?
>
> Thanks,
> Ram
>|||Thanks for the solutions Daniel, Tom and Vishal. It worked.
Ram
>--Original Message--
> Hi,
> You can use output parameter.
> create procedure sp_1 @.x int output
> as
> select @.x=1
> go
> -- executing above sp.
> declare @.xx int
> exec sp_1 @.xx output
> print @.xx
> --
> - Vishal
>
>.
>sql

Sunday, March 11, 2012

Calling a single UDF multiple times within 1 View

Lets say I have a User Defined Function. I need to call the UDF a number of times within the same View. However, the results are not as expected. The only way (I know of) is to have seperate instances on the same function saved under different names. Is there a way to call a single UDF multiple times within a View without the returned values being messed-up?read the sticky at the top of the forum, and why not post what you have.|||Sorry about the ambiguity, missed the new sticky.

I had this problem a few weeks ago, and ended-up having to create multiple instances of the UDF. However, I am now having problems recreating the error and was hoping that someone would have come accross what I described.|||Calling a function multiple times within a view will not screw anything up. There is something else fishy going on. Follow Brett's posting advice.

Calling a Function from a Stored Procedure

Hello all,

I'm trying to construct a select statement in a stored procedure that filters based on the returned values of a number of functions. My function works fine, but when I try to call the function from the stored procedure I get an error.

I'm going to try explain the thought process behind what I'm doing. Hope I make enough sense.
The purpose of the stored procedure is to perform a wildcard search on a tool. The tool contains a number of FK that link to different tables (e.g., manufacturer, vendor). So I'm creating functions that also search the manufacturer and vendor and return the matching IDs.

Example of tool SELECT statement:

SELECT tool_number, tool_descriptionFROM toolWHERE tool_manufacturerIN (UDFmanufacturer_SearchName(@.search_string)

This gives me an error:
'UDFmanufacturer_SearchName' is not a recognized built-in function name.

Function code (removed some wrapping code for simplicity):

SELECT manufacturer_idFROM manufacturerWHERE manufacturer_nameLIKE'%' + @.search_string +'%'
These statements both work if I run a independent query: 
SELECT *FROM UDFmanufacturer_SearchName('mol')

SELECT *FROM toolWHERE tool_manufacturerIN (SELECT *FROM UDFmanufacturer_SearchName('mol'))

This code fails:

SELECT *FROM ato_toolWHERE ato_tool_manufacturerIN (UDFmanufacturer_SearchName('mol')) 
I'm stuck. I haven't been able to find anything that shows me where I'm going wrong. Any thoughts or suggestions are appreciated.

Thanks,
Jay


You have to use a SELECT FROM function and not directly in the IN clause. Also use the prefix "dbo." for the function and see if it helps.

SELECT *FROM toolWHERE tool_manufacturerIN (SELECT *FROMdbo.UDFmanufacturer_SearchName('mol'))
|||

That was it. I thought I could use the function without needing the " SELECT * " in front.

Thanks for your help.

|||

The above post is absolutely correct.

UDFmanufacturer_SearchName is a table valued function (fancy for it returns a table/resultset). If you then think of UDFmanufacturer_SearchName('mol') as being a table, then

SELECT *
FROM ato_tool
WHERE ato_tool_manufacturerIN (UDFmanufacturer_SearchName('mol'))

isn't valid for the same reasons that:

SELECT *
FROM ato_tool
WHERE ato_tool_manufacturerIN (tool_manufacturer)

isn't valid.

So because tables aren't valid where you've placed it, it's looking for a different type of function, like one that returns a string, date, or number. It can't find one, so it says it can't find the function. Also, you should always prefix your function names with 'dbo.' or the correct schema. It may work without it in some places, but I believe syntactically it's wrong (sloppy). Don't be suprised if a service pack, hotfix, or the next version of SQL Server starts to fail on it.|||

Thanks for the explanation, it was helpful.

Hadn't thought about the schema prefix. Will keep it in mind.

Thanks.

Wednesday, March 7, 2012

call regular phone number job completes

How can I have a sql agent job call a regular (land line) phone number when the job has completed? Any way to do this?
Thanks!
You could package some kind of DLL that will do this, then call it using
sp_OA* or an EXE from the command line using xp_cmdshell.
http://www.aspfaq.com/
(Reverse address to reply.)
"AshleyT" <anonymous@.discussions.microsoft.com> wrote in message
news:67DCD721-A327-460C-A576-954D2FB24716@.microsoft.com...
> How can I have a sql agent job call a regular (land line) phone number
when the job has completed? Any way to do this?
> Thanks!

call regular phone number job completes

How can I have a sql agent job call a regular (land line) phone number when the job has completed? Any way to do this
Thanks!You could package some kind of DLL that will do this, then call it using
sp_OA* or an EXE from the command line using xp_cmdshell.
--
http://www.aspfaq.com/
(Reverse address to reply.)
"AshleyT" <anonymous@.discussions.microsoft.com> wrote in message
news:67DCD721-A327-460C-A576-954D2FB24716@.microsoft.com...
> How can I have a sql agent job call a regular (land line) phone number
when the job has completed? Any way to do this?
> Thanks!

call regular phone number job completes

How can I have a sql agent job call a regular (land line) phone number when
the job has completed? Any way to do this?
Thanks!You could package some kind of DLL that will do this, then call it using
sp_OA* or an EXE from the command line using xp_cmdshell.
http://www.aspfaq.com/
(Reverse address to reply.)
"AshleyT" <anonymous@.discussions.microsoft.com> wrote in message
news:67DCD721-A327-460C-A576-954D2FB24716@.microsoft.com...
> How can I have a sql agent job call a regular (land line) phone number
when the job has completed? Any way to do this?
> Thanks!

Friday, February 24, 2012

Calculations for number of DB's on a SQL Server 2000 server

Is there a Microsoft recommendation for how many databases that can exist on
a SQL Server 2000 (std ed) server (Windows 2000 4 CPU, 4GB RAM)? What form
ulas/calculations can be made to determine the capacity (# of db's w/o affec
ting performance/resources)
for a given server?It's not so much the number of databases that matter as much as their usage.
We have a 2 CPU box with 2 Gb of RAM that has 107 databases on it and works
great. We also have a 4CPU box with 8Gb of RAM with 67 databases on it that
works fine. We then have a 4CPU cluster with 4 Gb of RAM that has only 3
databases, the application runs fine but our database box is about maxed
out.
Basically other than disk space, a database won't consume SQL Server
resources unless it's actually used. So apart from the fixed limitation of
32,767 databases per instance there is no concern about the number of
databases. It's all about their utilization.
"LAM" <anonymous@.discussions.microsoft.com> wrote in message
news:2F6DFF19-F6B1-446A-85FA-0BDA53230CF1@.microsoft.com...
> Is there a Microsoft recommendation for how many databases that can exist
on a SQL Server 2000 (std ed) server (Windows 2000 4 CPU, 4GB RAM)? What
formulas/calculations can be made to determine the capacity (# of db's w/o
affecting performance/resources) for a given server?

Calculations for number of DB's on a SQL Server 2000 server

Is there a Microsoft recommendation for how many databases that can exist on a SQL Server 2000 (std ed) server (Windows 2000 4 CPU, 4GB RAM)? What formulas/calculations can be made to determine the capacity (# of db's w/o affecting performance/resources) for a given server?It's not so much the number of databases that matter as much as their usage.
We have a 2 CPU box with 2 Gb of RAM that has 107 databases on it and works
great. We also have a 4CPU box with 8Gb of RAM with 67 databases on it that
works fine. We then have a 4CPU cluster with 4 Gb of RAM that has only 3
databases, the application runs fine but our database box is about maxed
out.
Basically other than disk space, a database won't consume SQL Server
resources unless it's actually used. So apart from the fixed limitation of
32,767 databases per instance there is no concern about the number of
databases. It's all about their utilization.
"LAM" <anonymous@.discussions.microsoft.com> wrote in message
news:2F6DFF19-F6B1-446A-85FA-0BDA53230CF1@.microsoft.com...
> Is there a Microsoft recommendation for how many databases that can exist
on a SQL Server 2000 (std ed) server (Windows 2000 4 CPU, 4GB RAM)? What
formulas/calculations can be made to determine the capacity (# of db's w/o
affecting performance/resources) for a given server?

Calculations for number of DB's on a SQL Server 2000 server

Is there a Microsoft recommendation for how many databases that can exist on a SQL Server 2000 (std ed) server (Windows 2000 4 CPU, 4GB RAM)? What formulas/calculations can be made to determine the capacity (# of db's w/o affecting performance/resources)
for a given server?
It's not so much the number of databases that matter as much as their usage.
We have a 2 CPU box with 2 Gb of RAM that has 107 databases on it and works
great. We also have a 4CPU box with 8Gb of RAM with 67 databases on it that
works fine. We then have a 4CPU cluster with 4 Gb of RAM that has only 3
databases, the application runs fine but our database box is about maxed
out.
Basically other than disk space, a database won't consume SQL Server
resources unless it's actually used. So apart from the fixed limitation of
32,767 databases per instance there is no concern about the number of
databases. It's all about their utilization.
"LAM" <anonymous@.discussions.microsoft.com> wrote in message
news:2F6DFF19-F6B1-446A-85FA-0BDA53230CF1@.microsoft.com...
> Is there a Microsoft recommendation for how many databases that can exist
on a SQL Server 2000 (std ed) server (Windows 2000 4 CPU, 4GB RAM)? What
formulas/calculations can be made to determine the capacity (# of db's w/o
affecting performance/resources) for a given server?

Calculations

I have to perform a number of calculations on a table but these
calculations need intermediate values ( like for example I have to
first calculate one value before I can use it in the next step of the
calcualtion). There are two ways of doing this:
1. Have columns for intermediate values ( it is possible as my
calculations are on a temp tbl) and use update statements. One update
statement for every step in the calculation.
2. Have a cursor, loop through the resultset and do the calculations
like we would in a programming language like C#.
Which one is better? Are updates ( a number of them) faster than a
cursor?
Thanks.Cursors are usually slower than an SQL operation that updates all the rows i
n
one go.
I would only use a cursor for this sort of operation if there were any
locking issues.
Are Riksaasen
"John Smith" wrote:

> I have to perform a number of calculations on a table but these
> calculations need intermediate values ( like for example I have to
> first calculate one value before I can use it in the next step of the
> calcualtion). There are two ways of doing this:
> 1. Have columns for intermediate values ( it is possible as my
> calculations are on a temp tbl) and use update statements. One update
> statement for every step in the calculation.
> 2. Have a cursor, loop through the resultset and do the calculations
> like we would in a programming language like C#.
> Which one is better? Are updates ( a number of them) faster than a
> cursor?
> Thanks.
>

Sunday, February 19, 2012

calculation with dates challenge

I have a start and end datetime field and a field with the lunchbreak in
minutes. Now I need to find out the number of hours and minutes (HH:mm) this
person has worked.
endtime - starttime - lunchbreak and formatted in HH:mm.
Suggestions on how to go about this in Reporting services ?
PeterPeter,
You will want to do the following:
1) Do a 'DateDiff' and convert the endtime-starttime into minutes
e.g. DateDiff(m, Date1, Date2)
2) Do simple math to subtract the lunch break (if not in minutes, then
convert)
3) Divide the answer by 60 to get your hours
4) Take total minutes minus (60*hours) and this is your remainder minutes
5) format as desired.
if you require more info (ie, how to do date diff, or write it) let me know.
Michael
"Peter De Rop" wrote:
> I have a start and end datetime field and a field with the lunchbreak in
> minutes. Now I need to find out the number of hours and minutes (HH:mm) this
> person has worked.
> endtime - starttime - lunchbreak and formatted in HH:mm.
> Suggestions on how to go about this in Reporting services ?
> Peter

Calculation using count function in SQL server Business Intelligence development studio

Hello All,

I am working on a report using SQL server Business Intelligence development studio in which I need to count the total number of saleslines that either has a status : Delivered or Invoiced.

I am using the inbuilt count function under the calcuations, as: Count(Fields!SalesStatus.Value, scope). I am not sure what scope means here however, I read that scope refers to some dataset, dataregion or grouping. I dont know what that means. I shall be really thankful if someone can help me with this.

Regards,

Rashi

Scope is optional and if not given, will refer to the current scope.

The current scope is exactly what you've said. If your dataset name is "dataset1" and you put a textbox outside a table or matrix and put the value: Count(Fields!SalesStatus.Value, "dataset1"), you'll get the accurate count of all items within that dataset.

If you were doing some grouping inside your table and wanted to get the count for each group, in a group header or footer, you could do: Count(Fields!SalesStatus.Value, "table1_YourGroupName") to acheive this.|||An alternative approach to using the Count function would be to add a calculated column to your dataset, setting the value to 0 or 1 depending on whether the row meets your conditions. Then sum the calculated column.|||

Thanks for your response!

Well, the dataset is salesstatus which is a field in the salestable. Now, this field "salesstatus" has some values like: invoiced, delivered, pending, on hold etc on each line-item. I just want to count the line items that have a sales status equals to delievered or invoiced. I am not sure if there is a way to calculate that. I am doing reporting for the first time so I might not be sure.

What I am trying to achieve is to calculate the total number of orders shipped. For this, I am thinkin if I can get a count of lineitems that have a salesstatus = delivered or invoiced I can acheive that. I would appreciate your response on this one.

Thanks,

Rashi

|||Try adding a calculated field with "=iif(fields!salesstatus = "delivered", 1, 0)" (freehanding this so it may not be 100% correct syntax, but it's close). To check additional values, you could use nested a nested iif, or the switch function.|||You can group by salestatus then count the group. You might want to add drill down capability on this group.|||

Thanks all

I have been able to figure it out. I grouped all and used a count function and it just works great!!!

Thanks again,

Rashi

Thursday, February 16, 2012

Calculation between two rows

Hi,
I have a table and data set to populate it. I need to calculate
difference between two rows (lets say the number of items below) and
want to show the difference on another column on each row as below.
Does anyone know how to do that?
Table
Name #Items Date Difference
Bla Bla 60 05-06-2006 -40
Bla Bla 100 05-06-2006 20
Bla Bla 80 04-06-2006 0
Regards,
Alp Eren YILMAZAlp Eren schreef:
> Hi,
> I have a table and data set to populate it. I need to calculate
> difference between two rows (lets say the number of items below) and
> want to show the difference on another column on each row as below.
> Does anyone know how to do that?
> Table
> Name #Items Date Difference
> Bla Bla 60 05-06-2006 -40
> Bla Bla 100 05-06-2006 20
> Bla Bla 80 04-06-2006 0
> Regards,
>
> Alp Eren YILMAZ
Alp,
Something like currentmember-nextmember.......
Ronald|||On the Difference column's textbox right click and click on Expressions and
type the folliwing
=Fields!Item.Value - Previous(Fields!Item.Value)
This will give you the desired result.
Sum.
"Alp Eren" wrote:
> Hi,
> I have a table and data set to populate it. I need to calculate
> difference between two rows (lets say the number of items below) and
> want to show the difference on another column on each row as below.
> Does anyone know how to do that?
> Table
> Name #Items Date Difference
> Bla Bla 60 05-06-2006 -40
> Bla Bla 100 05-06-2006 20
> Bla Bla 80 04-06-2006 0
> Regards,
>
> Alp Eren YILMAZ
>|||Hi,
Thanks for the answers, a couple of tips
1- Change the sort order for the table data so you can use Previous
2- To reach the previous row data use Previous(..) function
= Previous( Fields!ColXXX.Value)-Fields!ColXXX.Value
MSDN Reference: http://msdn2.microsoft.com/en-us/ms156372(SQL.90).aspx
3- For the first row, avoid computation with iif and RowNumber(..)
check
Regards,
Alp Eren YILMAZ

Calculating the total number of open account between February - April

I submitted this issue to the reporting services thread, but I'm beginning to think I may have to do something in SSAS:

I want to enable my users to select 2 random month end dates in a report parameter. After selecting these dates, I want to be able to take the corresponding financial values, be able to line them up next to each other in a report, and calculate the difference. Obviously in a matrix I can line the values up, but I am then not able to calc a difference. I do have a time hierarchy that allows me to do this across dates at the same level of granularity, i.e. year to year, qtr to qtr, etc., but if my users want to compare February month end to November month end, it's a problem. I am more than willing to add calculated members to the cube to get this done, I just haven't figured out the MDX for it.

Thanks in Advance...

I think this can be done without any calculated members, simply by using the following query:

SELECT

{Measures.ImportantFinancialValue1, Measures.ImportantFinancialValue2, ...} ON COLUMNS

, {Time.Date1, Time.Date2} ON ROWS

FROM Cube

HTH,

Mosha (http://www.mosha.com/msolap)

|||

Mosha-

Thanks for your quick response. I am using SSAS on top of SSRS, and am trying to avoid writing MDX as these reports may get very involved. Can I replicate this in a calc'd member, can it be dynamic enough to accept values at runtime?

|||I am supposed to find out the total number of open account between February - April and i am having difficulties. Below is a working statement for SQL which i am supposed to do in MDX for my Cube:

SELECT COUNT(*) AS Expr1

FROM ConsumerAccount

WHERE (DATEPART(yyyy, AccountOpenedDate) = '2007')

AND (DATEPART(mm, AccountOpenedDate) IN (02, 03, 04))

I have a cube called Batch process, column AccountOpenedDate and CrearedOnDate, how do i do it

Calculating the days of the month?

Hi everyone,
Can anyone tell me how I could find out the number of days that there are
in any given month using TSQL?
I was hoping there was function I could use?
Any help would be much appreciated
Simon"Simon Harvey" <nothanks@.hotmail.com> wrote in message
news:7c72785b25e6f8c80ce0f3cef08d@.news.microsoft.com...
> Hi everyone,
> Can anyone tell me how I could find out the number of days that there are
> in any given month using TSQL?
> I was hoping there was function I could use?
> Any help would be much appreciated
> Simon
Hi Simon,
Easily done using a calendar table.
http://www.aspfaq.com/show.asp?id=2519|||you could use something like this
declare @.d datetime
select @.d = getdate()
select datename(m,@.d) , datediff(d,@.d,dateadd(m,1,@.d))
so for example for today (March, current month) we could do this:
select datename(m,getdate()) ,
datediff(d,getdate(),dateadd(m,1,getdate
()))
or for February 2006 you can use this
declare @.d datetime
select @.d = '20060201'
select datename(m,@.D) , datediff(d,@.d,dateadd(m,1,@.d))
http://sqlservercode.blogspot.com/|||scrap that, that won't work unless you use the first day of the month
declare @.d datetime
select @.d = '20060130'
select datename(m,@.D) , datediff(d,@.d,dateadd(m,1,@.d)) -- this will
give a wrong result
this is fine
declare @.d datetime
select @.d = '20060101'
select datename(m,@.D) , datediff(d,@.d,dateadd(m,1,@.d))
http://sqlservercode.blogspot.com/|||CREATE FUNCTION dbo.fDaysOfMonth(@.dtFecha DATETIME)
RETURNS INT
AS
BEGIN
RETURN DAY(DATEADD(ms, -3, DATEADD(mm, DATEDIFF(m, 0, @.dtFecha) + 1, 0)))
END
GO
SELECT dbo.fDaysOfMonth('2006-02-03')
28

Tuesday, February 14, 2012

Calculating problem in tables

Hi,
in my report I have two tables, that look like this
--table1
x y z (=y-x) where x is a database field, y is a hard number
en z is the result of y-x
--table2
x y z (=y-x) where x is a database field, y is a hard number
en z is the result of y-x
--
I want to sum the x, y and z values in another table.
the sum of the database field (x) isn't a problem, but
the sum of y and z doesn't work,
I've tried this: (textbox1.Value, "table1")+(textbox2.Value, "table2")
but it's not possible to do this with textboxes.
Can anyone help me with this problem?Dirkjan van Groeningen wrote:
> I've tried this: (textbox1.Value, "table1")+(textbox2.Value, "table2")
> but it's not possible to do this with textboxes.
> Can anyone help me with this problem?
I dont know...have you tried an expression like this?
=Fields!textbox1.Value + Fields!textbox2.value
regards
Frank
www.xax.de|||Hi Frank,
I've tried it, even with the expression , "table1" behind the textboxes to
set the dataset scope. Got the following error
The value expression for the textbox â'textbox11â' refers to the field
â'textbox9â'. Report item expressions can only refer to fields within the
current data set scope or, if inside an aggregate, the specified data set
scope.
"Frank Matthiesen" wrote:
> Dirkjan van Groeningen wrote:
> > I've tried this: (textbox1.Value, "table1")+(textbox2.Value, "table2")
> > but it's not possible to do this with textboxes.
> >
> > Can anyone help me with this problem?
> I dont know...have you tried an expression like this?
> =Fields!textbox1.Value + Fields!textbox2.value
> regards
> Frank
> www.xax.de
>
>|||Try:
=ReportItems!textbox1.Value + ReportItems!textbox2.value
--
Cheers,
'(' Jeff A. Stucker
\
Business Intelligence
www.criadvantage.com
---
"Frank Matthiesen" <fm@.xax.de> wrote in message
news:34i2d5F4aqquuU1@.individual.net...
> Dirkjan van Groeningen wrote:
>> I've tried this: (textbox1.Value, "table1")+(textbox2.Value, "table2")
>> but it's not possible to do this with textboxes.
>> Can anyone help me with this problem?
> I dont know...have you tried an expression like this?
> =Fields!textbox1.Value + Fields!textbox2.value
> regards
> Frank
> www.xax.de
>

calculating pecent

Hi,
Iâ'm trying to find a way to calculate percentage of number of customer for
reporting purposes. (Using reporting services)
In analysis manger I have one dimension called area and one measure called
numberOfCustomer. I want to find how many percent of the customers applies to
each area if the total number of customers is 100%.
Example in a report:
---
USA # of customer
North 5 20 %
South 6 24 %
Europe
Germany 10 40 %
UK 4 16 %
Totally 25 100 %
Same calculation (number of customers and percent) should be done for other
dimensions as well in other reports.
How do I calculate a general percent for number of customer? Is it better to
calculate this via a â'Calculated memberâ' in the cube or should I solve it in
reports?
Any hints? Thanks NilofarWe are just starting with RS. The solution we came up with was to use a
"Calculated Field"
We also came to the conclusion that this may be better handled in the cube
in the future.
"Nilofar" wrote:
> Hi,
> Iâ'm trying to find a way to calculate percentage of number of customer for
> reporting purposes. (Using reporting services)
> In analysis manger I have one dimension called area and one measure called
> numberOfCustomer. I want to find how many percent of the customers applies to
> each area if the total number of customers is 100%.
> Example in a report:
> ---
> USA # of customer
> North 5 20 %
> South 6 24 %
> Europe
> Germany 10 40 %
> UK 4 16 %
> Totally 25 100 %
> Same calculation (number of customers and percent) should be done for other
> dimensions as well in other reports.
> How do I calculate a general percent for number of customer? Is it better to
> calculate this via a â'Calculated memberâ' in the cube or should I solve it in
> reports?
> Any hints? Thanks Nilofar

Calculating number of subscriptions in time

Hi all,

I'm new to SQL AS and MDX in general but I'm trying to build the following and maybe you guys can help me.

I have a snowflake database with a table containing subscription. Each subscription has a date/time at which it starts and a date/time at which it is terminated:

subscription:

id

start_date

end_date

....

100

10-jul-2005 17:00

10-aug-2005 11:00

...

101

11-jun-2005 17:00

12-dec-2006 18:00

...

102

1-may-2005 17:00

17-jan-2007 13:00

...

103

10-apr-2005 17:00

10-sep-2005 15:00

...


And there is a dimension-table called datetime:

id

year

month

....

10-jul-2005 17:00

2005

06

...

10-jul-2005 18:00

2005

06

...

...

..

..

...

11-jul-2005 17:00

2005

06

...

11-jul-2005 18:00

2005

06

...

AS will generate the count-measures for start-date and end-date (e.g. the number of subscriptions started at any given moment: 1 started at 10-jul-2005 17:00). But I also want to know how many subscription are active at any given moment (e.g. 0 at 1-jan-2005 12:00, but 2 at 1-jan-2006 12:00 and 4 at 1-aug-2005 12:00). In SQL I would produce something like 'select count(*) from subscription where measuredate between start_date and end_date' but how do you create something like that in AS using MDX?

TIA,
Ferdinand

Not sure how well this Adventure Works query corresponds to your scenario, but it computes the number of products that are "active" during the selected Calendar Date member:

>>

With

Member [Measures].[ProductCount] as

Count(Filter(existing [Product].[Product].[Product],

[Product].[Start Date].MemberValue <=

ClosingPeriod([Date].[Calendar].[Date]).MemberValue

AND ([Product].[End Date].CurrentMember is

[Product].[End Date].[Active]

OR [Product].[End Date].MemberValue >=

OpeningPeriod([Date].[Calendar].[Date]).MemberValue)))

select

[Date].[Calendar Year].Members *

{[Measures].[ProductCount]} on 0,

[Product].[Category].Members on 1

from [Adventure Works]

All Periods CY 2001 CY 2002 CY 2003 CY 2004
ProductCount ProductCount ProductCount ProductCount ProductCount
All Products 606 283 411 534 406
Accessories 35 3 13 32 22
Bikes 125 30 65 95 60
Clothing 48 7 28 41 20
Components 189 34 96 157 95

>>

|||

Sorry for my 'not so very fast'-reply, but I had some other bussines to attend to. The above example is not entirly what I need. I need to create three measures with the model I posted:

1. I need to count all activations (e.g. all subscriptions that are activated (=start_date) at an given moment

2. All terminations (=end_date) at an given moment

3. All active subscriptions (= given date between start_date and end_date) at an given moment.

1 and 2 are relativly simple. These are accomplshed by using the generated [Count of Subscription] measure and the also generated [Start DateTime] and [End DateTime] dimensions. But the third measure is not so simple (for me that is). Has anyone got a clue on this?

The pivot I whish to create will look something like this:

Number of subscriptions

Number of activations

Number of terminations

01-jan-2007

100

1

0

02-jan-2007

101

0

5

03-jan-2007

96

0

0

04-jan-2007

96

0

0

|||

Well, in your report there is only one date for number of subscriptions, there isn't start_date and end_date. So I assume it means number of active subscriptions at that date. The standard way of doing it is maintaining in fact table measure "Number of subscriptions", and then you will put 1 for it when there is an activation and -1 when there is a termination. By implementing running sum calculation on the lowest level of Date dimension - you will get what you need. This problem is the same as classic inventory management problem - you can read more about it here: http://www.sqlserveranalysisservices.com/OLAPPapers/InventoryManagement%20in%20AS2005v2.htm

|||

Thanks for your tip Mosha.

I've created a view where there is a 1 for each activation and a -1 for each termination and indeed when you sum them up at a specified day the number of active subscriptions is acurate. I use this MDX script to create the sum at the specified day:

CREATE MEMBER CurrentCube.[Measures].[Number of active subscriptions] as
sum
(
[Date Time].[Day].Level.members(0):ClosingPeriod([Date Time].[Day].Level),
[Measures].[SubSum]
),
visible=true;

However, this script does not seem to work at other levels then day (my time dimension contains two hierarchies: year->quarter->month->day->hour and year->week->day->hour). The results at month level for example is the result of the sum of all records the fall within that month and not the sum of all records up to the first day of the month (which should be the case). Same goes for week, quarter and year level. The lowest level (hour) works fine, that is, it contains the same value as for the whole day.

Can you tell me what I'm doing wrong?

|||

I don't think at the month level you want to sum up to the first day of the month. I think you want to get the sum up to the current day. While it looks like this problem is for LastNonEmptyChild semiadditive calculation, it actually can be solved more efficiently and still give correct results with LastChild. And if you don't want to work with LastChild semiadditive aggregation, you can simulate it with the series of lines like

SCOPE (Measures.[Number of active subscriptions);

[Date Time].[YQMDH].[Month] = [Date Time].[YQMDH].LastChild;

[Date Time].[YQMDH].[Quarter] = [Date Time].[YQMDH].LastChild.LastChild;

[Date Time].[YQMDH].[Year] = [Date Time].[YQMDH].LastChild.LastChild.LastChild;

// etc

END SCOPE;

|||Thanks, that last part did the trick!

Calculating number of subscriptions in time

Hi all,

I'm new to SQL AS and MDX in general but I'm trying to build the following and maybe you guys can help me.

I have a snowflake database with a table containing subscription. Each subscription has a date/time at which it starts and a date/time at which it is terminated:

subscription:

id

start_date

end_date

....

100

10-jul-2005 17:00

10-aug-2005 11:00

...

101

11-jun-2005 17:00

12-dec-2006 18:00

...

102

1-may-2005 17:00

17-jan-2007 13:00

...

103

10-apr-2005 17:00

10-sep-2005 15:00

...


And there is a dimension-table called datetime:

id

year

month

....

10-jul-2005 17:00

2005

06

...

10-jul-2005 18:00

2005

06

...

...

..

..

...

11-jul-2005 17:00

2005

06

...

11-jul-2005 18:00

2005

06

...

AS will generate the count-measures for start-date and end-date (e.g. the number of subscriptions started at any given moment: 1 started at 10-jul-2005 17:00). But I also want to know how many subscription are active at any given moment (e.g. 0 at 1-jan-2005 12:00, but 2 at 1-jan-2006 12:00 and 4 at 1-aug-2005 12:00). In SQL I would produce something like 'select count(*) from subscription where measuredate between start_date and end_date' but how do you create something like that in AS using MDX?

TIA,
Ferdinand

Not sure how well this Adventure Works query corresponds to your scenario, but it computes the number of products that are "active" during the selected Calendar Date member:

>>

With

Member [Measures].[ProductCount] as

Count(Filter(existing [Product].[Product].[Product],

[Product].[Start Date].MemberValue <=

ClosingPeriod([Date].[Calendar].[Date]).MemberValue

AND ([Product].[End Date].CurrentMember is

[Product].[End Date].[Active]

OR [Product].[End Date].MemberValue >=

OpeningPeriod([Date].[Calendar].[Date]).MemberValue)))

select

[Date].[Calendar Year].Members *

{[Measures].[ProductCount]} on 0,

[Product].[Category].Members on 1

from [Adventure Works]

All Periods CY 2001 CY 2002 CY 2003 CY 2004
ProductCount ProductCount ProductCount ProductCount ProductCount
All Products 606 283 411 534 406
Accessories 35 3 13 32 22
Bikes 125 30 65 95 60
Clothing 48 7 28 41 20
Components 189 34 96 157 95

>>

|||

Sorry for my 'not so very fast'-reply, but I had some other bussines to attend to. The above example is not entirly what I need. I need to create three measures with the model I posted:

1. I need to count all activations (e.g. all subscriptions that are activated (=start_date) at an given moment

2. All terminations (=end_date) at an given moment

3. All active subscriptions (= given date between start_date and end_date) at an given moment.

1 and 2 are relativly simple. These are accomplshed by using the generated [Count of Subscription] measure and the also generated [Start DateTime] and [End DateTime] dimensions. But the third measure is not so simple (for me that is). Has anyone got a clue on this?

The pivot I whish to create will look something like this:

Number of subscriptions

Number of activations

Number of terminations

01-jan-2007

100

1

0

02-jan-2007

101

0

5

03-jan-2007

96

0

0

04-jan-2007

96

0

0

|||

Well, in your report there is only one date for number of subscriptions, there isn't start_date and end_date. So I assume it means number of active subscriptions at that date. The standard way of doing it is maintaining in fact table measure "Number of subscriptions", and then you will put 1 for it when there is an activation and -1 when there is a termination. By implementing running sum calculation on the lowest level of Date dimension - you will get what you need. This problem is the same as classic inventory management problem - you can read more about it here: http://www.sqlserveranalysisservices.com/OLAPPapers/InventoryManagement%20in%20AS2005v2.htm

|||

Thanks for your tip Mosha.

I've created a view where there is a 1 for each activation and a -1 for each termination and indeed when you sum them up at a specified day the number of active subscriptions is acurate. I use this MDX script to create the sum at the specified day:

CREATE MEMBER CurrentCube.[Measures].[Number of active subscriptions] as
sum
(
[Date Time].[Day].Level.members(0):ClosingPeriod([Date Time].[Day].Level),
[Measures].[SubSum]
),
visible=true;

However, this script does not seem to work at other levels then day (my time dimension contains two hierarchies: year->quarter->month->day->hour and year->week->day->hour). The results at month level for example is the result of the sum of all records the fall within that month and not the sum of all records up to the first day of the month (which should be the case). Same goes for week, quarter and year level. The lowest level (hour) works fine, that is, it contains the same value as for the whole day.

Can you tell me what I'm doing wrong?

|||

I don't think at the month level you want to sum up to the first day of the month. I think you want to get the sum up to the current day. While it looks like this problem is for LastNonEmptyChild semiadditive calculation, it actually can be solved more efficiently and still give correct results with LastChild. And if you don't want to work with LastChild semiadditive aggregation, you can simulate it with the series of lines like

SCOPE (Measures.[Number of active subscriptions);

[Date Time].[YQMDH].[Month] = [Date Time].[YQMDH].LastChild;

[Date Time].[YQMDH].[Quarter] = [Date Time].[YQMDH].LastChild.LastChild;

[Date Time].[YQMDH].[Year] = [Date Time].[YQMDH].LastChild.LastChild.LastChild;

// etc

END SCOPE;

|||Thanks, that last part did the trick!