在选择期间转换为负数

时间:2013-05-30 18:06:26

标签: sql-server-2008

试图找出如何在select语句中将整数和十进制列转换为负数。任何帮助表示赞赏。

Begin
  Select 
    OrderInvoiceHeader.Invoice_int As [Invoice #],
    CustomerBillTo.BillToCustomerName As [Customer name],
    OrderInvoiceHeader.Invoice_date As [Invoice date],
    Salesmen.SalesmanName As [Salesman],
    (Select 
      Cast(Round(SUM(BaleInventory.NetWeight_dec), 0) As int)
      from dbo.BaleInventory

      Where dbo.BaleInventory.BaleNumber In     
       (Select OrderInvoiceBales.BaleNumber 
       from OrderInvoiceBales 
       Where OrderInvoiceBales.Invoice_int = OrderInvoiceHeader.Invoice_int)) 
       as [Lbs invoiced],

我需要将InvoiceAdjustment_dec转换为否定if OrderInvoiceHeader.InvoiceType = "3",其他美元价值也是如此


OrderInvoiceHeader.InvoiceAdjustment_dec As [Adjustment $],
OrderInvoiceHeader.InvoiceFreight_dec As [Freight $],
OrderInvoiceHeader.InvoiceMerchandise_dec As [Merch $],
OrderInvoiceHeader.InvoiceTotal_dec As [Total $],
OrderInvoiceHeader.FreightPaid_dec As [Frt paid $]


From OrderInvoiceHeader

  Left Outer Join dbo.CustomerBillTo
  On OrderInvoiceHeader.BillToCustomer_int = CustomerBillTo.BillToCustomer_int

  Left Outer Join dbo.Salesmen
  On OrderInvoiceHeader.Salesman_int = Salesmen.Salesman_int

  Where 
    OrderInvoiceHeader.Invoice_date >= @From_date and
    OrderInvoiceHeader.Invoice_date <= @To_date

    Order By OrderInvoiceHeader.Invoice_int
End

1 个答案:

答案 0 :(得分:2)

使用CASE表达式:

SELECT ..... --all your other columns here
       CASE WHEN OrderInvoiceHeader.InvoiceType = '3' 
       THEN -1*InvoiceAdjustment_dec 
       ELSE InvoiceAdjustment_dec END AS InvoiceAdjustment_dec