views:

47

answers:

2

Hey all I am trying to combine my data into one sum. This is my output right now:

Amount
---------
$258.0
$400.0
$1011.0
$628.0
$628.0
$340.0
$340.0
$1764.0

of course the total would be $5369. This is the type of output I need

Description   | Quantity | Price | Amount
--------------------------------------------
Fees            8          $1.50   $12.00
Redep                              $5369.00

                                   $5381.00

Only information above I would really need is the 8, 12, 5369.00 and 5381.00.

And this is my query to get those values I first posted:

SELECT '$' + CONVERT(varchar(50),round((CONVERT(int,Points) * .1),0)) AS 'Amount' 
  FROM tblHGP HGP,  
       OrderDetails OD, 
       tblInvoices i
  JOIN tblCS cs ON i.INumber = cs.INumber
  JOIN tblECI ac ON i.INumber = ac.INumber 
 WHERE cs.SoldTo = HGP.ECard 
   AND issued BETWEEN '2010-09-01' AND '2010-09-30 23:59:59' 
   AND Country = 'US' 
   AND HGP.iNumber = OD.orderdetail 
ORDER BY issued
+4  A: 

Following your clarification if you really must do all this in the query itself I think you need something like.

DECLARE @Points float, @Qty int

SELECT @Points = SUM(Points), @Qty = COUNT(*)
  FROM tblHGP HGP,  
       OrderDetails OD, 
       tblInvoices i
  JOIN tblCS cs ON i.INumber = cs.INumber
  JOIN tblECI ac ON i.INumber = ac.INumber 
 WHERE cs.SoldTo = HGP.ECard 
   AND issued BETWEEN '2010-09-01' AND '2010-09-30 23:59:59' 
   AND Country = 'US' 
   AND HGP.iNumber = OD.orderdetail 


SELECT [Description],Quantity,Price, Amount
FROM
( 
SELECT 1 AS OrderBy, 'Fees' AS [Description],@Qty AS Quantity, 1.50 AS  Price , 1.5*@Qty AS Amount
UNION ALL   
SELECT 2 AS OrderBy, 'Redep' AS [Description],NULL AS Quantity, NULL AS  Price , @Points AS Amount
UNION ALL   
SELECT 3 AS OrderBy, NULL AS [Description],NULL AS Quantity, NULL AS  Price , @Points + 1.5*@Qty AS Amount
) D
ORDER BY OrderBy  
Martin Smith
Doesn't seem to work, Martin. I do not have Description, Quantity, Price in my table. I just gave them names to better understand what i was trying to do.
StealthRT
I now get an error [Msg 8117, Level 16, State 1, Line 3Operand data type varchar is invalid for sum operator] when trying that code of yours that you just posted.
StealthRT
I presume you must be storing numeric data as varchar then? Not a good idea. You'll need to do the `CONVERT(int,Points)` from your original query.
Martin Smith
Great example there, Martin! Thanks!!!
StealthRT
+1  A: 

For results on a single line, try:

SELECT count(*) fees_quantity,
       1.5 fees_price,
       1.5 * count(*) fees_amount,
       round(SUM((CONVERT(int,Points) * .1)),0)) redep_amount,
       round(SUM((CONVERT(int,Points) * .1)),0)) + 1.5 * count(*) total_amount
  FROM tblHGP HGP,  
       OrderDetails OD, 
       tblInvoices i
  JOIN tblCS cs ON i.INumber = cs.INumber
  JOIN tblECI ac ON i.INumber = ac.INumber 
 WHERE cs.SoldTo = HGP.ECard 
   AND issued BETWEEN '2010-09-01' AND '2010-09-30 23:59:59' 
   AND Country = 'US' 
   AND HGP.iNumber = OD.orderdetail 
ORDER BY issued
Mark Bannister
Great example, Mark! Thanks! :o)
StealthRT
@Mark. Another quick question. How can i set each of those to a varible? @FeesQty = fees_amount, @FeesTotal = total_amount, etc...?
StealthRT
@StealthRT, Similar method to Martin - declare the variables first, then change the query items around; so `SELECT count(*) fees_quantity, 1.5 fees_price,`... becomes `SELECT @fees_quantity=count(*), @fees_price=1.5`... etc.
Mark Bannister
Thanks again, Mark :o)
StealthRT