UNNEST 数组上的 N1ql SUM

N1ql SUM on UNNEST Array

UNNEST 数组上的 N1ql SUM

我有一个存储桶(Couchbase 社区版 6.5),其中包含以下文件:

钓鱼文档

{
    "boatIds": ["boatId_1","boatId_2","boatId_3"],
    "areaIds": ["areaId_1","areaId_2","areaId_3"],
    "total": 10,
    "date": "2021-05-13T00:00:00Z",
    "type": "fishing"
},
{
    "boatIds": ["boatId_1","boatId_3"],
    "areaIds": ["areaId_2","areaId_3"],
    "total": 25,
    "date": "2021-05-15T00:00:00Z",
    "type": "fishing"
}

RiverDoc

{
    "_id": "areaId_1",
    "size": 5,
    "type": "river"
},
{
    "_id": "areaId_1",
    "size": 10,
    "type": "river"
},
{
    "_id": "areaId_1",
    "size": 15,
    "type": "river"
}

BoatDoc

{
    "_id": "areaId_1",
    "name": "Small Boat",
    "type": "boat"
},
{
    "_id": "areaId_1",
    "name": "Medium Boat",
    "type": "boat"
},
{
    "_id": "areaId_1",
    "name": "Large Boat",
    "type": "boat"
}

我需要一个查询,我可以在其中获取按河流和按船分解的所有钓鱼文档。我在以下查询中使用 UNNEST 运算符使它工作:

SELECT river.size,
        boat.name,
        fishing.total
FROM bucket_name fishing
UNNEST fishing.riverIds AS river
UNNEST fishing.boatIds AS boat
WHERE fishing.type = "fishing"

但是这个查询中的问题是上面查询中的合计值是整个钓鱼对象的合计。

我需要得到相对于未嵌套河流大小的总数。所以我需要加入并对钓鱼对象的所有河流的总数求和,并得到特定河流相对于总数的大小。

这是我想到的 select 语句,但我不知道如何实际编写正确的查询:

SELECT river.size,
        boat.name,
        river.size/SUM( fishing.riverIds[0].size, fishing.riverIds[1].size, fishing.riverIds[2].size ) * fishing.total
FROM bucket_name fishing
UNNEST fishing.riverIds AS river
UNNEST fishing.boatIds AS boat
WHERE fishing.type = "fishing"
INSERT INTO default VALUES ("f01", { "boatIds": ["boatId_1","boatId_2","boatId_3"], "areaIds": ["areaId_1","areaId_2","areaId_3"], "total": 10, "date": "2021-05-13T00:00:00Z", "type": "fishing" });
INSERT INTO default VALUES ("f02", { "boatIds": ["boatId_1","boatId_3"], "areaIds": ["areaId_2","areaId_3"], "total": 25, "date": "2021-05-15T00:00:00Z", "type": "fishing" });
INSERT INTO default VALUES ("areaId_1", { "_id": "areaId_1", "size": 5, "type": "river" });
INSERT INTO default VALUES ("areaId_2", { "_id": "areaId_2", "size": 10, "type": "river" });
INSERT INTO default VALUES ("areaId_3", { "_id": "areaId_3", "size": 15, "type": "river" });
INSERT INTO default VALUES ("boatId_1", { "_id": "boatId_1", "name": "Small Boat", "type": "boat" });
INSERT INTO default VALUES ("boatId_2", { "_id": "boatId_2", "name": "Medium Boat", "type": "boat" });
INSERT INTO default VALUES ("boatId_3", { "_id": "boatId_3", "name": "Large Boat", "type": "boat" });

SELECT ARRAY {"size": f.river.[v], "name": f.boat.[f.boatIds[pos]], "total": f.total*f.river.[v]/ARRAY_SUM(OBJECT_VALUES(f.river))}
       FOR pos:v IN f.areaIds END AS distribution
FROM (SELECT d.*,
          OBJECT v._id:v.size FOR v IN (SELECT r._id, r.size FROM default AS r USE KEYS d.areaIds) END AS river,
          OBJECT v._id:v.name FOR v IN (SELECT b._id, b.name FROM default AS b USE KEYS d.boatIds) END AS boat
      FROM default AS d
      WHERE d.type = "fishing") AS f;
{
    "requestID": "fbe127b4-2ebb-4b01-a8a1-0bfe5310ed42",
    "signature": {
        "distribution": "array"
    },
    "results": [
    {
        "distribution": [
            {
                "name": "Small Boat",
                "size": 5,
                "total": 1.6666666666666667
            },
            {
                "name": "Medium Boat",
                "size": 10,
                "total": 3.3333333333333335
            },
            {
                "name": "Large Boat",
                "size": 15,
                "total": 5
            }
        ]
    },
    {
        "distribution": [
            {
                "name": "Small Boat",
                "size": 10,
                "total": 10
            },
            {
                "name": "Large Boat",
                "size": 15,
                "total": 15
            }
        ]
    }
    ],
    "status": "success",
    "metrics": {
        "elapsedTime": "6.946602ms",
        "executionTime": "6.881065ms",
        "resultCount": 2,
        "resultSize": 730,
        "serviceLoad": 2
    }
}

SELECT river AS SectionSize,
       f.total*(river/ARRAY_SUM(f.sections)) AS total,
       f.boats[UNNEST_POS(river)] AS name
FROM (SELECT d.total,
             (SELECT RAW r.size FROM default AS r USE KEYS d.areaIds) AS sections,
             (SELECT RAW b.name FROM default AS b USE KEYS d.boatIds) AS boats
      FROM default AS d
      WHERE d.type = "fishing") AS f
UNNEST f.sections AS river;

{
"results": [
    {
        "SectionSize": 5,
        "name": "Small Boat",
        "total": 1.6666666666666665
    },
    {
        "SectionSize": 10,
        "name": "Medium Boat",
        "total": 3.333333333333333
    },
    {
        "SectionSize": 15,
        "name": "Large Boat",
        "total": 5
    },
    {
        "SectionSize": 10,
        "name": "Small Boat",
        "total": 10
    },
    {
        "SectionSize": 15,
        "name": "Large Boat",
        "total": 15
    }
    ]
}