Showing posts with label sum. Show all posts
Showing posts with label sum. Show all posts

Monday, March 19, 2012

Aggregations in Analysis Services STANDARD edition

I have fields in my cube where SUM is not a good aggregation (rate fields for example). I thought I should just set them to No aggregation, but it tells me that semi-additive aggregations are not available in Standard Edition (?). So, two questions...

1) How do I avoid representing these fields incorrectly in Standard Edition? What aggregation should they be set to? I don't want users to point Excel to them and get this big meaningless number!

2) How/Where do I produce Averages, etc? (I'm thinking that I can only do this in the Report or Excel - that it can't be done in the cube, is that right?)

Thanks for you help...

- Jim

You're correct that Standard Edition does not support semi-additive aggregations. These functions include ByAccount, AverageOfChildren, LastChild, FirstChild, LastNonEmpty, and FirstNonEmpty. If these are the right aggregations to use, you might be able to set up a calculated member that gives you something similar by going to the leaf level and then controlling the aggregation to the level your interested in. If you do this, make the measure you are "replacing" not visible to your users.

Regarding averages, store a SUM and a COUNT measure and do a ratio in a calculated member.

Good luck,
Bryan

|||

If you don't have enterprise edition, then this workaround might help:

http://solidqualitylearning.com/Blogs/dejan/archive/2006/11/08/3439.aspx

or

http://lorentsnv.spaces.live.com/blog/cns!235C78448ABCFC07!134.entry

Aggregation within Dimension Attribute

I am trying to create kind of a running sum in a calculated member in a cube down a dimension attribute. In other words, if I had values 1-5, I would want to sum the corresponding financial values as I go down the list. See example below

Dimension Value Financial Value New Calc

1 100 525

2 50 425

3 75 375

4 100 300

5 200 200

Has anyone done anything like this?

Some more business details might help us understand your question. How does this strike you?

with
member [Measures].[New Calc] as Sum({null:[YourDimension].[Dimension Value].CurrentMember},[Measures].[Financial Value])
select {[Measures].[Financial Value], [Measures].[New Calc]} on columns,
[YourDimension].[Dimension Value].[Dimension Value].Members on rows
from YourCube

aggregation question

Aggregation SUM adds the numbers together. Is there a similar aggregation function that returns the product of all the numbers?

Thanks

No, there is no built-in PRODUCT aggregate. One thing you could try is taking the exponent of the sum of the logs of the individual rows; however, this will tend to cause arithmetic execution errors if any of the individual rows have zero for a value.

Here is a similar discussion:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=355075&SiteID=1

|||

Do you mean 'product' as in factorial?

|||

Check out the link below for a solution that uses the built-in aggregates to compute product:

http://www.umachandar.com/technical/SQL6x70Scripts/Main21.htm

You can also write a SQLCLR aggregate in SQL Server 2005 to do the same.

Sunday, March 11, 2012

aggregation functions

hi all,
it could be possible to do something like sum(other aggregation functions)?
i mean to have nested aggregation functions.
thank youAggregates of aggregates is currently not supported. This feature is on the
wish list for inclusion in a future release.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Mirela" <Mirela@.discussions.microsoft.com> wrote in message
news:39108DA6-4E27-4B6A-823D-563D354FD4AE@.microsoft.com...
> hi all,
> it could be possible to do something like sum(other aggregation
functions)?
> i mean to have nested aggregation functions.
> thank you

aggregates within aggregates

Here is the code I have:

=Sum(IIF(sum( Fields!Total_Amount.Value, "Collat_Acct_Group2") < 0, 1,0))

I am trying to do a count but only if the sum of a value is greater/less than 0.

The error I get is:

The value expression for the textbox 'textbox146' contains an aggregate function (or RunningValue or RowNumber functions) in the argument to another aggregate function (or RunningValue). Aggregate functions cannot be nested inside other aggregate functions.

Please help .

Thanks

Elias

I guess you want to count the number of groups which have sum of total amount > 0.

In the code (Report -> Report Properties -> Code tab), have a shared variable called count and initialize it to 0. Then have a function called IncrementCount which when called will increment count by 1.

In your Collat_Acct_Group2 header or footer, append the following expression to the any of the textboxes:

[old expression] & IIf(Sum(Fields!Total_Amount.Value, "Collat_Acct_Group2") > 0, Code.IncrementCount(), "")

and use "Code.count" in any field expression to get the count of all groups with sum of total amount > 0

Shyam

|||

Shyam,

Thanks, for the answer, but I have never written any code in RS if it would not be too much trouble could you please show me an example.

Thanks again,

Elias

|||

Sorry for the delay in my response

Your code will look something like this in VB.Net:

Private Shared count as Integer

count=0

Public Function IncrementCount() As String

count = count +1

IncrementCount = CStr(count)

End Function

Say one of the textboxes (or a column) in your table header has the following expression:

Fields!SomeField.Value

Now change the expression to:

Fields!SomeField.Value & IIf(Sum(Fields!Total_Amount.Value, "Collat_Acct_Group2") > 0, Code.IncrementCount(), "")

Then use Code.count in any of your field expressions to get the number of groups with total amount > 0.

Shyam

Aggregates question

Let's say I have this data set

Country Quant
USA 100
UK 50
USA 25

How would I get a sum of just the USA quantites while keeping all entries in the details section?

Assuming this is a table, you would put the following conditional aggregation expression into the table footer:
=Sum( iif(Fields!Country.Value = "USA", Fields!Quant.Value, 0))

Only if country = USA, the quantity values will be aggregated. Otherwise 0 will be added.

-- Robert

|||You sir are a gentleman and a scholar.

That worked thanks!

aggregates and type conversion

Hello,
I have a field of type string which I know will either hold a double value or null. I wish to sum the value of this field and mulitply the result by -1 to give a negative number, but it's proving really difficult.
My first problem is that if I modify my field to use CDbl I get an error when it runs, along the lines of "Field contains a direct or indirect reference to itself..."
So I have to get around it by creating a separate field with a different name, like
myFieldDbl and setting it's formula to = Cdbl(Fields!MyField.Value).
Why is that then ?
My next problem is that when I multiply by -1
e.g. =Sum(Fields!MyFieldDbl.Value * -1),
it gives me this error :
"The value expression for the textbox â'textbox19â' uses an aggregate function on data of varying data types".
To get around this I have to multiply by -1.0 instead ! As if there's a difference !!!
Type conversion in Reporting services is diabolical. I can't even use the format property unless I explicitly declare my field to be a particluar type first, even though I could happily use format$(myField,"C") in the Value property with any data type.
Can I expect MS might have a look at thses issues in time for the next service pack?Note: It is up to the data provider (e.g. managed SQL provider) how it
translates database types into .NET datatypes. Generally, you can determine
the runtime datatype of fields by temporarily adding a textbox to the report
which shows the runtime datatype:
=First(Fields!SomeFieldName.Value, "DataSetName").GetType.ToString
I assume that your field is actually a System.Decimal rather than a
System.Double as you indicated. An expression like this should work for you:
= -1 * Sum( iif(IsNothing(Fields!MyFieldDbl.Value), 0.0,
CDbl(Fields!MyFieldDbl.Value)))
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Mike Salway" <MikeSalway@.discussions.microsoft.com> wrote in message
news:B90D6631-A0D2-4F53-9C69-A06366ADA6C7@.microsoft.com...
> Hello,
> I have a field of type string which I know will either hold a double value
or null. I wish to sum the value of this field and mulitply the result
by -1 to give a negative number, but it's proving really difficult.
> My first problem is that if I modify my field to use CDbl I get an error
when it runs, along the lines of "Field contains a direct or indirect
reference to itself..."
> So I have to get around it by creating a separate field with a different
name, like
> myFieldDbl and setting it's formula to = Cdbl(Fields!MyField.Value).
> Why is that then ?
> My next problem is that when I multiply by -1
> e.g. =Sum(Fields!MyFieldDbl.Value * -1),
> it gives me this error :
> "The value expression for the textbox 'textbox19' uses an aggregate
function on data of varying data types".
> To get around this I have to multiply by -1.0 instead ! As if there's a
difference !!!
> Type conversion in Reporting services is diabolical. I can't even use the
format property unless I explicitly declare my field to be a particluar type
first, even though I could happily use format$(myField,"C") in the Value
property with any data type.
> Can I expect MS might have a look at thses issues in time for the next
service pack?

