Monday, March 12, 2012

Help needed on simple SUM query

I tried this query:
SELECT `QuoteId`, `QuoteItemId`, SUM(QtyQuoted*Price)
FROM `QuoteItem`
GROUP BY `QuoteId`
HAVING (`QuoteId` LIKE "250033")
to get the values of each QuoteItemId.
There are 7 different items ion this quote, but this query is summing
them ALL together.
I want results like:
250033, 1, 50
250033, 2, 38
250033, 3, 75
250033, 4, 100
etc...
Please advise where I'm getting it wrong.
I googled the topic and I must just have some killer blinders on.
I did try a few other things, but no go.
I know it's going to be simple, but I'm just not seeing it.
Thanks-a-bunch,
MoUse square brackets as name delimiters - not single quotes.
--
Tom
----
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
SQL Server MVP
Toronto, ON Canada
https://mvp.support.microsoft.com/profile/Tom.Moreau
"Mo" <Mehile.Orloff@.gmail.com> wrote in message
news:35683345-c1af-4eac-b8fb-58e7bd73205d@.e10g2000prf.googlegroups.com...
I tried this query:
SELECT `QuoteId`, `QuoteItemId`, SUM(QtyQuoted*Price)
FROM `QuoteItem`
GROUP BY `QuoteId`
HAVING (`QuoteId` LIKE "250033")
to get the values of each QuoteItemId.
There are 7 different items ion this quote, but this query is summing
them ALL together.
I want results like:
250033, 1, 50
250033, 2, 38
250033, 3, 75
250033, 4, 100
etc...
Please advise where I'm getting it wrong.
I googled the topic and I must just have some killer blinders on.
I did try a few other things, but no go.
I know it's going to be simple, but I'm just not seeing it.
Thanks-a-bunch,
Mo|||As Tom noted you need to remove the single quotes. I see your grouping is
incorrect too. Try this:
SELECT QuoteId, QuoteItemId, SUM(QtyQuoted * Price)
FROM QuoteItem
GROUP BY QuoteId, QuoteItemId
HAVING QuoteId = '250033'
HTH,
Plamen Ratchev
http://www.SQLStudio.com|||On Apr 2, 10:33=A0am, "Tom Moreau" <t...@.dont.spam.me.cips.ca> wrote:
> Use square brackets as name delimiters - not single quotes.
> --
> =A0 =A0Tom
> ----
> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
> SQL Server MVP
> Toronto, ON =A0 Canadahttps://mvp.support.microsoft.com/profile/Tom.Moreau=
> "Mo" <Mehile.Orl...@.gmail.com> wrote in message
> news:35683345-c1af-4eac-b8fb-58e7bd73205d@.e10g2000prf.googlegroups.com...
> I tried this query:
> SELECT `QuoteId`, `QuoteItemId`, SUM(QtyQuoted*Price)
> FROM `QuoteItem`
> GROUP BY `QuoteId`
> HAVING (`QuoteId` LIKE "250033")
> to get the values of each QuoteItemId.
> There are 7 different items ion this quote, but this query is summing
> them ALL together.
> I want results like:
> 250033, 1, 50
> 250033, 2, 38
> 250033, 3, 75
> 250033, 4, 100
> etc...
> Please advise where I'm getting it wrong.
> I googled the topic and I must just have some killer blinders on.
> I did try a few other things, but no go.
> I know it's going to be simple, but I'm just not seeing it.
> Thanks-a-bunch,
> Mo
Thanks.
I've figured out how to do what I need by replacing the HAVING
statement with a WHERE statement between the FROM and GROUP BY.
Thanks.
-Mo|||On Apr 2, 10:55=A0am, "Plamen Ratchev" <Pla...@.SQLStudio.com> wrote:
> As Tom noted you need to remove the single quotes. I see your grouping is
> incorrect too. Try this:
> SELECT QuoteId, QuoteItemId, SUM(QtyQuoted * Price)
> FROM QuoteItem
> GROUP BY QuoteId, QuoteItemId
> HAVING QuoteId =3D '250033'
> HTH,
> Plamen Ratchevhttp://www.SQLStudio.com
Thanks, I'll put your advise into operation.
Regards.|||>>
I've figured out how to do what I need by replacing the HAVING
statement with a WHERE statement between the FROM and GROUP BY.
I don't think that was the (only) problem.
:-)

No comments:

Post a Comment