I have 1000 documents of type below and I want to design a document in cloudant (write a view) equivalent to this Mysql query:
(SELECT
COUNT(DISTINCT(correlationassetid)), PERIOD
FROM
view_asset
WHERE
((PERIOD >= '201705') AND (PERIOD <= '201705'))
GROUP BY
PERIOD
ORDER BY
PERIOD ASC)
I have tried below view but it does not give proper result. could any one help me?
function (doc) {
if(doc.type == "asset"){
emit(doc.period,1);
}
{
"_id": "24dee0ec910e22605fe8fc4189000c56",
"_rev": "1-d667f0b4ce3d984c0d7aafadd223674a",
"parentName": "",
"period": "201701",
"providerGlobalAssetId": "",
"cost": "5",
"providerRegionCode": "",
"owner": "",
"snapshotId": "659c5f7a-35d62",
"correlationAssetId": "aws-6082634880291"
}
I'm a bit confused by your example query and your sample document because there are some inconsistencies, but I am going to try to answer your question based on a couple assumptions.
If you are filtering by a single PERIOD value no GROUPBY clause should be required in your SQL statement:
Defining the following design document
a
GET
request to query the viewshould return the desired result if you specify
start_key=["201705"]
andend_key=["201705",{}]
.Example response if there are two documents within that period, both containing the same
correlationAssetId
:The number of rows in the result set should identify the distinct number of
correlationAssetId
s for the specified PERIOD.Example result for two
correlationAssetId
s:P.S. Your example document didn't define the
asset
property and your view definition would therefore not have returned the document. My response above assumes that that property is defined in the documents.