我是SQL Server的新手,我正在尝试创建一个报告,该报告将使发票总额减少10%

时间:2015-03-06 12:56:49

标签: sql-server

我正在尝试创建一个存储过程,我可以使用该过程创建报告以显示客户的发票和临时列(我不希望这些实际上只是在表中报告)公司折扣(-10%)和发票金额总额 - 10%。

ALTER PROCEDURE [dbo].[GetInvoice10incTotal]
(@invoiceid INT)
AS
BEGIN
   SELECT     
      dbo.Invoices.Invoice_Number, dbo.Invoices.Invoice_Date, 
      dbo.Invoices.Invoice_Amount, dbo.Customer.CustomerID, 
      dbo.Customer.First_Name, dbo.Customer.Last_Name, 
      (dbo.Invoices.Invoice_Amount * 0.1) AS Corporate_Discount 
   FROM         
      dbo.Customer 
   INNER JOIN
      dbo.Invoices ON dbo.Customer.CustomerID = dbo.Invoices.CustomerID
   WHERE 
      dbo.Invoices.Invoice_Number = @invoiceid 

   SELECT     
      dbo.Invoices.Invoice_Number, dbo.Invoices.Invoice_Date, 
      dbo.Invoices.Invoice_Amount, dbo.Customer.CustomerID, 
      dbo.Customer.First_Name, dbo.Customer.Last_Name, 
      (dbo.Invoices.Invoice_Amount - Corporate_Discount) AS Total 
   FROM         
      dbo.Customer 
   INNER JOIN
      dbo.Invoices ON dbo.Customer.CustomerID = dbo.Invoices.CustomerID
   WHERE 
      dbo.Invoices.Invoice_Number = @invoiceid 
END

/* SET NOCOUNT ON */
RETURN 

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT  dbo.Invoices.Invoice_Number ,
        dbo.Invoices.Invoice_Date ,
        dbo.Invoices.Invoice_Amount ,
        dbo.Customer.CustomerID ,
        dbo.Customer.First_Name ,
        dbo.Customer.Last_Name ,
        dbo.Invoices.Invoice_Amount * 0.1 AS Corporate_Discount ,
        dbo.Invoices.Invoice_Amount - dbo.Invoices.Invoice_Amount * 0.1 AS Total
FROM    dbo.Customer
        INNER JOIN dbo.Invoices ON dbo.Customer.CustomerID = dbo.Invoices.CustomerID
WHERE   dbo.Invoices.Invoice_Number = @invoiceid 

答案 1 :(得分:0)

您可以使用UNION合并两个查询,但这可能不是您所追求的(也是您不能在第二个查询中使用别名)。

看起来你宁愿通过一个查询水平扩展(添加列),例如:

SELECT dbo.Invoices.Invoice_Number, 
dbo.Invoices.Invoice_Date, 
dbo.Invoices.Invoice_Amount, 
dbo.Customer.CustomerID, 
dbo.Customer.First_Name, 
dbo.Customer.Last_Name, 
(dbo.Invoices.Invoice_Amount * 0.1) AS Corporate_Discount,
(dbo.Invoices.Invoice_Amount - (dbo.Invoices.Invoice_Amount * 0.1)) AS Total 
FROM dbo.Customer INNER JOIN
dbo.Invoices ON dbo.Customer.CustomerID = dbo.Invoices.CustomerID
WHERE dbo.Invoices.Invoice_Number=@invoiceid 

答案 2 :(得分:0)

感谢大家非常感谢,当我真正阅读建议时,它更有意义。我不相信我没有想到* 0.9而不是* 0.1。干杯