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

Sunday, March 25, 2012

Distinct Sum for my column

Hi,

Bonjour,

I want distinct sum for one of my column.But iam not able to do that.

I tried DISTINCTSUM function given inMSDN, but it always return ZERO.

My function call in FOOTER section is called first, before my DETAILS section function call.

please help me for this.

thanks and regards

Hemant

You need to add an expression to your detail cells in the column, that expression should call a custom (code) function that records all unique values into an array:

=Code.AddUniqueNumber(myField)

then in your total just call another function that sums the array you built:

=Code.SumUniqueNumbers()

this is roughly what your code should look like:

Code Snippet

dim myArray() as Integer

public function AddUniqueNumber(Byval newNumber as integer) as integer

AddUniqueNumber = newNumber

dim i as integer

for i = lbound(myArray) to ubound(myArray)

'if this array element equals the number then it isn't unique

if myArray(i) = newNumber then exit sub

next

'increase the size of the array

redim preserve myArray(ubound(myArray) + 1)

'add the new unique number to it

myArray(ubound(myArray)) = newNumber

end function

public function SumUniqueNumbers() as Integer

dim sum as integer

dim i as integer

for i = lbound(myArray) to ubound(myArray)

sum = sum + myArray(i)

next i

SumUniqueNumbers = sum

end function

Note that this code is purely of the top of my head, my VBA is rusty, and it is UNTESTED and will have syntax errors. But it gives you an indication of how to do it. You will also need to initialise your array, probably by passing the rownum in as a parameter as well, and if the rownum = 1 then reinitialise the array.

|||

hi,

thanks for the reply.

but function call in my footer is called first, where i display the sum,so the sum always come zero.

so the code doesnt works

-thanks and rgeards

Hemant

|||

HemantC wrote:

but function call in my footer is called first, where i display the sum,so the sum always come zero.

so the code doesnt works

Then you are doing something wrong.... the code concept does work, i have used it in the past.

The columns in a report are evaluated left to right, top to bottom, so if your array was zero then maybe you have one of these things wrong:

- you have not inserted a call to add a value to the array in the detail rows (or you put the call in the wrong place)

- you are making the call correctly but not adding the new value to the array like you should

- you are reinitialising the array on every call, instead of on just the first row of the table

- you are not looping through the array correctly to sum it

- there is an error in the code and you are showing a zero instead of #ERROR

What i have found helpful in the past is to write the code in the macro editor of Excel, along with a test function that calls it, then once it is performing correctly i insert the code into the report.

|||

Hi,

Thanks again.

I deleted my table and again created new one.But my footer function is called first and then my details section.

For debugging i just put a messagebox, which shows that first footer function is called.

thanks and regards

Hemant.

|||Do you mean the page footer or do you mean the subtotal on a table?

If it is the former, then try referencing the code from a hidden text box in the page body, and then refer to the hidden text box from the footer using the "reportitems" collection|||

Its in footer.

I also tried in hidden textbox.

But we cant access textbox of details section in Footer section.It gives error.

thanks

Hemant

|||

this is my code


Public orderIDs As System.Collections.Hashtable
Public total As Double

Public function CalculateSum(ByVal orderID As Object, ByVal freight As Object) As Double

If (orderIDs Is Nothing) Then
orderIDs = New System.Collections.Hashtable
End If
If (orderID Is Nothing) Then
CalculateSum = total
Else
If (Not orderIDs.Contains(orderID)) Then
total = total + freight
orderIDs.Add(orderID, freight)
End If
CalculateSum = total
End If
End Function

Public function SumUniqueNumbers() as Integer
System.Windows.Forms.MessageBox.Show("toto")
dim sum as integer
Dim myDE As System.Collections. DictionaryEntry

For Each myDE In orderIDs

sum =sum +myDE.Value
Next myDE
SumUniqueNumbers = sum
end function

|||workaround for that error
http://forums.microsoft.com/TechNet/ShowPost.aspx?PostID=1903450&SiteID=17|||

hi,

i have tried this but we cannot acess the textbox present in details section in footer.

this is the error

Report item expressions can only refer to other report items within the same grouping scope or a containing grouping scope.

hemant

Distinct question

Hi
Ive got a problem with the Distinct function
I need to only select one column with the distinct function and leave the
rest so they can have duplicates.
For exampel
Table_Orders
OrderID | Orderdate | User | City
Then I want to select the unique OrderID to list all current orders but at
the same time i want the users and the orderdates no to be unique. so i want
the distinct function to work only on the OrderID column.
Is that possible ?
/GustafThis has been responded to in the .programming newsgroup.
Please do not multi-post.
Thanks!
--
HTH
--
Kalen Delaney
SQL Server MVP
www.SolidQualityLearning.com
"Gurra" <gurgel@.telia.com> wrote in message
news:umLGpRPnDHA.360@.TK2MSFTNGP12.phx.gbl...
> Hi
> Ive got a problem with the Distinct function
> I need to only select one column with the distinct function and leave the
> rest so they can have duplicates.
> For exampel
> Table_Orders
> OrderID | Orderdate | User | City
> Then I want to select the unique OrderID to list all current orders but at
> the same time i want the users and the orderdates no to be unique. so i
want
> the distinct function to work only on the OrderID column.
> Is that possible ?
> /Gustaf
>
>

Thursday, March 22, 2012

DISTINCT function does not remove the duplicate member

Hi All,

The following query returns 4 members ([Gross Sales], [Trade Sales], [Intercompany Sales], [Trade Sales]) while it should return only 3. DISTINCT function does not remove the duplicated member [Trade Sales].

SELECT

{[Measures].[Amount]}

