?
Solved

How to use Group By to Sum a query field

Posted on 2008-10-31
1
Medium Priority
?
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

764 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