AggregateOfMixedDataTypes error

I have an expression that returns Invoice amounts for the current
month. I try to Sum this though and get the aggregate of mixed data
types error. I've tried to convert the dates, but then get the 'trying
to Sum on data types other than numeric'. Can anyone help me figure
out how I can sum this column? Thank you. This is in Visual Studio on
SQL Server.
= iif(month(InvoiceDate)= month(getdate()) AND year(InvoiceDate)=
year(getdate()), InvoiceAmount, 0)
On Thu, 28 Feb 2008 09:44:32 -0800 (PST), p-rat <osupratt@.yahoo.com>
wrote:

>I have an expression that returns Invoice amounts for the current
>month. I try to Sum this though and get the aggregate of mixed data
>types error. I've tried to convert the dates, but then get the 'trying
>to Sum on data types other than numeric'. Can anyone help me figure
>out how I can sum this column? Thank you. This is in Visual Studio on
>SQL Server.
>= iif(month(InvoiceDate)= month(getdate()) AND year(InvoiceDate)=
>year(getdate()), InvoiceAmount, 0)
This looks like a mix of Access (iif) and SQL Server (getdate()). You
might want to explain that, and show the entire SELECT with the SUM.
In SQL Server I would write this:
CASE WHEN month(invoice_date) = month(getdate())
AND year(invoice_date) = year(getdate())
THEN InvoiceAmount
ELSE 0
END
Note that they type of the constant 0 has to be compatible with the
type of the column InvoiceAmount, and InvoiceAmount has to be a number
that can be SUMmed.
Roy Harvey
Beacon Falls, CT

AggregateOfMixedDataTypes error

I have an expression that returns Invoice amounts for the current
month. I try to Sum this though and get the aggregate of mixed data
types error. I've tried to convert the dates, but then get the 'trying
to Sum on data types other than numeric'. Can anyone help me figure
out how I can sum this column? Thank you. This is in Visual Studio on
SQL Server.
= iif(month(InvoiceDate)= month(getdate()) AND year(InvoiceDate)= year(getdate()), InvoiceAmount, 0)On Thu, 28 Feb 2008 09:44:32 -0800 (PST), p-rat <osupratt@.yahoo.com>
wrote:
>I have an expression that returns Invoice amounts for the current
>month. I try to Sum this though and get the aggregate of mixed data
>types error. I've tried to convert the dates, but then get the 'trying
>to Sum on data types other than numeric'. Can anyone help me figure
>out how I can sum this column? Thank you. This is in Visual Studio on
>SQL Server.
>= iif(month(InvoiceDate)= month(getdate()) AND year(InvoiceDate)=>year(getdate()), InvoiceAmount, 0)
This looks like a mix of Access (iif) and SQL Server (getdate()). You
might want to explain that, and show the entire SELECT with the SUM.
In SQL Server I would write this:
CASE WHEN month(invoice_date) = month(getdate())
AND year(invoice_date) = year(getdate())
THEN InvoiceAmount
ELSE 0
END
Note that they type of the constant 0 has to be compatible with the
type of the column InvoiceAmount, and InvoiceAmount has to be a number
that can be SUMmed.
Roy Harvey
Beacon Falls, CT

Thursday, March 8, 2012

Aggregate where claus problem =/

Hi Guys,

Is it possible to have a where clause (or other method) where you only select the max value from this: SUM(ORDER_ITEM.ItemQuantity) and only output that 1 row... or even perhaps a range of rows... in others words... find the ItemID with the greatest combined Quantity

Heres the query so far:

SELECT ORDER_ITEM.ItemID, SUM(ORDER_ITEM.ItemQuantity) AS 'Max'
FROM ORDER_ITEM
GROUP BY ORDER_ITEM.ItemID

Thx for reading :-)

--PhilkillsTry this:
SELECT itemid, max(qsum)
FROM
(
SELECT ORDER_ITEM.ItemID, SUM(ORDER_ITEM.ItemQuantity) AS qsum
FROM ORDER_ITEM
GROUP BY ORDER_ITEM.ItemID
)
group by itemid|||Shammat, I don't think that will work. The outer query will return the same multi-record dataset as the inner query.

Try this:
SELECT TOP 1
ORDER_ITEM.ItemID,
SUM(ORDER_ITEM.ItemQuantity) AS 'Max'
FROM ORDER_ITEM
GROUP BY ORDER_ITEM.ItemID
ORDER BY SUM(ORDER_ITEM.ItemQuantity) DESC|||Shammat, I don't think that will work. The outer query will return the same multi-record dataset as the inner query.

Try this:
SELECT TOP 1
ORDER_ITEM.ItemID,
SUM(ORDER_ITEM.ItemQuantity) AS 'Max'
FROM ORDER_ITEM
GROUP BY ORDER_ITEM.ItemID
ORDER BY SUM(ORDER_ITEM.ItemQuantity) DESC

ah thx man perfect ^^

Although, is it not inefficient to have 2 sums for the same thing? or will SQL realise that their the same thing and count them as such..

Also would be it possible to change the query to select all Total's that are greater than say... 10..

like:

SELECT
ORDER_ITEM.ItemID,
SUM(ORDER_ITEM.ItemQuantity) AS 'Max'
FROM ORDER_ITEM
WHERE SUM(ORDER_ITEM.ItemQuantity) > 10
GROUP BY ORDER_ITEM.ItemID
ORDER BY SUM(ORDER_ITEM.ItemQuantity) DESC

although this doesn't work as it doesn't like that in the where clause =/|||Doing multiple aggregate is not expensive. Doing multiple data-scans is what costs query time.

To filter on an aggregate value you must use the HAVING clause:
SELECT
ORDER_ITEM.ItemID,
SUM(ORDER_ITEM.ItemQuantity) AS 'Max'
FROM ORDER_ITEM
GROUP BY ORDER_ITEM.ItemID
HAVING SUM(ORDER_ITEM.ItemQuantity) > 10
ORDER BY SUM(ORDER_ITEM.ItemQuantity) DESC|||Doing multiple aggregate is not expensive. Doing multiple data-scans is what costs query time.

