从Postgresql数据生成JSON树的有效方法

时间:2018-07-10 21:43:16

标签: json postgresql plpgsql

是否有比此递归函数更有效的方法来在JSON对象中生成树结构?使用这样的递归函数有什么问题吗?

这是它查询的表。根类别从0继承。

int i = 5;
int x = ++i + --i;
//       ^
//   (i = i + 1) + --i
//   (i = 5 + 1) + --i  
//   (i = 6)     + --i
//                   ^
//        6      + (i = i - 1)
//        6      + (i = 6 - 1)
//        6      + (i = 5)
//        6      + 5
// x = 11
int y = i++ + i--;
//       ^
//    (i = 5) + i--
//         5  + i--
//     i = i + 1 // post evaluation, after i was evaluated to 5, now i increments its value and it is 6
//         5 + (i = 6)
//         5 + 6
// y = 11
//     i = i - 1 // post evaluation, after i was evaluated to 6, now i decrements its value and it is 5
System.out.println("x=" + x); // x=11
System.out.println("y=" + y); // y=11

这是生成树的根函数

Dim LastRow As Long, Value As Long, Sum As Long, Count As Long

' Get the last row, looking at all 3 columns "AC:AE"
LastRow = FindLastRow(wbFrom.Sheets("Sheet0").Range("AC:AE"))

' Iterate through all 3 columns
For Each Cla In wbFrom.Sheets("Sheet0").Range("AC9:AE" & LastRow)

    ' Use Val() to get just the numeric value and the inline IIF() statment to automatically adjust the speed
    Value = Val(Cla.Value) * IIf(InStr(Cla.Value, "Gbps") > 0, 1000, 1)

    ' Check if there is a Value, if so, Add it to the Sum (and increment the count)
    If Value > 0 Then
        Sum = Sum + Value
        Count = Count + 1
    End If
Next Cla

' Write the Sum to the other Workbook (not sure if you need the Count)
wbTo.Sheets("Sheet1").Range("D13") = Sum

这是递归函数

Public Function FindLastRow(r As Range) As Long
' Works on Filtered Lists/Hidden rows
Const NotFoundResult As Long = 1 ' If all cells are empty (no value, no formula), this value is returned
    FindLastRow = r.Worksheet.Evaluate("IFERROR(LARGE(ROW('" & r.Worksheet.Name & "'!" & r.Address & ")*--(NOT(ISBLANK('" & r.Worksheet.Name & "'!" & r.Address & "))),1)," & NotFoundResult & ")")
End Function

0 个答案:

没有答案