ON AXIS(0),

DISTINCT (DESCENDANTS({ [Account].[Accounts].[Gross Sales],

[Account].[Accounts].[Trade Sales]}, 0, self_and_after))

ON AXIS(1)

FROM [ADVENTURE WORKS]

Any inputs will be appreciated.

David.

DISTINCT must follow SELECT:

Code Snippet

SELECT

DISTINCT (DESCENDANTS({ [Account].[Accounts].[Gross Sales],

[Account].[Accounts].[Trade Sales]}, 0, self_and_after)) ON AXIS(1),

{[Measures].[Amount]}

ON AXIS(0),

FROM [ADVENTURE WORKS]

Adamus

|||

Seems to be a bug, since if you statically list the members, Distinct() seems to work:

Code Snippet

SELECT

{[Measures].[Amount]} ON AXIS(0),

Distinct({[Account].[Accounts].[Gross Sales],

[Account].[Accounts].[Trade Sales],

[Account].[Accounts].[Intercompany Sales],

[Account].[Accounts].[Trade Sales]}) ON AXIS(1)

FROM [ADVENTURE WORKS]

Using Generate() instead of Distinct() also seems to work:

Code Snippet

SELECT

{[Measures].[Amount]} ON AXIS(0),

Generate(DESCENDANTS({[Account].[Accounts].[Gross Sales],

[Account].[Accounts].[Trade Sales]}, 0, self_and_after),

{[Account].[Accounts].CurrentMember}) ON AXIS(1)

FROM [ADVENTURE WORKS]

|||

Thx, Deepak.

The question remains: is it just a bug or there is some hidden meaning to this behavior?

Curiously, if instead of DISTINCT you apply other functions that should remove duplicates,

the duplicates still remain, for example, UNION with an empty set:

UNION(DESCENDANTS({[Account].[Accounts].[Gross Sales], [Account].[Accounts].[Trade Sales]}, 0, self_and_after), {})

Distinct Function

Hi All,

I have used Distinct function in my mdx query to remove duplicate
values.

I want to know what performance effect it will have on execution of
query.

With large volume of data query is taking more time to execute with
Distinct function. If we remove it it is taking less time.

Any inputs is appreciated.

Raghu

Depends on the query, but usually it has no effect on the performance. Distinct function doesn't look at cell values - it dedups tuples from the set. And if it is placed on the axis of SELECT query, then before getting cell values, the AS engine performs Distinct internally anyway. So it would be interesting to see your exact scenario to understand why you see performance difference.

Friday, February 17, 2012

display numbers in a specific format

how can i display numbers in a specific format
like function: format(Num,"#,###") in msaccess?
thanksYour client application controls how numbers are displayed. SQL Server
has no control over that so you need to refer to whatever client /
presentation app you are using (you didn't specify).
The only way to do that from SQL would be to return the number as a
string (not really a good idea). Lookup CONVERT in Books Online if you
want to know how to do that.
David Portas
SQL Server MVP
--|||There is no such format in SQL Server. As David says, this is usually
controlled by the client application and/or the regional settings or
preferences of the end user.
If you really feel strongly enough that SQL Server should do this for you
(you may have compelling reasons), then you can use CONVERT() or STR().
Both are documented in Books Online.
"Sam" <focus10@.zahav.net.il> wrote in message
news:eHv7pvH4FHA.1416@.TK2MSFTNGP09.phx.gbl...
> how can i display numbers in a specific format
> like function: format(Num,"#,###") in msaccess?
> thanks
>
>

display msgbox in browser

hi..

i have the following embedded code in my rdl file..

Function validateDate (ByVal startDt as datetime, ByVal endDt as datetime)

if (startDt > endDt)
System.Windows.Forms.MessageBox.Show("Date Range From must be earlier or the same as Date Range To")
End if

End function

this code works in VS IDE but the msgbox would not appear when view using a browser...

how can i get the msgbox to appear when view using a browser?

thanksss

Code execution will happen at the server, not at the client. Client only receives the output of the renderer. The MessageBox might be showing up at the server, but that doesn't help the client.

There is no good way to cause such a dialog to display at the client.

|||

thanks mike for your reply. is there any workabout to this?

is there any error handling i can do to alert the users?

thanks!

Tuesday, February 14, 2012

Display Data in Particular Format

I want to display in following manner.i tried a lot with Matrix with celing function but i can't achieve it.

My Crieteria:

I Used to Develop An attendence Register for employees.Each Employees have Multiple Login and Logout.

NwMy Issue is I want to Group Each Employees with having his own IN andOut on a particular day he Present.And at the same time since eachemployees having several login and logout for a particular day.I wantto Wrap a matrix after certain columns and display the rest in nextline for that day of that employee

ie,

i Want in this format

Employee Name: E1

Present Day 1

IN1 OUT1 IN2 OUT2 IN3 OUT3........after a particular column it should be wrapped and displayed in next line

IN4 Out4 IN5 OUT5............this process is continued until all the login and Logouts of tht employees is displayed...

Then

Present Day2

INs and Outs

Present Day3

INs and Outs

After The first Employees

Similar Format is For all the other employees

How can i achieve this??


Could you be more specific? Say, where are your data? in SQL Server?

You can use, for example, nested datalist's, with the nested DataList configured with the columns you want to display, is very easy and there are a lot of tutorials, but I don't know exactly where are your data and where do you want to display that information.

Another way is to retrieve it in XML and render with a XSLT stylesheet. Is quite more complex but gives you more flexibility.

Here's a tutorial with nested repeaters, but the philosophy with datalists is the same.
http://www.aspnettutorials.com/tutorials/controls/nested-repeater-vb.aspx