To filter on an aggregate value you must use the HAVING clause:
SELECT
ORDER_ITEM.ItemID,
SUM(ORDER_ITEM.ItemQuantity) AS 'Max'
FROM ORDER_ITEM
GROUP BY ORDER_ITEM.ItemID
HAVING SUM(ORDER_ITEM.ItemQuantity) > 10
ORDER BY SUM(ORDER_ITEM.ItemQuantity) DESC

thx mate, lol seems kinda pointless "having" an extra keyword just to perform those extra operations ;p (that is instead of where)

Aggregate sum query, need help

I have two tables tb1 with item and qtyOnHand and a second table tb2 with item and qtyOrdered I am trying without success to make this happen;
select sum (onHand-Ordered) from (select sum (qtyOnHand) from tb1 where item = RD35 group by item) as onHand, (select sum (qtyOrdered) from tb2 where item = RD35 group by item) as Ordered
I kind of gathered it would work based on thishttp://weblogs.asp.net/jgalloway/archive/2004/05/19/135358.aspx
I have also tried this;
select tb1.item from (select sum (qtyOnHand) from tb1 where item = RD35 group by item) as onHand, (select sum (qtyOrdered) from tb2 where item = RD35 group by item) as Ordered, sum (onHand-Ordered) as available from tb1 where tb1.item = RD35
Any ides, there are multiple rows of each item in each table tb1 is inventory with several different locations and tb2 is an orders table.

I'm not sure exactly what values you are looking for but it looks like you want the new quantity on hand after the order for a particular item. Try this:
SELECT
tb1.Item,
IsNull(tb1.qtyOnHand - SUM(qtyOrdered), 0) AS qtyNewOnHand
FROM tb1
LEFT JOIN tb2 ON tb1.item = tb2.item
WHERE tb1.item = RD35
GROUP BY tb1.item, tb1.qtyOnHand
I assumed that tb1 has 1 record per item but that tb2 may have multiple records (orders).
HTH.

|||

Thanks for the reply. There a multiple records in table 1 for a single item, it is an inventory by location table where bin numbers are the primary key. There are multiple records in table 2 as well. Which is why unions and joins are biting me with some multplication. In this particular case RD35 has 3 records in table 1 with a sum of 237 and 6 records in table 2 with a sum of 97.
select table1.item from (select sum (qtyOnHand) from table1 where item = RD35 group by item) as onHand, (select sum (qtyOrdered) from table2 where item = RD35 group by item) as Ordered, sum (onHand-Ordered) as available from table1 where table1.item = RD35
This ends up giving me; onHand 237, Ordered 97 and vailable 402. available should be 140.

|||

Try this:

SELECT
tbOnHand.item,
OnHand,
IsNull(Ordered, 0),
OnHand - IsNull(Ordered, 0) AS Available
FROM
(
SELECT
item,
SUM(qtyOnHand) as OnHand
FROM tb1
WHERE item = RD35
GROUP BY item
) AS tbOnHand
LEFT JOIN
(
SELECT
item,
SUM(qtyOrdered) as Ordered
FROM tb2
WHERE item = RD35
GROUP BY item
) AS tbOrdered ON tbOnHand.item = tbOrdered.item

Aggregate String Concatenation function?

Hi,

I'm trying to do the following, but am getting errors because (obviously) SUM doesn't work with data types of nvarchar.

SELECT
SUM(CASE WHEN FieldName = 'SPECIFIC' THEN Tolerance ELSE '' END) AS 'Specific Tolerance'
FROM FIELD_TOLERANCE
GROUP BY Area

Tolerance holds values such as '100 +/- 25'. Obviously the first thought would be to seperate the two parts '100' and '25' into seperate fields and then have the program reconstruct it. Unfortunatly sometimes the value is odd, such as '100 +10 -25' (meaning a range of 75 - 110 with a target of 100).

Is there any way to put effectively sum up the Tolerance. Also, I know for a fact the FieldName 'SPECIFIC' will only be in the database once for each area.

Thanks,
RyanI searched Books Online for aggregate functions as well as just functions. I found nothing listed under either which would help.

Should I create a function for this task? How would I create a function to do this?

Thanks,
Ryan|||We no longer need to do what I was asking about. But is there a way? I'm curious.|||If there is only one 'SPECIFIC' value per area, are you really adding anything together? Not being in your field, I am having a problem getting my mind around adding tolerances together. It may be that sum is not the right function for this application. What is the result set you want in the end?|||Originally posted by MCrowley
If there is only one 'SPECIFIC' value per area, are you really adding anything together? Not being in your field, I am having a problem getting my mind around adding tolerances together. It may be that sum is not the right function for this application. What is the result set you want in the end?

well I have a table like so (dashes inserted for web formating purposes):

Area--Field--Tolerance
1----FieldA--100 +/- 12
1----FieldB--100 +/- 13
2----FieldA--97 +3 -7
2----FieldC--95 +/- 5

Area type = int
Field type = varchar
Tolerance type = varchar

I want the results I want are as follows (dashes inserted for web formating purposes):

Area--FieldATolerance--FieldBTolerance--FieldCTolerance
1----100 +/- 12---100 +/-13
2----97 +3 -7----------95 +/- 5

This way I can pass the values for field I want the tolerances for and get them all back in one record. This allows the Tolerance table to hold tolerances for different fields, yet make retrieving the tolerances easy.

-Ryan

ps. Thanks for the reply

Aggregate on an aggregate

I need to get the sum of a field that already has an aggregate function (MAX) performed on it. I am using the following query

Code Snippet

SELECT "tI"."ItemID", MAX("vSS"."ShortDesc") "Short Description",

MAX("tPCT"."FreezeQty") "Freeze Qty", SUM("vSS"."QtyOnHand") "Current Qty",

"tPCT"."BatchKey"

FROM ("vSS" "vSS"

INNER JOIN "tI" "tI"

ON "vSS"."ItemKey"="tI"."ItemKey")

LEFT OUTER JOIN "tPCT" "tPCT"

ON "vSS"."ItemKey"="tPCT"."ItemKey"

WHERE "vSS"."ItemID" = '3002954'

GROUP BY "tI"."ItemID", "tPCT"."BatchKey"

It yields the following results

ItemID Short Description Freeze Qty Current Qty BatchKey 3002954 SET, WRENCH HEX METRIC -33 129 42221 3002954 SET, WRENCH HEX METRIC 51 129 42244 3002954 SET, WRENCH HEX METRIC -31 129 42250

I need to SUM the maximum freeze quantity values per item ID. Therefore for this record, I need the following results:

3002954 SET, WRENCH HEX METRIC -13 129

Can this be done via a subquery? Any assistnance would be greatly appreciated?

Thanks,

DLee

I would think you could create a subquery using the following;

SELECT "ItemID","Short Description","Current Qty", SUM("Freeze Qty")

FROM ("vSS");

|||

Donna, try this query

SELECT ItemID

