Showing posts with label appears. Show all posts
Showing posts with label appears. Show all posts

Wednesday, March 28, 2012

More grouping questions - sum of maximum

I have a table with two groups, unit and domicile. Structure appears as follows:

GH1 - Domicile Group: Bentwood
GH2- Unit Group: 200
Detail: LegID Unit ID Miles TripMiles
1 200 25 150
2 200 75 150
3 200 50 150

Subtotal for GH2 Count(Fields!legID.value) Sum(Fields!Miles.value) First(Fields!TripMiles.value)
3 150 150

This works, what breaks is the subtotal for GH1. on the TripMiles Field.
It doesnt like Sum(First(Fields!Tripmiles.value,"Unit_Group")), nor can I do Sum(ReportItems!Trip_group.value) I get the error that an aggregate cannot occur on a report item unless it is in the page header or footer.

Of course if I try Sum(Fields!TripMiles.value,"Domicile_Group") I get 450 which is incorrect.

I've tried making First(fields!tripmiles.value) a calculated field which I just then drop into the header of the Unit group<GH2>. This works perfectly until I then try to Sum that field in the Domicile group. I can create the expression but once I click ok, VS2005 reports that it must shut down and crashes to desktop, when I do a recovery all my changes are lost.

<by the way this crash always occurs when I create a calculated field that uses First/Max or Min, then try to use that field in another calculation. Fields based on IIF work without issue>

To clarify, I want to take the TripMiles Column and for The Unit group show the first value that occurs for that unit as the value.

In the Domicile Group, I want to sum all of those Unit Values.
if unit 200 had 3 occurances of 150, my Unit group would have a value of 150 <not 450>
if Unit 300 had 4 occurances of 100, my Unit group would have a value of 100 <not 400>
My total for the Domicile would be 250 <not 850>

Thank you for any assistance.

I can't think of a way of doing this but surely in your case the subtotal at the Domicile level for TripMiles column should equal that of the Miles column.

I would cheat and just put Sum(Fields!Miles.Value) in the subtotal for the TripMiles column in the Domicile group footer.

|||

Bah I jinxed myself using a perfect scenario in that mielage column didnt I <sigh>

Yes I agree with you in this particular instance with the data I displayed. TripMiles never change, it is a base amount for the order. I should have added two more rows to show that the two columns do not always match.

Let's pretend that the TripColumn was labeled something else, Like fuel Consumed.

