If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Sub Query With Subtotals

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-09-02, 13:57
Justin Justin is offline
Registered User
 
Join Date: Jun 2002
Posts: 12
Sub Query With Subtotals

Hello, Thank you for your help.

I Need to produce a table with sub totals (the number of items by color in an inventory table)

I'm trying:
SELECT Color, ModelID,
(SELECT COUNT(*)) AS SubTotal
FROM dbo.Inventory_Joins
GROUP BY Color, ModelID

But I'm stuck

I Need to produce

ID Color Qty in Stock
1 Brown 3
2 Green 5
__________________
Justin Case
Reply With Quote
  #2 (permalink)  
Old 07-09-02, 14:03
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
if ModelID is a primary key, you won't want to group on it (there's only going to be one of each, eh?) --

select Color, count(*) as subtotal
into NameOfYourNewTable
from dbo.Inventory_Joins
group by Color

if ModelID is not the primary key, then yeah, add it into the above like this --

select ModelID, Color, count(*) as subtotal
into NameOfYourNewTable
from dbo.Inventory_Joins
group by ModelID, Color

rudy
http://rudy.ca/
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On