mysql-在列值对应的行之间减去值

时间:2019-03-06 20:21:56

标签: mysql sql subtraction

我有一个像这样的表

ex

对于每行,我想从Sub SplitandFilterSheet() ' Declare objects Dim sourceSheet As Worksheet Dim targetSheet As Worksheet Dim splitOrderNum As Range Dim sourceCell As Range ' Declare other variables Dim sourceSheetName As String Dim sourceRangeName As String Dim targetSheetName As String Dim targetRangeName As String Dim lastSheetHidden As Boolean ' <<< Customize this >>> sourceSheetName = "Sum To Line Item" sourceRangeName = "SplitOrderNum" targetRangeName = "OrderData" ' Initialize the source sheet Set sourceSheet = ThisWorkbook.Sheets(sourceSheetName) ' Initialize the range (Add full qualifier to the current workbook, sheet and range) Set splitOrderNum = sourceSheet.Range(sourceRangeName) ' Get if last sheet is visible in current workbook lastSheetHidden = Not ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = True For Each sourceCell In splitOrderNum ' Copy the source worksheet sourceSheet.Copy After:=Worksheets(ThisWorkbook.Sheets.Count) ' Rename the new worksheet Sheets(ThisWorkbook.Sheets.Count).Name = sourceCell.Value ' Reference to the added worksheet Set targetSheet = ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) With targetSheet.Range(targetRangeName) .AutoFilter Field:=2, Criteria1:="<>" & sourceCell.Value, Operator:=xlFilterValues .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete End With ' Check this next line if this should point to the orderdata range too (?) targetSheet.AutoFilter.ShowAllData Next sourceCell ' Return the last sheet visible state If lastSheetHidden = False Then ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Visible = Not lastSheetHidden End If End Sub 中减去Number | Event | Weight 1 4 150 1 4 160 2 5 200 2 4 200 3 6 190 3 6 195 Weight匹配的另一行的Weight(如果存在)。所需的输出是:

Number

这样的操作可行吗?不知道是否相关,最终我将需要将此查询转换为Event。预先感谢。

2 个答案:

答案 0 :(得分:1)

可以通过简单地减去联接表中的列来完成。当其中一个操作数为空时,算术运算的结果为空:

select a.Number, a.Event, a.Weight, a.Weight - b.Weight as DIFF
from a
left join b on a.Number = b.Number and a.Event = b.Event

答案 1 :(得分:1)

您需要左联接:

select 
  t.*,
  t.weight - tt.weight diff
from tablename t left join tablename tt
on tt.number = t.number and tt.event = t.event and tt.weight <> t.weight