If I could do an aggregate on the Max/min/first of that field I would be set :(

Edit: OK, I found another thread similar to my situation here :http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=106424&SiteID=1

Where it is stated that at the time that post was made, One year ago last week, you could not do an aggregate of an aggregate and gave a work expression that is one I've tried but errors out.

They then stated to 'modifey' the query to insert nulls at the duplicate. So in my example I'd have
150
null
null
null

How do I do that?

|||

You can use the SQL 2005 ROW_NUMBER() aggreagate function as follows:

SELECT LegID
, UnitID
, Miles
, TripMiles = CASE ROW_NUMBER() OVER (PARTITION BY LegID ORDER BY LegID) WHEN 1 THEN TripMiles ELSE NULL END
FROM YourTable

More grouping questions - sum of maximum

I have a table with two groups, unit and domicile. Structure appears as follows:

GH1 - Domicile Group: Bentwood
GH2- Unit Group: 200
Detail: LegID Unit ID Miles TripMiles
1 200 25 150
2 200 75 150
3 200 50 150

Subtotal for GH2 Count(Fields!legID.value) Sum(Fields!Miles.value) First(Fields!TripMiles.value)
3 150 150

This works, what breaks is the subtotal for GH1. on the TripMiles Field.
It doesnt like Sum(First(Fields!Tripmiles.value,"Unit_Group")), nor can I do Sum(ReportItems!Trip_group.value) I get the error that an aggregate cannot occur on a report item unless it is in the page header or footer.

Of course if I try Sum(Fields!TripMiles.value,"Domicile_Group") I get 450 which is incorrect.

I've tried making First(fields!tripmiles.value) a calculated field which I just then drop into the header of the Unit group<GH2>. This works perfectly until I then try to Sum that field in the Domicile group. I can create the expression but once I click ok, VS2005 reports that it must shut down and crashes to desktop, when I do a recovery all my changes are lost.

<by the way this crash always occurs when I create a calculated field that uses First/Max or Min, then try to use that field in another calculation. Fields based on IIF work without issue>

To clarify, I want to take the TripMiles Column and for The Unit group show the first value that occurs for that unit as the value.

In the Domicile Group, I want to sum all of those Unit Values.
if unit 200 had 3 occurances of 150, my Unit group would have a value of 150 <not 450>
if Unit 300 had 4 occurances of 100, my Unit group would have a value of 100 <not 400>
My total for the Domicile would be 250 <not 850>

Thank you for any assistance.

I can't think of a way of doing this but surely in your case the subtotal at the Domicile level for TripMiles column should equal that of the Miles column.

I would cheat and just put Sum(Fields!Miles.Value) in the subtotal for the TripMiles column in the Domicile group footer.

|||

Bah I jinxed myself using a perfect scenario in that mielage column didnt I <sigh>

Yes I agree with you in this particular instance with the data I displayed. TripMiles never change, it is a base amount for the order. I should have added two more rows to show that the two columns do not always match.

Let's pretend that the TripColumn was labeled something else, Like fuel Consumed.

If I could do an aggregate on the Max/min/first of that field I would be set :(

Edit: OK, I found another thread similar to my situation here :http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=106424&SiteID=1

Where it is stated that at the time that post was made, One year ago last week, you could not do an aggregate of an aggregate and gave a work expression that is one I've tried but errors out.

They then stated to 'modifey' the query to insert nulls at the duplicate. So in my example I'd have
150
null
null
null

How do I do that?

|||

You can use the SQL 2005 ROW_NUMBER() aggreagate function as follows:

SELECT LegID
, UnitID
, Miles
, TripMiles = CASE ROW_NUMBER() OVER (PARTITION BY LegID ORDER BY LegID) WHEN 1 THEN TripMiles ELSE NULL END
FROM YourTable

|||what other solution, if i'm using SQL 2000? what other method to achived the above reasult?|||

Code Block

SELECT LegID
, UnitID
, Miles
, TripMiles = CASE LegID

WHEN (SELECT MIN(LegID) FROM YourTable)

THEN TripMiles

ELSE NULL

END
FROM YourTable

Monday, February 20, 2012

Money Data Type

Recently any query run that calls for a filed of the money data type has started displaying all four decimal places. The only thing that appears to have changed is the hotfix referenced in MS04-16 (Deals with Direct X) was applied to the server. As far
as I can tell the money data type has always had a scale of 4. Does anyone know why our queries are showing all four decimal places now? I guess more importantly does anyone know how to make it return to only displaying 2 decimal places? I already sugg
ested casting the values to decmial data type with a scale of 2, but our developers rejected that idea as there are thousands of stored procedures they would have to go through to do that.
Thanks in advance,
Jason
Jason,
Please differentiate between a value and the presentation of a value. SQL Server returns values (in
binary format) and the client application present those values. Obviously, the programmer of your
client applications didn't bother to define the presentation format and because of that has some
default setting for that dev environment, something which changed with the security fix. But SQL
Server does not and cannot present any data. The client application does. This is where you have to
go hunting.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jason Szotak" <Jason Szotak@.discussions.microsoft.com> wrote in message
news:E5816A4C-F985-486B-AF01-0718C80D095C@.microsoft.com...
> Recently any query run that calls for a filed of the money data type has started displaying all
four decimal places. The only thing that appears to have changed is the hotfix referenced in
MS04-16 (Deals with Direct X) was applied to the server. As far as I can tell the money data type
has always had a scale of 4. Does anyone know why our queries are showing all four decimal places
now? I guess more importantly does anyone know how to make it return to only displaying 2 decimal
places? I already suggested casting the values to decmial data type with a scale of 2, but our
developers rejected that idea as there are thousands of stored procedures they would have to go
through to do that.
> Thanks in advance,
> Jason
|||Tibor,
Thanks for the response. I could use a little more clarification though. When running a stored procedure through query analyer or in the display of a view through enterprise manager the output now shows the four decimal places. It used to show only tw
o. Is it possible that a patch somehow changed this? Like I said before, it started about a week ago and the only thing that has changed in that week is the one hotfix for Direct X was applied. One of our developers thinks that if the display of the va
lue returns to 2 decimal places when running a stored procedure through query analyzer that our problem would be solved. Would you consider this to be an accurate statement?
"Tibor Karaszi" wrote:

> Jason,
> Please differentiate between a value and the presentation of a value. SQL Server returns values (in
> binary format) and the client application present those values. Obviously, the programmer of your
> client applications didn't bother to define the presentation format and because of that has some
> default setting for that dev environment, something which changed with the security fix. But SQL
> Server does not and cannot present any data. The client application does. This is where you have to
> go hunting.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
|||After invetigating this issue further I think the change to the system was that we upgraded the .Net Framework on our webserver to v1.1. The developer that is having the problem is using ASP.Net. We also have applications written in asp. When a stored
procedure is run in query analyzer on a money field the output has the four decimal places, but our asp web page only show the decimals places that are non-zero (the value 100.4000 would display as 100.4 and the value 100.0400 displays as 100.04), so like
you said our development environment has changed and probably has nothing to do with the hotfix. That said, Is there a way to configure the .Net Framework v1.1 to only show two decimal places for money data types?
Jason
"Tibor Karaszi" wrote:

> Jason,
> Please differentiate between a value and the presentation of a value. SQL Server returns values (in
> binary format) and the client application present those values. Obviously, the programmer of your
> client applications didn't bother to define the presentation format and because of that has some
> default setting for that dev environment, something which changed with the security fix. But SQL
> Server does not and cannot present any data. The client application does. This is where you have to
> go hunting.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
|||I don't know the framework enough to guess if or where you do that. I suggest you post that to a
..NET fx group.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jason Szotak" <JasonSzotak@.discussions.microsoft.com> wrote in message
news:0F1A91C6-AA58-4DBB-AED9-8A2A463B774A@.microsoft.com...
> After invetigating this issue further I think the change to the system was that we upgraded the
..Net Framework on our webserver to v1.1. The developer that is having the problem is using ASP.Net.
We also have applications written in asp. When a stored procedure is run in query analyzer on a
money field the output has the four decimal places, but our asp web page only show the decimals
places that are non-zero (the value 100.4000 would display as 100.4 and the value 100.0400 displays
as 100.04), so like you said our development environment has changed and probably has nothing to do
with the hotfix. That said, Is there a way to configure the .Net Framework v1.1 to only show two
decimal places for money data types?[vbcol=seagreen]
> Jason
> "Tibor Karaszi" wrote:
(in[vbcol=seagreen]
your[vbcol=seagreen]
to[vbcol=seagreen]

Money Data Type

Recently any query run that calls for a filed of the money data type has sta
rted displaying all four decimal places. The only thing that appears to hav
e changed is the hotfix referenced in MS04-16 (Deals with Direct X) was appl
ied to the server. As far
as I can tell the money data type has always had a scale of 4. Does anyone
know why our queries are showing all four decimal places now? I guess more
importantly does anyone know how to make it return to only displaying 2 deci
mal places? I already sugg
ested casting the values to decmial data type with a scale of 2, but our dev
elopers rejected that idea as there are thousands of stored procedures they
would have to go through to do that.
Thanks in advance,
JasonJason,
Please differentiate between a value and the presentation of a value. SQL Se
rver returns values (in
binary format) and the client application present those values. Obviously, t
he programmer of your
client applications didn't bother to define the presentation format and beca
use of that has some
default setting for that dev environment, something which changed with the s
ecurity fix. But SQL
Server does not and cannot present any data. The client application does. Th
is is where you have to
go hunting.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jason Szotak" <Jason Szotak@.discussions.microsoft.com> wrote in message
news:E5816A4C-F985-486B-AF01-0718C80D095C@.microsoft.com...
> Recently any query run that calls for a filed of the money data type has started d
isplaying all
four decimal places. The only thing that appears to have changed is the hot
fix referenced in
MS04-16 (Deals with Direct X) was applied to the server. As far as I can te
ll the money data type
has always had a scale of 4. Does anyone know why our queries are showing a
ll four decimal places
now? I guess more importantly does anyone know how to make it return to onl
y displaying 2 decimal
places? I already suggested casting the values to decmial data type with a
scale of 2, but our
developers rejected that idea as there are thousands of stored procedures th
ey would have to go
through to do that.
> Thanks in advance,
> Jason|||Tibor,
Thanks for the response. I could use a little more clarification though. Wh
en running a stored procedure through query analyer or in the display of a v
iew through enterprise manager the output now shows the four decimal places.
It used to show only tw
o. Is it possible that a patch somehow changed this? Like I said before, i
t started about a week ago and the only thing that has changed in that week
is the one hotfix for Direct X was applied. One of our developers thinks th
at if the display of the va
lue returns to 2 decimal places when running a stored procedure through quer
y analyzer that our problem would be solved. Would you consider this to be
an accurate statement?
"Tibor Karaszi" wrote:

> Jason,
> Please differentiate between a value and the presentation of a value. SQL
Server returns values (in
> binary format) and the client application present those values. Obviously,
the programmer of your
> client applications didn't bother to define the presentation format and be
cause of that has some
> default setting for that dev environment, something which changed with the
security fix. But SQL
> Server does not and cannot present any data. The client application does.
This is where you have to
> go hunting.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/|||After invetigating this issue further I think the change to the system was t
hat we upgraded the .Net Framework on our webserver to v1.1. The developer
that is having the problem is using ASP.Net. We also have applications writ
ten in asp. When a stored
procedure is run in query analyzer on a money field the output has the four
decimal places, but our asp web page only show the decimals places that are
non-zero (the value 100.4000 would display as 100.4 and the value 100.0400 d
isplays as 100.04), so like
you said our development environment has changed and probably has nothing to
do with the hotfix. That said, Is there a way to configure the .Net Framew
ork v1.1 to only show two decimal places for money data types?
Jason
"Tibor Karaszi" wrote:

> Jason,
> Please differentiate between a value and the presentation of a value. SQL
Server returns values (in
> binary format) and the client application present those values. Obviously,
the programmer of your
> client applications didn't bother to define the presentation format and be
cause of that has some
> default setting for that dev environment, something which changed with the
security fix. But SQL
> Server does not and cannot present any data. The client application does.
This is where you have to
> go hunting.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/|||I don't know the framework enough to guess if or where you do that. I sugges
t you post that to a
.NET fx group.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Jason Szotak" <JasonSzotak@.discussions.microsoft.com> wrote in message
news:0F1A91C6-AA58-4DBB-AED9-8A2A463B774A@.microsoft.com...
> After invetigating this issue further I think the change to the system was that we
upgraded the
.Net Framework on our webserver to v1.1. The developer that is having the
problem is using ASP.Net.
We also have applications written in asp. When a stored procedure is run in
query analyzer on a
money field the output has the four decimal places, but our asp web page onl
y show the decimals
places that are non-zero (the value 100.4000 would display as 100.4 and the
value 100.0400 displays
as 100.04), so like you said our development environment has changed and pro
bably has nothing to do
with the hotfix. That said, Is there a way to configure the .Net Framework
v1.1 to only show two
decimal places for money data types?[vbcol=seagreen]
> Jason
> "Tibor Karaszi" wrote:
>
(in[vbcol=seagreen]
your[vbcol=seagreen]
to[vbcol=seagreen]