Details
Description
BUCKET: policies
Using N1QL to insert data:
{noformat}
insert into policies (key, value)
values ("c123",
{"policyno": "C123",
"state": "CA",
"zipcode": "96008",
"make": "Honda",
"accidents": [
{ "year": "2015",
"cost": 5000
},
{"year": "2016",
"cost": 8000
},
{"year": "2016",
"cost": 6000
}
]
}
), values ("c124",
{"policyno": "C124",
"state": "CA",
"zipcode": "96853",
"make": "Ford",
"accidents": [
{ "year": "2015",
"cost": 5000
},
{"year": "2015",
"cost": 8000
},
{"year": "2016",
"cost": 6000
}
]
}
), values ("a123",
{"policyno": "A123",
"state": "AZ",
"zipcode": "86008",
"make": "Honda",
"accidents": [
{ "year": "2015",
"cost": 5000
},
{"year": "2016",
"cost": 8000
},
{"year": "2016",
"cost": 6000
}
]
}
), values ("a124",
{"policyno": "A124",
"state": "AZ",
"zipcode": "86853",
"make": "Ford",
"accidents": [
{ "year": "2015",
"cost": 5000
},
{"year": "2016",
"cost": 8000
},
{"year": "2016",
"cost": 6000
}
]
}
), values ("u123",
{"policyno": "U123",
"state": "UT",
"zipcode": "66008",
"make": "Honda",
"accidents": [
{ "year": "2015",
"cost": 5000
},
{"year": "2016",
"cost": 8000
},
{"year": "2016",
"cost": 6000
}
]
}
), values ("u124",
{"policyno": "U124",
"state": "UT",
"zipcode": "66853",
"make": "Ford",
"accidents": [ ]
}
), values ("u125",
{"policyno": "U125",
"state": "UT",
"zipcode": "66853",
"make": "Ford"
}
);
{noformat}
Define "risk" as the number of accidents divided by the number of policies.
Find the five states that had the most risk in 2016.
{noformat}
from policies
group by state group as g
select state,
( from g
select sum(
(from g.accidents a
where a.year = "2016"
select count )[0]
)[0] / count
) as risk
order by risk desc
limit 5;
{noformat}
This query runs into a NPE.
Expected:
"results": [
{ "state": "AZ",
"risk": 2.0 },
{ "state": "CA",
"risk": 1.5 },
{ "state": "UT",
"risk": 0.6666666666666666
}
]
Attachments
For Gerrit Dashboard: MB-22837 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
82019,6 | [MB-22837] Add tests for complex group-by queries. | master | asterix-opt | Status: MERGED | +2 | +1 |