English 中文(简体)
如何在服务器成果中安排初等生素收集
原标题:How to arrange JSON collections within SQL Server result

我曾试图从Akk服务器查询中收回以下结果:

{
  "data":[
    {"id":3,"type":1,"job":1},
    {"id":4,"type":2,"job":34},
  ],
  "collections": {
    "jobs":[
      {"key":1,"label":"Fenchurch"},
      {"key":34,"label":"Raggle"}
    ],
    "users":[
      {"id":5,"label":"Bob"},
      {"id":20,"label":"Jeff"}
    ]
  }
}

我的最近两件尝试使我感到::

{
  "data":[
    {"id":3,"type":1,"job":1},
    {"id":4,"type":2,"job":34},
  ],
  "collections":[{
    "jobs":[
      {"key":1,"label":"Fenchurch"},
      {"key":34,"label":"Raggle"}
    ],
    "users":[
      {"id":5,"label":"Bob"},
      {"id":20,"label":"Jeff"}
    ]
  }]
}

通知 Collections”:[{和封闭>> 仅应为 Collections>:{<><>>>

我用以下文件来说明这一结果:

SELECT 
(
    SELECT
    data = (
        SELECT TOP 2 * 
        FROM Event 
        FOR JSON PATH
    ),
    (
    SELECT 
        (
            SELECT TOP 2 id AS  key , job_number AS  label  
            FROM [Job] 
            ORDER BY job_number DESC 
            FOR JSON PATH
        ) AS  jobs ,
        (
            SELECT TOP 2 id, full_name AS  label  
            FROM [User] 
            ORDER BY full_name 
            FOR JSON PATH
        ) AS  users 
        FOR JSON PATH
    ) AS  collections 
    FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) AS  result 

我的另一项最接近的尝试是利用CONCAT_WS(CONCAT)给我如下:

{
  "data":[
    {"id":3,"type":1,"job":1},
    {"id":4,"type":2,"job":34},
  ],
  "collections":"
  {
    "jobs":[
      {"key":1,"label":"Fenchurch"},
      {"key":34,"label":"Raggle"}
    ]
  },
  {
    "users":[
      {"id":5,"label":"Bob"},
      {"id":20,"label":"Jeff"}
    ]
  }"
}

Which enclosed "jobs" and "users" in individual { } rather both being within one set.

实现这一目标的基点如下:

SELECT
(
    SELECT
    data = (
        SELECT TOP 2 * 
        FROM Event 
        FOR JSON PATH
    ),
    (
        SELECT CONCAT_WS( , ,
            (
                SELECT TOP 2 id AS  key , job_number AS  label  
                FROM [Job] 
                ORDER BY job_number DESC 
                FOR JSON PATH, ROOT( jobs )
            ),
            (
                SELECT TOP 2 id, full_name AS  label  
                FROM [User] 
                ORDER BY full_name 
                FOR JSON PATH, ROOT( users )
            )
        )
    ) AS  collections  FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) AS  result 

我开始考虑把这个问题分成多个问题,然后建造我自己的“智者”物体,但我很想到的是,我需要社区帮助。

非常赞赏这方面的帮助。

事先感谢你。

最佳回答

页: 1 参考文献目录:ARRAY_WRAPPER to Collections subquery,但为了防止双重计算(because , 参考文献目录:ARRAY_WRAPPER

http://europa-eu-un.org 允许你指明一条宽松的道路,如果你有多种微量价值,希望把这些价值合并为一个单一的nes子的话,这条道路运转良好。

SELECT result = (
    SELECT
      data = (
        SELECT TOP 2 * 
        FROM Event 
        FOR JSON PATH
      ),
      (
        SELECT TOP 2
          j.id AS key,
          j.job_number AS label
        FROM Job j
        ORDER BY
          j.job_number DESC 
        FOR JSON PATH
      ) AS [collections.jobs],
      (
        SELECT TOP 2
          u.id,
          u.full_name AS label
        FROM User u
        ORDER BY
          u.full_name 
        FOR JSON PATH
      ) AS [collections.users]
    FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
);
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...