Solved

How to use Group By to Sum a query field

Posted on 2008-10-31
1
214 Views
Last Modified: 2010-03-20
I have the following query (in code snippet) which runs perfectly.  However, I need to modify it slightly to sum the column named 'Quantity'.  However, everything I try in creating a GROUP BY line fails.  Can somebody rewrite to group & sum the 'Quantity' column?

Thanks
SELECT     TOP 100 PERCENT item_id AS [Part Number], 'TSI' AS [Distributor Code], CONVERT(decimal(5, 0), qty_on_hand) AS Quantity, '25' AS [% Discount], 
                      CONVERT(decimal(8, 2), price1) AS [Sell Price], CONVERT(varchar, GETDATE(), 101) + '|' AS [Date]
FROM         dbo.p21_view_inv_loc
WHERE     (qty_on_hand > 0) AND (product_group_id LIKE 'mac%')
ORDER BY item_id

Open in new window

0
Comment
Question by:timoteoga
1 Comment
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 500 total points
ID: 22852954
Try:


select [part number], [distributor code], [discount], [sell price],[date] , sum(quantity) as Sum_quantity
from  
(SELECT     TOP 100 PERCENT item_id AS [Part Number], 'TSI' AS [Distributor Code], CONVERT(decimal(5, 0), qty_on_hand) AS Quantity, '25' AS [% Discount], 
                      CONVERT(decimal(8, 2), price1) AS [Sell Price], CONVERT(varchar, GETDATE(), 101) + '|' AS [Date]
FROM         dbo.p21_view_inv_loc
WHERE     (qty_on_hand > 0) AND (product_group_id LIKE 'mac%')
) a
group by  [part number], [distributor code], [discount], [sell price],[date] 
order by [part number]

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question