, MAX(SQ.ShortDesc) as [Short Description]

, SUM(SQ.FreezeQty) as [Freeze Qty]

, MAX(SQ.[Current Qty]) as [Current Qty]

FROM (

SELECT tI.ItemID

, MAX(vSS.ShortDesc)

, MAX(tPCT.FreezeQty)

, SUM(vSS.QtyOnHand)

, tPCT.BatchKey

FROM vSS INNER JOIN tI

ON vSS.ItemKey=tI.ItemKey

LEFT OUTER JOIN tPCT

ON vSS.ItemKey=tPCT.ItemKey

WHERE vSS.ItemID = '3002954'

GROUP BY tI.ItemID, tPCT.BatchKey

) SQ

GROUP BY SQ.ItemID

|||

Hello Donna,

Tweak your query a little and try this...

SELECT "tI"."ItemID", "vSS"."ShortDesc" "Short Description",

SUM("tPCT"."FreezeQty") "Freeze Qty", "vSS"."QtyOnHand" "Current Qty"

FROM ("vSS" "vSS"

INNER JOIN "tI" "tI"

ON "vSS"."ItemKey"="tI"."ItemKey")

LEFT OUTER JOIN "tPCT" "tPCT"

ON "vSS"."ItemKey"="tPCT"."ItemKey"

WHERE "vSS"."ItemID" = '3002954'

GROUP BY "tI"."ItemID", "vSS"."ShortDesc", "vSS"."QtyOnHand"

Hope this helps.

Regards.....

|||

If you do not need BatchKey, you can just remove it from the query and you should get the desired answer.

e.g.

Code Snippet

SELECT "tI"."ItemID", MAX("vSS"."ShortDesc") "Short Description",

MAX("tPCT"."FreezeQty") "Freeze Qty", SUM("vSS"."QtyOnHand") "Current Qty"

FROM ("vSS" "vSS"

INNER JOIN "tI" "tI"

ON "vSS"."ItemKey"="tI"."ItemKey")

LEFT OUTER JOIN "tPCT" "tPCT"

ON "vSS"."ItemKey"="tPCT"."ItemKey"

WHERE "vSS"."ItemID" = '3002954'

GROUP BY "tI"."ItemID"

|||

This did the trick!

Thanks Gopi!

Aggregate on an aggregate

I need to get the sum of a field that already has an aggregate function (MAX) performed on it. I am using the following query

Code Snippet

SELECT "tI"."ItemID", MAX("vSS"."ShortDesc") "Short Description",

MAX("tPCT"."FreezeQty") "Freeze Qty", SUM("vSS"."QtyOnHand") "Current Qty",

"tPCT"."BatchKey"

FROM ("vSS" "vSS"

INNER JOIN "tI" "tI"

ON "vSS"."ItemKey"="tI"."ItemKey")

LEFT OUTER JOIN "tPCT" "tPCT"

ON "vSS"."ItemKey"="tPCT"."ItemKey"

WHERE "vSS"."ItemID" = '3002954'

GROUP BY "tI"."ItemID", "tPCT"."BatchKey"

It yields the following results

ItemID Short Description Freeze Qty Current Qty BatchKey 3002954 SET, WRENCH HEX METRIC -33 129 42221 3002954 SET, WRENCH HEX METRIC 51 129 42244 3002954 SET, WRENCH HEX METRIC -31 129 42250

I need to SUM the maximum freeze quantity values per item ID. Therefore for this record, I need the following results:

3002954 SET, WRENCH HEX METRIC -13 129

Can this be done via a subquery? Any assistnance would be greatly appreciated?

Thanks,

DLee

I would think you could create a subquery using the following;

SELECT "ItemID","Short Description","Current Qty", SUM("Freeze Qty")

FROM ("vSS");

|||

Donna, try this query

SELECT ItemID

, MAX(SQ.ShortDesc) as [Short Description]

, SUM(SQ.FreezeQty) as [Freeze Qty]

, MAX(SQ.[Current Qty]) as [Current Qty]

FROM (

SELECT tI.ItemID

, MAX(vSS.ShortDesc)

, MAX(tPCT.FreezeQty)

, SUM(vSS.QtyOnHand)

, tPCT.BatchKey

FROM vSS INNER JOIN tI

ON vSS.ItemKey=tI.ItemKey

LEFT OUTER JOIN tPCT

ON vSS.ItemKey=tPCT.ItemKey

WHERE vSS.ItemID = '3002954'

GROUP BY tI.ItemID, tPCT.BatchKey

) SQ

GROUP BY SQ.ItemID

|||

Hello Donna,

Tweak your query a little and try this...

SELECT "tI"."ItemID", "vSS"."ShortDesc" "Short Description",

SUM("tPCT"."FreezeQty") "Freeze Qty", "vSS"."QtyOnHand" "Current Qty"

FROM ("vSS" "vSS"

INNER JOIN "tI" "tI"

ON "vSS"."ItemKey"="tI"."ItemKey")

LEFT OUTER JOIN "tPCT" "tPCT"

ON "vSS"."ItemKey"="tPCT"."ItemKey"

WHERE "vSS"."ItemID" = '3002954'

GROUP BY "tI"."ItemID", "vSS"."ShortDesc", "vSS"."QtyOnHand"

Hope this helps.

Regards.....

|||

If you do not need BatchKey, you can just remove it from the query and you should get the desired answer.

e.g.

Code Snippet

SELECT "tI"."ItemID", MAX("vSS"."ShortDesc") "Short Description",

MAX("tPCT"."FreezeQty") "Freeze Qty", SUM("vSS"."QtyOnHand") "Current Qty"

FROM ("vSS" "vSS"

INNER JOIN "tI" "tI"

ON "vSS"."ItemKey"="tI"."ItemKey")

LEFT OUTER JOIN "tPCT" "tPCT"

ON "vSS"."ItemKey"="tPCT"."ItemKey"

WHERE "vSS"."ItemID" = '3002954'

GROUP BY "tI"."ItemID"

|||

This did the trick!

Thanks Gopi!

Aggregate Functions Support?

Does anyone know if the jdbc driver for SQL Server
support aggregate functions such as AVG, SUM, COUNT ?
I can't find any documentation stating it does or
doesn't, but my testing seems to show that it does not.
Thanks
JD
JD wrote:

> Does anyone know if the jdbc driver for SQL Server
> support aggregate functions such as AVG, SUM, COUNT ?
> I can't find any documentation stating it does or
> doesn't, but my testing seems to show that it does not.
> Thanks
> JD
In general, the driver doesn't care about what SQL you
send to the DBMS. What problems are you having using
those functions?
Joe Weinstein at BEA
|||The result set is empty. I can successfully run the query
manually via isql.
On second thought perhaps it's an issue with the version
of java I'm running, JDK 1.2.2, that might not support
the aggregate functions.
JD
>--Original Message--
>
>JD wrote:
>
>In general, the driver doesn't care about what SQL you
>send to the DBMS. What problems are you having using
>those functions?
>Joe Weinstein at BEA
>.
>
|||anonymous@.discussions.microsoft.com wrote:

> The result set is empty. I can successfully run the query
> manually via isql.
> On second thought perhaps it's an issue with the version
> of java I'm running, JDK 1.2.2, that might not support
> the aggregate functions.
Hi. No that's not it either. If the driver doesn't care about your SQL,
the JVM certainly won't. SHow your JDBC code and function definition.
Then show what isql shows when you call it.
Joe Weinstein at BEA
[vbcol=seagreen]
> JD
|||<anonymous@.discussions.microsoft.com> schrieb im Newsbeitrag
news:2078b01c45933$f788d340$a401280a@.phx.gbl...
> The result set is empty. I can successfully run the query
> manually via isql.
> On second thought perhaps it's an issue with the version
> of java I'm running, JDK 1.2.2, that might not support
> the aggregate functions.
Nope. I'd guess that it's rather a database connection thing: do you
connect to the same database via JDBC and ISQL? Do you use the same
credentials?
robert
[vbcol=seagreen]
> JD

Aggregate functions not allowed in the dataset filter

Hi,

I was trying to filter a dataset based on a condition like this: Fields!SalesAmt.value <= Sum(Fields!SalesAmt.Value)*0.05. This is nothing but it filters those SalesAmt that are less than 5% of the total sales amount. However SSRS doesn't allow to use aggregate functions in the dataset and data region filter.

Is there another way to do this?

Sincerely,

--Amde

You're trying to hide the rows correct? if so then try this

=IIF(Fields!SalesAmt.value

<= Sum(Fields!SalesAmt.Value)*0.05, true,false)
true meaning that if it is true then hid the row.

Hook this into a parameter and you have a dynamically filterable

report.

-Fap

|||Sorrt about the font size.

Here it is again.

You're trying to hide the rows correct? if so then try this

=IIF(Fields!SalesAmt.value

<= Sum(Fields!SalesAmt.Value)*0.05, true,false)
true meaning that if it is true then hid the row.

Hook this into a parameter and you have a dynamically filterable

report.

-Fap

|||

Dear Fap,

That was not my question. I just want to filter the data based on the condition I specified earlier. Please read my question carefully and let me know if you have any idea.

Sincerely,

--Amde

|||Aggregates are not supported in filter expressions (as you have seen), but there are few ways that you can work around this. Here are the first two that come to mind.

1. Filter the data returned directly in the SQL query. This can make the query more complicated. See http://msdn2.microsoft.com/en-us/library/ms179270.aspx for more information on filtering rows.

2. Use a hidden report parameter that has its default value populated by a second dataset whose query just computes and returns the result of the Aggregate. Then use the hidden report parameter in the filter expression where you were using the aggregate.

Sample Query:
SELECT Sum(SalesAmt)*.05 AS AggregateResult FROM SalesTable

Filter Expression:
Fields!SalesAmt.Value <= Parameters!AggregateResult.Value

Ian|||

Hi,

What you have said make sense, but, the thing is I am using MDX query instead of T-Sql. The link that you sent to me is helpful. It would be more helpful if you can send me similar link that uses mdx query.

Sincerely,

--Amde

|||I'm not an expert using MDX, but one way to filter the data would be to create and add another calculated member to the query, and then filter based on the result of that member. For example, create another member that will calculate whether or not the the row should be included.

The new member calculation would look something like

WITH MEMBER [Measures].[Include] AS ([Measures].[SalesAmt] <= Sum([SalesData].[SetOfAllRow], [Measures].[SalesAmt])*0.05)


Make sure to add [Measures].[Include] to the SELECT set. Then you can use this field in the data region's filter expressions. The filter expression would look something like

Fields!Include.Value = True

Or, you can use the Filter function in the query to retrive only a subset of the data. Here is more information on filtering data using MDX queries.

Filter Function:
http://msdn2.microsoft.com/en-us/library/ms146037.aspx

Slicer Axes:
http://msdn2.microsoft.com/en-us/library/ms146047.aspx

Aggregate functions and locking

Do aggregate functions (sum, count, min, max) inherently cause table
locks?

More concretely, would the following query typically result in a table
lock?

select sum(quantity) as total
from products

Thanks,
Scottgobwash@.gmail.com (orders@.bunchesofbooks.com) writes:

Quote:

Originally Posted by

Do aggregate functions (sum, count, min, max) inherently cause table
locks?


Not as such.

Quote:

Originally Posted by

More concretely, would the following query typically result in a table
lock?
>
select sum(quantity) as total
from products


Yes. So would "SELECT quanity FROM products". The fact there is an
aggregate has nothing to do with it. What matters is that you access
all rows.

There is one situation where the table would not be locked and that is
if there is a non-clustered index which includes quantity, not necessarily
as the first column. In that case SQL Server will scan the index instead.
I don't know exactly which locks SQL Server takes out in this case. It
appears reasonable that it would lock the index, but I am not sure.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Thanks for the fast response. I just threw that out as a sample query,
but if a where clause were present, what behavior should be expected?

Scott

On Nov 29, 4:50 pm, Erland Sommarskog <esq...@.sommarskog.sewrote:

Quote:

Originally Posted by

gobw...@.gmail.com (ord...@.bunchesofbooks.com) writes:

Quote:

Originally Posted by

Do aggregate functions (sum, count, min, max) inherently cause table
locks?Not as such.


>

Quote:

Originally Posted by

More concretely, would the following query typically result in a table
lock?


>

Quote:

Originally Posted by

select sum(quantity) as total
from productsYes. So would "SELECT quanity FROM products". The fact there is an


aggregate has nothing to do with it. What matters is that you access
all rows.
>
There is one situation where the table would not be locked and that is
if there is a non-clustered index which includes quantity, not necessarily
as the first column. In that case SQL Server will scan the index instead.
I don't know exactly which locks SQL Server takes out in this case. It
appears reasonable that it would lock the index, but I am not sure.
--
Erland Sommarskog, SQL Server MVP, esq...@.sommarskog.se
>
Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

|||Gobwash (gobwash@.gmail.com) writes:

Quote:

Originally Posted by

Thanks for the fast response. I just threw that out as a sample query,
but if a where clause were present, what behavior should be expected?


It depends on the WHERE clause. If there is an index that can be used to
seek the rows, SQL Server will take out share locks. If there is no useful
index, so that the table will be scanned, there will be a table lock.

Again, aggregate or not has nothing to do with it.

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx

Tuesday, March 6, 2012

aggregate function in the argument to another aggregate function

