Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

SQL- Getting Subtotal From a Query

Participant ,
Sep 30, 2009 Sep 30, 2009

Hi,

I need to run a query where I can get a subtotal for each items in a table.  The query should produce something like:

3 apple

4 grape

1 orange

I did a SUM and COUNT function but, I get the total records (8) in the table instead of the subtotals.

SELECT      id, SUM(fruit) AS Total

FROM         table
WHERE     ORDER BY fruit

Any suggestions would be great.

Thanks,

Jenn

TOPICS
Getting started
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 30, 2009 Sep 30, 2009

A group by clause in your query might help.  In fact, I'm surprised that it ran without one.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 30, 2009 Sep 30, 2009

The group by should look like so:

SELECT      COUNT(id), fruit

FROM         table
GROUP BY fruit

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Oct 07, 2009 Oct 07, 2009
LATEST

try this

SELECT     fruit,count(*) as fruit_count, SUM(fruit) AS Total

FROM         table
WHERE     group BY fruit

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources