Twinkle HubTwinkle Hub

tw_query_dataset

tw_query_dataset

關鍵字 / Keywords: 查詢 篩選 過濾 聚合 統計 加總 計數 分組 排序 資料列 query filter aggregate group count sum rows dataset

用結構化條件查資料集的列(不寫 SQL)。v3 的資料查詢入口(#367)。

第一次查某個資料集前,先用 tw_get_dataset 看它的欄位名(metadata.schema.columns): select / filters / aggregate / group_by / order_by 的 field 都要照欄位原文。 metadata 是 None(資料集還沒準備好)時,直接查;欄位不對的錯誤會列出可用欄位(經 execute_tool 呼叫時在 data.result.error.details.allowed_columns)。

參數: dataset_id: 資料集 ID(由 tw_search_datasets 取得) select: 要回的欄位名(照資料集表頭原文,例 ["agency", "award_price"]) filters: 條件清單,每個 {"field", "operator", "value"}。 operator:eq / ne / gt / gte / lt / lte / in(value 是清單)/ between(value 是 [下限, 上限])/ contains(包含字串) 例:[{"field": "agency", "operator": "contains", "value": "國防部"}, {"field": "date", "operator": "gte", "value": "2024-01-01"}] aggregate: 聚合清單,每個 {"function", "field", "alias", "as"};function:count / sum / avg / min / max (count 可用 field="")。例:[{"function": "count", "field": "", "alias": "件數"}] min / max 預設當數字比;日期或文字(最新公告日、最早成交日)要寫 "as": "text" (資料集標明的日期欄,例 pcc-tender 的 date、lvr-* 的 iso_trade_date,沒寫也會當文字比) 當數字用時(sum / avg / min / max、數值篩選、"as": "number" 排序),純數字與標準千分位 (1,234,567)都認;其他值(帶單位、符號、文字)不算數字 —— sum / avg 有這種列時 _meta.aggregate_hints 會說有幾列沒算進去 group_by: 分組欄位(配合 aggregate) order_by: 排序,每個 {"field": 欄位或 alias, "direction": "asc" | "desc", "as": "text" | "number"}。 欄位值都是文字:排原始欄位(含 group_by 的分組欄)一律要寫 "as" —— 金額、人數等數值給 "number";零補位、固定寬度的日期(2024-09-09、1130909)或文字給 "text";不寫會被拒(不扣點)。 aggregate 的 alias 不用寫(count / sum / avg 是數字;min / max 照它的 as) limit: 最多回幾列(1–500;部分方案有更低上限) predefined / params: 資料集若提供預定查詢,用名稱與參數呼叫

回傳 {dataset_id, columns, rows, row_count_returned, limit, truncated}(能判斷資料期別時另附 data_period: latest = 這份資料的最新一期)。truncated=true 代表還有更多列 —— 加條件縮小範圍。欄位名不存在時錯誤會列出可用欄位(經 execute_tool 呼叫時在 data.result.error.details.allowed_columns)。

Parameters

ParamTypeDescription
dataset_id *string—
selectarray—
filtersarray—
aggregatearray—
group_byarray—
order_byarray—
limitinteger—
predefinedstring—
paramsobject—
Raw JSON schema
{
  "properties": {
    "dataset_id": {
      "title": "Dataset Id",
      "type": "string"
    },
    "select": {
      "anyOf": [
        {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Select"
    },
    "filters": {
      "anyOf": [
        {
          "items": {
            "additionalProperties": true,
            "type": "object"
          },
          "type": "array"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Filters"
    },
    "aggregate": {
      "anyOf": [
        {
          "items": {
            "additionalProperties": true,
            "type": "object"
          },
          "type": "array"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Aggregate"
    },
    "group_by": {
      "anyOf": [
        {
          "items": {
            "type": "string"
          },
          "type": "array"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Group By"
    },
    "order_by": {
      "anyOf": [
        {
          "items": {
            "additionalProperties": true,
            "type": "object"
          },
          "type": "array"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Order By"
    },
    "limit": {
      "default": 100,
      "title": "Limit",
      "type": "integer"
    },
    "predefined": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Predefined"
    },
    "params": {
      "anyOf": [
        {
          "additionalProperties": true,
          "type": "object"
        },
        {
          "type": "null"
        }
      ],
      "default": null,
      "title": "Params"
    }
  },
  "required": [
    "dataset_id"
  ],
  "title": "tw_query_datasetArguments",
  "type": "object",
  "additionalProperties": false
}

How to call

Starter — params in the table above
tw_query_dataset({ dataset_id: "…" })