为什么在 Cosmos 中 DISTINCT 不接受 WHERE 条件?

huangapple go评论58阅读模式
英文:

Why distinct is not taking WHERE condition in Cosmos?

问题

查询中为什么不考虑WHERE子句?我获取到了所有记录,但我只想获取满足WHERE条件的记录。

期望结果:

{
  "id": "b3d7768e-7a22-4109-9511-cdd17585267b",
  "createdDateTime": "2023-06-01T21:02:34.4800722+00:00",
  "earliestPaymentDate": "2023-05-31T07:00:00+00:00",
  "stockNumber": "123",
  "lineItems": [
    {
      "itemUpdateDateTime": "2023-06-01T21:07:26.2846163+00:00",
      "location": "SomeLocation",
      "operationId": 1400742
    }
  ]
}

实际结果:

[
  {
    "id": "b3d7768e-7a22-4109-9511-cdd17585267b",
    "createdDateTime": "2023-06-01T21:02:34.4800722+00:00",
    "earliestPaymentDate": "2023-05-31T07:00:00+00:00",
    "stockNumber": "123",
    "lineItems": [
      {
        "itemUpdateDateTime": "2023-06-01T21:07:26.2846163+00:00",
        "location": "SomeLocation",
        "operationId": 1400742
      },
      {
        "itemUpdateDateTime": "2023-06-01T21:07:26.28473+00:00",
        "location": "SomeLocation",
        "operationId": 1400741
      },
      {
        "itemUpdateDateTime": "2023-06-01T21:07:26.284864+00:00",
        "location": "SomeLocation",
        "operationId": 1400741
      },
      {
        "itemUpdateDateTime": "2023-06-01T21:07:26.284906+00:00",
        "location": null,
        "operationId": null
      },
      {
        "itemUpdateDateTime": "2023-06-01T21:07:26.2849904+00:00",
        "location": null,
        "operationId": null
      }
    ]
  }
]
英文:

Why distinct is not taking considering WHERE clause? I am getting all records and I want only those which satisfies WHERE conditon.

Query:

SELECT distinct c.id, c.stockNumber, c.createdDateTime, c.lineItems 
FROM c
JOIN t IN c.lineItems
WHERE c.stockNumber in ('123')
and t.operationId = 1400742

Expected Result:

{
  "id": "b3d7768e-7a22-4109-9511-cdd17585267b",
  "createdDateTime": "2023-06-01T21:02:34.4800722+00:00",
  "earliestPaymentDate": "2023-05-31T07:00:00+00:00",
  "stockNumber": "123",
  "lineItems": [
    {
      "itemUpdateDateTime": "2023-06-01T21:07:26.2846163+00:00",
      "location": "SomeLocation",
      "operationId": 1400742
    }
  ]
}

Actual Result:

[
   {
  "id": "b3d7768e-7a22-4109-9511-cdd17585267b",
  "createdDateTime": "2023-06-01T21:02:34.4800722+00:00",
  "earliestPaymentDate": "2023-05-31T07:00:00+00:00",
  "stockNumber": "123",
  "lineItems": [
    {
      "itemUpdateDateTime": "2023-06-01T21:07:26.2846163+00:00",
      "location": "SomeLocation",
      "operationId": 1400742
    },
    {
      "itemUpdateDateTime": "2023-06-01T21:07:26.28473+00:00",
      "location": "SomeLocation",
      "operationId": 1400741
    },
    {
      "itemUpdateDateTime": "2023-06-01T21:07:26.284864+00:00",
      "location": "SomeLocation",
      "operationId": 1400741
    },
    {
      "itemUpdateDateTime": "2023-06-01T21:07:26.284906+00:00",
      "location": null,
      "operationId": null
    },
    {
      "itemUpdateDateTime": "2023-06-01T21:07:26.2849904+00:00",
      "location": null,
      "operationId": null
    }
  ]
}]

答案1

得分: 1

The error is not because of the distinct keyword. The code you provided selects the field c.lineitem and filter is on t.operationId. That is the reason you are getting all the data from lineitems array. Below is the corrected code.

Code:

SELECT distinct c.id, c.stockNumber, c.createdDateTime,
{ "operationId": t.operationId, "location": t.location, "itemUpdateDateTime": t.itemUpdateDateTime } as lineItems
FROM c
JOIN t IN c.lineItems
WHERE c.stockNumber in ('123')
and t.operationId = 1400742

The above code selects the id, stockNumber, and createdDateTime fields from the root document c, and creates a new lineItems object that includes only the operationId, location, and itemUpdateDateTime fields from the lineItems array using the JOIN operator.

为什么在 Cosmos 中 DISTINCT 不接受 WHERE 条件?

Updated code:

SELECT DISTINCT c.id, c.stockNumber, c.createdDateTime, 
t as lineItems FROM c JOIN t IN c.lineItems
WHERE c.stockNumber in ('123') and t.operationId = 1400742
英文:

The error is not because of the distinct keyword. The code you provided selects the field c.lineitem and filter is on t.operationId. That is the reason you are getting all the data from lineitems array. Below is the corrected code.

Code:

SELECT  distinct c.id, c.stockNumber, c.createdDateTime,
{"operationId":t.operationId,"location":t.location,"itemUpdateDateTime": t.itemUpdateDateTime} as lineItems
FROM c
JOIN t IN c.lineItems
WHERE c.stockNumber in ('123')
and t.operationId =  1400742

The above code selects the id, stockNumber, and createdDateTime fields from the root document c, and creates a new lineItems object that includes only the operationId, location, and itemUpdateDateTime fields from the lineItems array using the JOIN operator.

为什么在 Cosmos 中 DISTINCT 不接受 WHERE 条件?

Updated code:

SELECT DISTINCT c.id, c.stockNumber, c.createdDateTime, 
t as lineItems FROM c JOIN t IN c.lineItems
WHERE c.stockNumber in ('123') and t.operationId = 1400742

huangapple
  • 本文由 发表于 2023年6月6日 05:06:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定