File size: 2,817 Bytes
ad8da65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Subqueries

For a general overview of this endpoint, see the {doc}`endpoint_overview` page.

Subqueries are a powerful feature of modern SQL. The query endpoint
can support subqueries via the `"subquery"` field in the request body.
The format of `"subquery"` is exactly the same as the full request body 
and can even support recursive subqueries! Here are some helpful examples
that show how to use them.

## Concise queries with subqueries
Sometimes you may have a calculation that needs to be aggregated in multiple ways.
One option would be to repeat the calculation in each aggregation's
`select`, but that can lead to lots of repeated JSON. Subqueries can be
used to reduce duplicated expressions. In this example, we use a subquery
to square the property `Home_Value`, then aggregate with `min`, `max`, and `avg` 
without repeating the calculation.

```json
{
  "select": [
    {
      "function": "max",
      "alias": "max",
      "parameters": {
        "property": {
          "alias_ref": "hv_squared"
        }
      }
    },
    {
      "function": "min",
      "alias": "min",
      "parameters": {
        "property": {
          "alias_ref": "hv_squared"
        }
      }
    },
    {
      "function": "avg",
      "alias": "avg",
      "parameters": {
        "property": {
          "alias_ref": "hv_squared"
        }
      }
    }
  ],
  "subquery": {
    "select": [
      {
        "function": "multiply",
        "alias": "hv_squared",
        "parameters": {
          "left": "Home_Value",
          "right": "Home_Value"
        }
      }
    ]
  }
}
```

This example returns:
```json
{
    "query_result": [
        {
            "avg": 33413668226.974968,
            "max": 17640000000000,
            "min": 0
        }
    ]
}
```

## Subqueries for grouping
Subqueries can also be used to perform operations on grouped data.
In this example, we get the count of the inferences in each batch 
in the subquery, then average those counts.
```json
{
    "select": [
        {
            "function": "avg",
            "alias": "avg_count",
            "parameters": {
                "property": {
                    "alias_ref": "batch_count"
                }
            }
        },
        {
            "function": "count",
            "alias": "total_batches"
        }
    ],
    "subquery": {
        "select": [
            {
                "function": "count",
                "alias": "batch_count"
            },
            {
                "property": "batch_id"
            }
        ],
        "group_by": [
            {
                "property": "batch_id"
            }
        ]
    }
}
```

This query returns: 
```json
{
    "query_result": [
        {
            "avg_count": 5930.2558139534885,
            "total_batches": 86
        }
    ]
}
```