Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON到SQL Server_Json_Sql Server_Ssis - Fatal编程技术网

JSON到SQL Server

JSON到SQL Server,json,sql-server,ssis,Json,Sql Server,Ssis,我可以访问保存在SQL server数据库中的JSON数据。我想创建一个用于报告的数据模型 下面是我的数据现在的样子: +------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+ | Order_Id |

我可以访问保存在SQL server数据库中的JSON数据。我想创建一个用于报告的数据模型

下面是我的数据现在的样子:

+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
|  Order_Id  |                                                                       JSON_Detail                                                                        |
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Order_1001 | {"Customer":"Mario","BillingNumber":"99","List_ABC_0":"475","List_ABC_1":"461","List_ABC_2":"940","List_Type_0":"N","List_Type_1":"D","List_Type_2":"N"} |
| Order_1002 | {"Customer":"Luigi","BillingNumber":"61","List_ABC_0":"182","List_Type_0":"N"}                                                                           |
| Order_1003 | {"Customer":"Toad","BillingNumber":"03","List_ABC_0":"028","List_ABC_1":"283","List_Type_0":"D","List_Type_1":"D"}                                       |
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
使用SSIS,我可以将其分解为一个带有JSON值字段的平面表。 我得到的结果是:

+------------+----------+---------------+------------+------------+------------+-------------+-------------+-------------+
|  Order_Id  | Customer | BillingNumber | List_ABC_0 | List_ABC_1 | List_ABC_2 | List_Type_0 | List_Type_1 | List_Type_2 |
+------------+----------+---------------+------------+------------+------------+-------------+-------------+-------------+
| Order_1001 | Mario    |            99 |        475 |        461 |        940 | N           | D           | N           |
| Order_1002 | Luigi    |            61 |        182 |            |            | N           |             |             |
| Order_1003 | Toad     |            03 |        028 |        283 |            | D           | D           |             |
+------------+----------+---------------+------------+------------+------------+-------------+-------------+-------------+
现在是我的问题,注意列表字段(ABC和类型)。在我的示例中,我将其上限设置为3,但我的真实数据可以有更多。
我想将每个列表类型合并到一个合并字段中。我期望的最终结果如下:

*我添加了一个序列字段,对应于n

+------------+-----------+----------+---------------+-----+------+
|  Order_Id  | Sequence  | Customer | BillingNumber | ABC | Type |
+------------+-----------+----------+---------------+-----+------+
| Order_1001 |         0 | Mario    |            99 | 475 | N    |
| Order_1001 |         1 | Mario    |            99 | 461 | D    |
| Order_1001 |         2 | Mario    |            99 | 940 | N    |
| Order_1002 |         0 | Luigi    |            61 | 182 | N    |
| Order_1003 |         0 | Toad     |            03 | 028 | D    |
| Order_1003 |         1 | Toad     |            03 | 283 | D    |
+------------+-----------+----------+---------------+-----+------+
如何从当前状态转到所需输出

SSI和SQL Server是我可以访问的工具。
我在服务器上有写权限(创建/更改表、视图、函数、存储过程等)。

试试这个

SELECT order_id, customer, billingnumber, abc, [type], 
       Row_number() OVER (partition BY order_id ORDER BY order_id) - 1 AS [Sequence] 
FROM   tablename 
       CROSS apply (SELECT list_abc_0, list_type_0 UNION ALL 
                    SELECT list_abc_1, list_type_1 UNION ALL 
                    SELECT list_abc_2, list_type_2) Crs (abc, [Type]) 
WHERE  abc IS NOT NULL AND type IS NOT NULL; 
要从旧表创建新表

Select * into new_table  from (
SELECT order_id, customer, billingnumber, abc, [type], 
       Row_number() OVER (partition BY order_id ORDER BY order_id) - 1 AS [Sequence] 
FROM   tablename 
       CROSS apply (SELECT list_abc_0, list_type_0 UNION ALL 
                    SELECT list_abc_1, list_type_1 UNION ALL 
                    SELECT list_abc_2, list_type_2) Crs (abc, [Type]) 
WHERE  abc IS NOT NULL AND type IS NOT NULL) tmp
输出

+-------------+----------+---------------+-----+------+----------+
|  order_id   | customer | billingnumber | abc | type | Sequence |
+-------------+----------+---------------+-----+------+----------+
| Order_1001  | Mario    |            99 | 475 | N    |        0 |
| Order_1001  | Mario    |            99 | 461 | D    |        1 |
| Order_1001  | Mario    |            99 | 940 | N    |        2 |
| Order_1002  | Luigi    |            61 | 182 | N    |        0 |
| Order_1003  | Toad     |            03 | 028 | D    |        0 |
| Order_1003  | Toad     |            03 | 283 | D    |        1 |
+-------------+----------+---------------+-----+------+----------+
在线演示: