开发者

SQL Server combining data question

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


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  


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