I have table created with a script
CREATE TABLE [dbo].[Queries]
(
[Id] [INT] NOT NULL,
[Name] [NVARCHAR](MAX) NOT NULL,
[Module] [NCHAR](64) NOT NULL,
[Query] [NVARCHAR](MAX) NOT NULL,
PRIMARY KEY (Id)
)
GO
where Query column is JSON string like:
[
{
"FieldName": "TargetFieldName",
"Filters": [
{ /* Description Filter 1 */ },
...
{ /* Description Filter N */ }
]
}
]
And I'd like to select from that table: Id, Name, and distinct list of column names as json array in THIRD column.
For example for row:
-------------------------------------------------------------------------------------------------
| Id | Name | Query |
-------------------------------------------------------------------------------------------------
| 7 | Query 7 | [{"FieldName": "A", ... },{ "FieldName": "B" ...},{"FieldName": "B", ... }] |
-------------------------------------------------------------------------------------------------
I'd like to get
| Id | Name | DistinctFieldNames |
+----+---------+--------------------+
| 7 | Query 7 | ["A","B"] |
My questions are:
- Is it possible at all transform json data to other format json data at SQL Server side?
- How to write a SQL query to select one field from json array of objects?