Hello all,
I am new to SQL Reporting services and I am having trouble performing a
group sum. I Currently have a group within another group the embedded
group (group2) is performing a group sum
(=3DCount(Fields!FacilityTemp1.V=ACalue/Fields!Temp2.Value) and this works
fine but now I would like to use the outside group to perform a sum of
all values within group2 however SQL reporting Services does not allow
aggregate function in the argument to another aggregate function. Does
anyone no how I can simply get around this. Expression that does not
work:
=3DSUM(Count(Fields!FacilityTemp=AC1.Value/Fields!Temp2.Value)
Thanks=20
ChrisSince this question is specific to SQL Server Reporting Services, you
should post it in the microsoft.public.sqlserver.reportingsvcs group.
Razvan

Aggregate Function Error

I am having a problem with a query statement when I try to use sum( ) on a
field in the query. For example:
select somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate,
sum(fnetprice)
from somast inner join sorels on somast.fsono=sorels.fsono
where somast.fstatus<>'CANCELLED'
GROUP BY somast.fsono
ORDER BY somast.fsono
When I run this I get the error message as follows
************
Server: Msg 8120, Level 16, State 1, Line 1
Column 'somast.forderdate' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'somast.fcompany' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'sorels.fduedate' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
************
Can I get around this or must I include every field in the group by clause
(which will not get me the info I need)?
Thanks.
That's how GROUP BY works. Please explain what it is that you're trying to
achieve, I suspect it can be done with a subquery.
DDL and sample data might help as well.
ML
http://milambda.blogspot.com/
|||VFP did not work like this and I am new to SQL so thanks for your help on
this.
I want info from two tables SOMAST (Sales Order master table) and SORELS
(Sales Order Detail table)
I am looking for a summed total for sales orders for each due date.
I will paste my query again:
***
select somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate,
sum(fnetprice)
from somast inner join sorels on somast.fsono=sorels.fsono
where somast.fstatus<>'CANCELLED'
GROUP BY somast.fsono
ORDER BY somast.fsono
***
Can you give me an example of a subquery you mentioned?
Thanks again.
"ML" <ML@.discussions.microsoft.com> wrote in message
news:CB7A6CF5-F91B-423F-A4B1-9A7B1BE7650F@.microsoft.com...
> That's how GROUP BY works. Please explain what it is that you're trying to
> achieve, I suspect it can be done with a subquery.
> DDL and sample data might help as well.
>
> ML
> --
> http://milambda.blogspot.com/
|||For a fully working solution we would apreciate DDL and sample data.
While you're busy preparing it let me get a clear understanding of what
you're trying to achieve.
You need a list of dates when sales were made, each with the sum of
netprice. Is that right?
Maybe something like this:
select <date column>
,sum(fnetprice) as SumFnetprice
from somast
inner join sorels
on somast.fsono=sorels.fsono
where (somast.fstatus<>'CANCELLED')
group by <date column>
order by <date column>
ML
http://milambda.blogspot.com/
|||I will list some sample data for you. I will make it very simple. I hope
this is what you want. I am not sure what you mean by DDL.
SOMAST
Sales Order Company Date
1 A Company 12/1/05
2 B Company 12/1/05
3 C Company 12/2/05
4 A Company 12/2/05
SORELS
Sales Order Item Price
1 $1
1 $2
1 $2
2 $3
2 $1
3 $5
4 $3
4 $2
My query would be:
Select somast.SalesOrder, Somast.Company, Somast.Date, Sum(Sorels.ItemPrice)
as SumPrice from SOMAST inner join SOITEM on
somast.SalesOrder=sorels.SalesOrder Group By Somast.SalesOrder
Order By Somast.Date, Somast.SalesOrder
My desired results would be
Sales Order Company Date SumPrice
1 A Company 12/1/05 $5
2 B Company 12/1/05 $4
3 C Company 12/2/05 $5
4 A Company 12/2/05 $5
Instead it gives me the Aggregate Function error I mentioned.
Thanks.
"ML" <ML@.discussions.microsoft.com> wrote in message
news:1819232F-1A57-4C95-90F8-F0DAFB0F4C37@.microsoft.com...
> For a fully working solution we would apreciate DDL and sample data.
> While you're busy preparing it let me get a clear understanding of what
> you're trying to achieve.
> You need a list of dates when sales were made, each with the sum of
> netprice. Is that right?
> Maybe something like this:
> select <date column>
> ,sum(fnetprice) as SumFnetprice
> from somast
> inner join sorels
> on somast.fsono=sorels.fsono
> where (somast.fstatus<>'CANCELLED')
> group by <date column>
> order by <date column>
>
> ML
> --
> http://milambda.blogspot.com/
|||The GROUP BY clause needs to include all non-aggregate columns identified in
the SELECT clause.
Try this:
SELECT s1.fsono, s1.forderdate, s1.fcompany, s2.fduedate, sum(s1.fnetprice)
FROM somast s1
INNER JOIN sorels s2
ON s1.fsono = s2.fsono
WHERE s1.fstatus <> 'CANCELLED'
GROUP BY s1.fsono, s1.forderdate, s1.fcompany, s2.fduedate
ORDER BY s1.fsono
If you don't want to "group by" the extra fields, then the next question
would be whether you really need to display those values. If all you're
looking for is the sum of fnetprice by fsono, then the other three fields do
not need to be displayed.
However, I suspect that forderdate, fcompany, and fduedate are unique to
each fsono, in which case adding them to the GROUP BY clause won't make any
difference in the presentation of the output.
"Preacher Man" <nospam> wrote in message
news:uZX4BIYAGHA.3268@.TK2MSFTNGP10.phx.gbl...
>I am having a problem with a query statement when I try to use sum( ) on a
>field in the query. For example:
> select somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate,
> sum(fnetprice)
> from somast inner join sorels on somast.fsono=sorels.fsono
> where somast.fstatus<>'CANCELLED'
> GROUP BY somast.fsono
> ORDER BY somast.fsono
> When I run this I get the error message as follows
> ************
> Server: Msg 8120, Level 16, State 1, Line 1
> Column 'somast.forderdate' is invalid in the select list because it is not
> contained in either an aggregate function or the GROUP BY clause.
> Server: Msg 8120, Level 16, State 1, Line 1
> Column 'somast.fcompany' is invalid in the select list because it is not
> contained in either an aggregate function or the GROUP BY clause.
> Server: Msg 8120, Level 16, State 1, Line 1
> Column 'sorels.fduedate' is invalid in the select list because it is not
> contained in either an aggregate function or the GROUP BY clause.
> ************
> Can I get around this or must I include every field in the group by clause
> (which will not get me the info I need)?
> Thanks.
>
|||look up technique 1 here:
http://www.devx.com/dbzone/Article/30149
|||selet som.salesOrder,som.company,som.date,sum(sor.itemPr ice) SumPrice
from somast som
join sorels sor
group by som.salesOrder,som.company,som.date
"Preacher Man" <nospam> wrote in message
news:OSg4pSZAGHA.2356@.tk2msftngp13.phx.gbl...
>I will list some sample data for you. I will make it very simple. I hope
>this is what you want. I am not sure what you mean by DDL.
> SOMAST
> Sales Order Company Date
> 1 A Company 12/1/05
> 2 B Company 12/1/05
> 3 C Company 12/2/05
> 4 A Company 12/2/05
> SORELS
> Sales Order Item Price
> 1 $1
> 1 $2
> 1 $2
> 2 $3
> 2 $1
> 3 $5
> 4 $3
> 4 $2
> My query would be:
> Select somast.SalesOrder, Somast.Company, Somast.Date,
> Sum(Sorels.ItemPrice) as SumPrice from SOMAST inner join SOITEM on
> somast.SalesOrder=sorels.SalesOrder Group By Somast.SalesOrder
> Order By Somast.Date, Somast.SalesOrder
> My desired results would be
> Sales Order Company Date SumPrice
> 1 A Company 12/1/05 $5
> 2 B Company 12/1/05 $4
> 3 C Company 12/2/05 $5
> 4 A Company 12/2/05 $5
> Instead it gives me the Aggregate Function error I mentioned.
> Thanks.
>
> "ML" <ML@.discussions.microsoft.com> wrote in message
> news:1819232F-1A57-4C95-90F8-F0DAFB0F4C37@.microsoft.com...
>
|||Hi
VFP works like this since VFP8, in order to conform with the SQL standard
rules. Rule: The GROUP BY column-list must contain the same columns as the
Select columnlist.
In VFP9 and SQL Server you do this:
SELECT somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate, ;
(SELECT SUM(fnetprice) AS sum_netprince FROM Sorels ;
WHERE Sorels.fsono=somast.fsono ) ;
FROM Somast ;
WHERE UPPER(somast.fstatus)<>'CANCELLED' ;
ORDER BY smoast.fsono
or this:
SELECT S1.* S2.sumnetprice ;
FROM Somast AS S1 JOIN ;
(SELECT fsno. SUM( netprice ) FROM Sorels ;
GROUP BY fsnono ) AS S2 ;
ON S2.fsono=S1.fsono ;
WHERE S1.status<>'CANCELLED'
ORDER BY S1.fsono
or this:
...
GROUP BY somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate
In VFP <9 you can of course dissolve these type of queries into two queries
that you join.
In SQL Server you do as shown above.
-Anders
"Preacher Man" <nospam> skrev i meddelandet
news:e19EsgYAGHA.1028@.TK2MSFTNGP11.phx.gbl...
> VFP did not work like this and I am new to SQL so thanks for your help on
> this.
> I want info from two tables SOMAST (Sales Order master table) and SORELS
> (Sales Order Detail table)
> I am looking for a summed total for sales orders for each due date.
> I will paste my query again:
> ***
> select somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate,
> sum(fnetprice)
> from somast inner join sorels on somast.fsono=sorels.fsono
> where somast.fstatus<>'CANCELLED'
> GROUP BY somast.fsono
> ORDER BY somast.fsono
> ***
> Can you give me an example of a subquery you mentioned?
> Thanks again.
> "ML" <ML@.discussions.microsoft.com> wrote in message
> news:CB7A6CF5-F91B-423F-A4B1-9A7B1BE7650F@.microsoft.com...
>
|||Hi Preacher,
I'd like to go a little further with what the others have said. If you don't
include every item from the Select list in the Group By list, excepting the
aggregates, what values do you expect to get in the other columns? Random
values?
When you say that including everything in the Group By doesn't get what you
need, what were you expecting to get?
If you think about it, getting random values means that those values have no
connection whatsoever to the aggregate value. If there is a particular
(predictable) value that you are expecting, how do you expect the SQL engine
to choose that value? Of course, if the value is predictable then grouping
on that column should get that predictable value.
Cindy Winegarden MSCD, Microsoft Visual FoxPro MVP
cindy_winegarden@.msn.com www.cindywinegarden.com
"Preacher Man" <nospam> wrote in message
news:uZX4BIYAGHA.3268@.TK2MSFTNGP10.phx.gbl...

> select somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate,
> sum(fnetprice)
> from somast inner join sorels on somast.fsono=sorels.fsono
> where somast.fstatus<>'CANCELLED'
> GROUP BY somast.fsono
> ORDER BY somast.fsono

> Can I get around this or must I include every field in the group by clause
> (which will not get me the info I need)?

Aggregate Function Error

I am having a problem with a query statement when I try to use sum( ) on a
field in the query. For example:
select somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate,
sum(fnetprice)
from somast inner join sorels on somast.fsono=sorels.fsono
where somast.fstatus<>'CANCELLED'
GROUP BY somast.fsono
ORDER BY somast.fsono
When I run this I get the error message as follows
************
Server: Msg 8120, Level 16, State 1, Line 1
Column 'somast.forderdate' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'somast.fcompany' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
Server: Msg 8120, Level 16, State 1, Line 1
Column 'sorels.fduedate' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
************
Can I get around this or must I include every field in the group by clause
(which will not get me the info I need)?
Thanks.
That's how GROUP BY works. Please explain what it is that you're trying to
achieve, I suspect it can be done with a subquery.
DDL and sample data might help as well.
ML
http://milambda.blogspot.com/
|||VFP did not work like this and I am new to SQL so thanks for your help on
this.
I want info from two tables SOMAST (Sales Order master table) and SORELS
(Sales Order Detail table)
I am looking for a summed total for sales orders for each due date.
I will paste my query again:
***
select somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate,
sum(fnetprice)
from somast inner join sorels on somast.fsono=sorels.fsono
where somast.fstatus<>'CANCELLED'
GROUP BY somast.fsono
ORDER BY somast.fsono
***
Can you give me an example of a subquery you mentioned?
Thanks again.
"ML" <ML@.discussions.microsoft.com> wrote in message
news:CB7A6CF5-F91B-423F-A4B1-9A7B1BE7650F@.microsoft.com...
> That's how GROUP BY works. Please explain what it is that you're trying to
> achieve, I suspect it can be done with a subquery.
> DDL and sample data might help as well.
>
> ML
> --
> http://milambda.blogspot.com/
|||For a fully working solution we would apreciate DDL and sample data.
While you're busy preparing it let me get a clear understanding of what
you're trying to achieve.
You need a list of dates when sales were made, each with the sum of
netprice. Is that right?
Maybe something like this:
select <date column>
,sum(fnetprice) as SumFnetprice
from somast
inner join sorels
on somast.fsono=sorels.fsono
where (somast.fstatus<>'CANCELLED')
group by <date column>
order by <date column>
ML
http://milambda.blogspot.com/
|||I will list some sample data for you. I will make it very simple. I hope
this is what you want. I am not sure what you mean by DDL.
SOMAST
Sales Order Company Date
1 A Company 12/1/05
2 B Company 12/1/05
3 C Company 12/2/05
4 A Company 12/2/05
SORELS
Sales Order Item Price
1 $1
1 $2
1 $2
2 $3
2 $1
3 $5
4 $3
4 $2
My query would be:
Select somast.SalesOrder, Somast.Company, Somast.Date, Sum(Sorels.ItemPrice)
as SumPrice from SOMAST inner join SOITEM on
somast.SalesOrder=sorels.SalesOrder Group By Somast.SalesOrder
Order By Somast.Date, Somast.SalesOrder
My desired results would be
Sales Order Company Date SumPrice
1 A Company 12/1/05 $5
2 B Company 12/1/05 $4
3 C Company 12/2/05 $5
4 A Company 12/2/05 $5
Instead it gives me the Aggregate Function error I mentioned.
Thanks.
"ML" <ML@.discussions.microsoft.com> wrote in message
news:1819232F-1A57-4C95-90F8-F0DAFB0F4C37@.microsoft.com...
> For a fully working solution we would apreciate DDL and sample data.
> While you're busy preparing it let me get a clear understanding of what
> you're trying to achieve.
> You need a list of dates when sales were made, each with the sum of
> netprice. Is that right?
> Maybe something like this:
> select <date column>
> ,sum(fnetprice) as SumFnetprice
> from somast
> inner join sorels
> on somast.fsono=sorels.fsono
> where (somast.fstatus<>'CANCELLED')
> group by <date column>
> order by <date column>
>
> ML
> --
> http://milambda.blogspot.com/
|||The GROUP BY clause needs to include all non-aggregate columns identified in
the SELECT clause.
Try this:
SELECT s1.fsono, s1.forderdate, s1.fcompany, s2.fduedate, sum(s1.fnetprice)
FROM somast s1
INNER JOIN sorels s2
ON s1.fsono = s2.fsono
WHERE s1.fstatus <> 'CANCELLED'
GROUP BY s1.fsono, s1.forderdate, s1.fcompany, s2.fduedate
ORDER BY s1.fsono
If you don't want to "group by" the extra fields, then the next question
would be whether you really need to display those values. If all you're
looking for is the sum of fnetprice by fsono, then the other three fields do
not need to be displayed.
However, I suspect that forderdate, fcompany, and fduedate are unique to
each fsono, in which case adding them to the GROUP BY clause won't make any
difference in the presentation of the output.
"Preacher Man" <nospam> wrote in message
news:uZX4BIYAGHA.3268@.TK2MSFTNGP10.phx.gbl...
>I am having a problem with a query statement when I try to use sum( ) on a
>field in the query. For example:
> select somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate,
> sum(fnetprice)
> from somast inner join sorels on somast.fsono=sorels.fsono
> where somast.fstatus<>'CANCELLED'
> GROUP BY somast.fsono
> ORDER BY somast.fsono
> When I run this I get the error message as follows
> ************
> Server: Msg 8120, Level 16, State 1, Line 1
> Column 'somast.forderdate' is invalid in the select list because it is not
> contained in either an aggregate function or the GROUP BY clause.
> Server: Msg 8120, Level 16, State 1, Line 1
> Column 'somast.fcompany' is invalid in the select list because it is not
> contained in either an aggregate function or the GROUP BY clause.
> Server: Msg 8120, Level 16, State 1, Line 1
> Column 'sorels.fduedate' is invalid in the select list because it is not
> contained in either an aggregate function or the GROUP BY clause.
> ************
> Can I get around this or must I include every field in the group by clause
> (which will not get me the info I need)?
> Thanks.
>
|||look up technique 1 here:
http://www.devx.com/dbzone/Article/30149
|||selet som.salesOrder,som.company,som.date,sum(sor.itemPr ice) SumPrice
from somast som
join sorels sor
group by som.salesOrder,som.company,som.date
"Preacher Man" <nospam> wrote in message
news:OSg4pSZAGHA.2356@.tk2msftngp13.phx.gbl...
>I will list some sample data for you. I will make it very simple. I hope
>this is what you want. I am not sure what you mean by DDL.
> SOMAST
> Sales Order Company Date
> 1 A Company 12/1/05
> 2 B Company 12/1/05
> 3 C Company 12/2/05
> 4 A Company 12/2/05
> SORELS
> Sales Order Item Price
> 1 $1
> 1 $2
> 1 $2
> 2 $3
> 2 $1
> 3 $5
> 4 $3
> 4 $2
> My query would be:
> Select somast.SalesOrder, Somast.Company, Somast.Date,
> Sum(Sorels.ItemPrice) as SumPrice from SOMAST inner join SOITEM on
> somast.SalesOrder=sorels.SalesOrder Group By Somast.SalesOrder
> Order By Somast.Date, Somast.SalesOrder
> My desired results would be
> Sales Order Company Date SumPrice
> 1 A Company 12/1/05 $5
> 2 B Company 12/1/05 $4
> 3 C Company 12/2/05 $5
> 4 A Company 12/2/05 $5
> Instead it gives me the Aggregate Function error I mentioned.
> Thanks.
>
> "ML" <ML@.discussions.microsoft.com> wrote in message
> news:1819232F-1A57-4C95-90F8-F0DAFB0F4C37@.microsoft.com...
>
|||Hi
VFP works like this since VFP8, in order to conform with the SQL standard
rules. Rule: The GROUP BY column-list must contain the same columns as the
Select columnlist.
In VFP9 and SQL Server you do this:
SELECT somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate, ;
(SELECT SUM(fnetprice) AS sum_netprince FROM Sorels ;
WHERE Sorels.fsono=somast.fsono ) ;
FROM Somast ;
WHERE UPPER(somast.fstatus)<>'CANCELLED' ;
ORDER BY smoast.fsono
or this:
SELECT S1.* S2.sumnetprice ;
FROM Somast AS S1 JOIN ;
(SELECT fsno. SUM( netprice ) FROM Sorels ;
GROUP BY fsnono ) AS S2 ;
ON S2.fsono=S1.fsono ;
WHERE S1.status<>'CANCELLED'
ORDER BY S1.fsono
or this:
...
GROUP BY somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate
In VFP <9 you can of course dissolve these type of queries into two queries
that you join.
In SQL Server you do as shown above.
-Anders
"Preacher Man" <nospam> skrev i meddelandet
news:e19EsgYAGHA.1028@.TK2MSFTNGP11.phx.gbl...
> VFP did not work like this and I am new to SQL so thanks for your help on
> this.
> I want info from two tables SOMAST (Sales Order master table) and SORELS
> (Sales Order Detail table)
> I am looking for a summed total for sales orders for each due date.
> I will paste my query again:
> ***
> select somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate,
> sum(fnetprice)
> from somast inner join sorels on somast.fsono=sorels.fsono
> where somast.fstatus<>'CANCELLED'
> GROUP BY somast.fsono
> ORDER BY somast.fsono
> ***
> Can you give me an example of a subquery you mentioned?
> Thanks again.
> "ML" <ML@.discussions.microsoft.com> wrote in message
> news:CB7A6CF5-F91B-423F-A4B1-9A7B1BE7650F@.microsoft.com...
>
|||Hi Preacher,
I'd like to go a little further with what the others have said. If you don't
include every item from the Select list in the Group By list, excepting the
aggregates, what values do you expect to get in the other columns? Random
values?
When you say that including everything in the Group By doesn't get what you
need, what were you expecting to get?
If you think about it, getting random values means that those values have no
connection whatsoever to the aggregate value. If there is a particular
(predictable) value that you are expecting, how do you expect the SQL engine
to choose that value? Of course, if the value is predictable then grouping
on that column should get that predictable value.
Cindy Winegarden MSCD, Microsoft Visual FoxPro MVP
cindy_winegarden@.msn.com www.cindywinegarden.com
"Preacher Man" <nospam> wrote in message
news:uZX4BIYAGHA.3268@.TK2MSFTNGP10.phx.gbl...

> select somast.fsono, somast.forderdate, somast.fcompany, sorels.fduedate,
> sum(fnetprice)
> from somast inner join sorels on somast.fsono=sorels.fsono
> where somast.fstatus<>'CANCELLED'
> GROUP BY somast.fsono
> ORDER BY somast.fsono

> Can I get around this or must I include every field in the group by clause
> (which will not get me the info I need)?