Related
I have this table which is very simple with this data
CREATE TABLE #Prices
(
ProductId int,
SizeId int,
Price int,
Date date
)
INSERT INTO #Prices
VALUES (1, 1, 100, '2020-01-01'),
(1, 1, 120, '2020-02-01'),
(1, 1, 130, '2020-03-01'),
(1, 2, 100, '2020-01-01'),
(1, 2, 100, '2020-02-01'),
(2, 1, 100, '2020-01-01'),
(2, 1, 120, '2020-02-01'),
(2, 1, 130, '2020-03-01'),
(2, 2, 100, '2020-01-01'),
(2, 2, 100, '2020-02-01')
I would like to format the output to be something like this:
{
"Products": [
{
"Product": 2,
"UnitSizes": [
{
"SizeId": 1,
"PerDate": [
{
"Date": "2020-01-02",
"Price": 870.0
},
{
"Date": "2021-04-29",
"Price": 900.0
}
]
},
{
"SizeId": 2,
"PerDate": [
{
"Date": "2020-01-02",
"Price": 435.0
},
{
"Date": "2021-04-29",
"Price": 450.0
}
]
}
]
},
{
"Product": 4,
"UnitSizes": [
{
"SizeId": 1,
"PerDate": [
{
"Date": "2020-01-02",
"Price": 900.0
}
]
}
]
}
]
}
I almost have it but I don't know how to format to get the array inside 'PerDate'. This is what I have
SELECT
ProductId AS [Product],
SizeId AS 'Sizes.SizeId',
date AS 'Sizes.PerDate.Date',
price AS 'Sizes.PerDate.Price'
FROM
#Prices
ORDER BY
ProductId, [Sizes.SizeId], Date
FOR JSON PATH, ROOT('Products')
I have tried with FOR JSON AUTO and nothing, I've tried with JSON_QUERY() but I was not able to achieve the result I want.
Every help will be very appreciated.
Thanks
Unfortunately, SQL Server does not have the JSON_AGG function, which means you would normally need to use a number of correlated subqueries and keep on rescanning the base table.
However, we can simulate it by using STRING_AGG against single JSON objects generated in an APPLY. This means that we only scan the base table once.
Use of JSON_QUERY with no path prevents double-escaping
WITH PerDate AS (
SELECT
p.ProductId,
p.SizeId,
PerDate = '[' + STRING_AGG(j.PerDate, ',') WITHIN GROUP (ORDER BY p.Date) + ']'
FROM #Prices AS p
CROSS APPLY ( -- This produces multiple rows of single JSON objects
SELECT p.Date, p.Price
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) j(PerDate)
GROUP BY
p.ProductId,
p.SizeId
),
UnitSizes AS (
SELECT
p.ProductId,
UnitSizes = '[' + STRING_AGG(j.UnitSizes, ',') WITHIN GROUP (ORDER BY p.SizeId) + ']'
FROM PerDate p
CROSS APPLY (
SELECT p.SizeId, PerDate = JSON_QUERY(p.PerDate)
FOR JSON PATH, WITHOUT_ARRAY_WRAPPER
) j(UnitSizes)
GROUP BY
p.ProductId
)
SELECT
Product = p.ProductId,
UnitSizes = JSON_QUERY(p.UnitSizes)
FROM UnitSizes p
ORDER BY p.ProductId
FOR JSON PATH, ROOT('Products');
db<>fiddle
This is one way of doing it
DROP TABLE IF EXISTS #Prices
CREATE TABLE #Prices
(
ProductId INT,
SizeId INT,
Price INT,
Date DATE
)
-- SQL Prompt formatting off
INSERT INTO #Prices
VALUES (1, 1, 100, '2020-01-01'),
(1, 1, 120, '2020-02-01'),
(1, 1, 130, '2020-03-01'),
(1, 2, 100, '2020-01-01'),
(1, 2, 100, '2020-02-01'),
(2, 1, 100, '2020-01-01'),
(2, 1, 120, '2020-02-01'),
(2, 1, 130, '2020-03-01'),
(2, 2, 100, '2020-01-01'),
(2, 2, 100, '2020-02-01')
-- SQL Prompt formatting on
SELECT m.ProductId AS Product,
(
SELECT s.SizeId,
(
SELECT p.Date,
p.Price
FROM #Prices AS p
WHERE p.SizeId = s.SizeId
GROUP BY p.Date,
p.Price
ORDER BY p.Date
FOR JSON PATH
) AS PerDate
FROM #Prices AS s
WHERE s.ProductId = m.ProductId
GROUP BY s.SizeId
ORDER BY s.SizeId
FOR JSON PATH
) AS UnitSizes
FROM #Prices AS m
GROUP BY m.ProductId
ORDER BY m.ProductId
FOR JSON PATH, ROOT('Products')
Output:
{
"Products":
[
{
"Product": 1,
"UnitSizes":
[
{
"SizeId": 1,
"PerDate":
[
{
"Date": "2020-01-01",
"Price": 100
},
{
"Date": "2020-02-01",
"Price": 120
},
{
"Date": "2020-03-01",
"Price": 130
}
]
},
{
"SizeId": 2,
"PerDate":
[
{
"Date": "2020-01-01",
"Price": 100
},
{
"Date": "2020-02-01",
"Price": 100
}
]
}
]
},
{
"Product": 2,
"UnitSizes":
[
{
"SizeId": 1,
"PerDate":
[
{
"Date": "2020-01-01",
"Price": 100
},
{
"Date": "2020-02-01",
"Price": 120
},
{
"Date": "2020-03-01",
"Price": 130
}
]
},
{
"SizeId": 2,
"PerDate":
[
{
"Date": "2020-01-01",
"Price": 100
},
{
"Date": "2020-02-01",
"Price": 100
}
]
}
]
}
]
}
can anyone offer a clue on how to do query values within arrays -- such as below, I want to find all records where
DiscoveredInformationTypes_s Confidence > 80
Can anyone help? How do I query inside this array?
MachineName_s
Version_s
ProcessName_s
ApplicationName_s
Operation_s
ObjectId_s
DiscoveredInformationTypes_s
[ { "Confidence": 55, "Count": 1, "SensitiveType": "3356946c-6bb7-449b-b253-6ffa419c0ce7", "UniqueCount": 1, "SensitiveInformationDetections": null, "Name": "International Classification of Diseases (ICD-10-CM)" } ]
you can use mv-apply: https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/mv-applyoperator
for example:
datatable(DiscoveredInformationTypes_s:dynamic)
[
dynamic([ { "Confidence": 55, "Count": 1, "SensitiveType": "3356946c-6bb7-449b-b253-6ffa419c0ce7", "UniqueCount": 1, "SensitiveInformationDetections": null, "Name": "International Classification of Diseases (ICD-10-CM)" } ]),
dynamic([ { "Confidence": 81, "Count": 1, "SensitiveType": "3356946c-6bb7-449b-b253-6ffa419c0ce7", "UniqueCount": 1, "SensitiveInformationDetections": null, "Name": "International Classification of Diseases (ICD-10-CM)" } ])
]
| mv-apply DiscoveredInformationTypes_s on (
where DiscoveredInformationTypes_s.Confidence > 80
)
or, if your column is string-typed and not dynamic-typed, you'll need to invoke parse_json() on it first:
datatable(s:string)
[
'[ { "Confidence": 55, "Count": 1, "SensitiveType": "3356946c-6bb7-449b-b253-6ffa419c0ce7", "UniqueCount": 1, "SensitiveInformationDetections": null, "Name": "International Classification of Diseases (ICD-10-CM)" } ]',
'[ { "Confidence": 81, "Count": 1, "SensitiveType": "3356946c-6bb7-449b-b253-6ffa419c0ce7", "UniqueCount": 1, "SensitiveInformationDetections": null, "Name": "International Classification of Diseases (ICD-10-CM)" } ]'
]
| project DiscoveredInformationTypes_s = parse_json(s)
| mv-apply DiscoveredInformationTypes_s on (
where DiscoveredInformationTypes_s.Confidence > 80
)
I have a Json like this (it is contained in a clob variable):
{"id": "33", "type": "abc", "val": "2", "cod": "", "sg1": "1", "sg2": "1"}
{"id": "359", "type": "abcef", "val": "52", "cod": "aa", "sg1": "", "sg2": "0"}
…
I need to remove " from values of: id, val, sg1, sg2
Is it possibile?
For example, I need to obtain this:
{"id": 33, "type": "abc", "val": 2, "cod": "", "sg1": 1, "sg2": 1}
{"id": 359, "type": "abcef", "val": 52, "cod": "aa", "sg1": , "sg2": 0}
…
If you are using Oracle 12 (R2?) or later then you can convert your JSON to the appropriate data types and then convert it back to JSON.
Oracle 18 Setup:
CREATE TABLE test_data ( value CLOB );
INSERT INTO test_data ( value )
VALUES ( '{"id": "33", "type": "abc", "val": "2", "cod": "", "sg1": "1", "sg2": "1"}' );
INSERT INTO test_data ( value )
VALUES ( '{"id": "359", "type": "abcef", "val": "52", "cod": "aa", "sg1": "", "sg2": "0"}' );
Query:
SELECT JSON_OBJECT(
'id' IS j.id,
'type' IS j.typ,
'val' IS j.val,
'cod' IS j.cod,
'sg1' IS j.sg1,
'sg2' IS j.sg2
) AS JSON
FROM test_data t
CROSS JOIN
JSON_TABLE(
t.value,
'$'
COLUMNS
id NUMBER(5,0) PATH '$.id',
typ VARCHAR2(10) PATH '$.type',
val NUMBER(5,0) PATH '$.val',
cod VARCHAR2(10) PATH '$.cod',
sg1 NUMBER(5,0) PATH '$.sg1',
sg2 NUMBER(5,0) PATH '$.sg2'
) j
Output:
| JSON |
| :--------------------------------------------------------------- |
| {"id":33,"type":"abc","val":2,"cod":null,"sg1":1,"sg2":1} |
| {"id":359,"type":"abcef","val":52,"cod":"aa","sg1":null,"sg2":0} |
Or, if you want to use regular expressions (you shouldn't if you have the choice and should use a proper JSON parser instead) then:
Query 2:
SELECT REGEXP_REPLACE(
REGEXP_REPLACE(
value,
'"(id|val|sg1|sg2)": ""',
'"\1": "null"'
),
'"(id|val|sg1|sg2)": "(\d+|null)"',
'"\1": \2'
) AS JSON
FROM test_data
Output:
| JSON |
| :-------------------------------------------------------------------------- |
| {"id": 33, "type": "abc", "val": 2, "cod": "", "sg1": 1, "sg2": 1} |
| {"id": 359, "type": "abcef", "val": 52, "cod": "aa", "sg1": null, "sg2": 0} |
db<>fiddle here
I want to use the create_coco_tf_record-script provided by tensorflow, to convert the following simple label definition in coco json format to TFRecord:
"info": {
"year": 2018,
"version": null,
"description": "TransferLearningTest",
"contributor": "ralph#r4robotics.com.au",
"url": "labelbox.io",
"date_created": "2018-03-25T08:30:27.427851+00:00"
},
"images": [{
"id": "cjf6gxqjw2fho01619gre5j0y",
"width": 615,
"height": 409,
"file_name": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles1.jpg?alt=media&token=b381c976-da30-49d7-8e95-eb4ae8588354",
"license": null,
"flickr_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles1.jpg?alt=media&token=b381c976-da30-49d7-8e95-eb4ae8588354",
"coco_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles1.jpg?alt=media&token=b381c976-da30-49d7-8e95-eb4ae8588354",
"date_captured": null
}, {
"id": "cjf6gyhtl55sv01385xtqjrqi",
"width": 259,
"height": 194,
"file_name": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles2.jpg?alt=media&token=9b274e2e-c541-4e80-8f3d-b198f3ba9b4d",
"license": null,
"flickr_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles2.jpg?alt=media&token=9b274e2e-c541-4e80-8f3d-b198f3ba9b4d",
"coco_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles2.jpg?alt=media&token=9b274e2e-c541-4e80-8f3d-b198f3ba9b4d",
"date_captured": null
}, {
"id": "cjf6gzj9v2g1h0161bwh18chv",
"width": 277,
"height": 182,
"file_name": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles3.jpg?alt=media&token=3cfc13ca-432d-4501-b574-00d3874a4682",
"license": null,
"flickr_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles3.jpg?alt=media&token=3cfc13ca-432d-4501-b574-00d3874a4682",
"coco_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles3.jpg?alt=media&token=3cfc13ca-432d-4501-b574-00d3874a4682",
"date_captured": null
}, {
"id": "cjf6h0p9n55wz0178pg79lc3c",
"width": 301,
"height": 167,
"file_name": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles4.jpg?alt=media&token=d2660bc4-d576-45f0-8de6-557270fc683d",
"license": null,
"flickr_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles4.jpg?alt=media&token=d2660bc4-d576-45f0-8de6-557270fc683d",
"coco_url": "https://firebasestorage.googleapis.com/v0/b/labelbox-193903.appspot.com/o/cjf6gtsr950sr0125idy65yiy%2Ff245e964-d756-4c01-98de-b6e5a9070588%2Fbottles4.jpg?alt=media&token=d2660bc4-d576-45f0-8de6-557270fc683d",
"date_captured": null
}],
"annotations": [{
"id": 1,
"image_id": "cjf6gxqjw2fho01619gre5j0y",
"category_id": 1,
"segmentation": [
[118.39765618513167, 313.457848898712, 179.7169976455091, 299.1734470204843, 294.6908226914901, 310.3222212573321, 337.1962867729657, 334.7101881586143, 366.4623832276035, 338.89097185223864, 372.03689297966736, 385.5765403887654, 332.31863061175864, 389.75732408238974, 282.84505592143563, 406.48051201843583, 215.9512047089942, 408.5708772844735, 192.2596180064203, 390.4541125044023, 151.8445552101198, 403.6933051688366, 105.1582353958287, 376.51813141795526, 118.39765618513167, 313.457848898712]
],
"area": 22106.876283900496,
"bbox": [105.1582353958287, 0.42912271552648545, 266.8786575838387, 109.39743026398922],
"iscrowd": 0
}, {
"id": 2,
"image_id": "cjf6gxqjw2fho01619gre5j0y",
"category_id": 1,
"segmentation": [
[160.20631983821562, 142.04523900617488, 195.04687288245222, 131.24488556110788, 308.62704390918475, 134.03209241070698, 356.01021731433246, 152.1488571907783, 381.7922053020817, 150.75522718520426, 384.57951334057844, 186.64038911511503, 349.7389071338769, 187.68559832890833, 317.6856089656722, 202.3183678373679, 159.50946624735892, 195.3503772941444, 160.20631983821562, 142.04523900617488]
],
"area": 13123.705213053147,
"bbox": [159.50946624735892, 206.6816321626321, 225.07004709321953, 71.07348227626002],
"iscrowd": 0
}, {
"id": 3,
"image_id": "cjf6gyhtl55sv01385xtqjrqi",
"category_id": 1,
"segmentation": [
[80.06035395893144, 68.18619344603749, 119.11342792196902, 74.69491256085784, 131.84812313721997, 72.14801308536389, 177.97602777539703, 78.09078572494903, 187.59778022105084, 91.67421361042045, 203.1624077063576, 93.37213939731716, 201.18146375358646, 112.04938782407424, 184.76784795099502, 111.200414135477, 169.20322046568833, 122.51994816851872, 128.16920254987957, 117.42614921753086, 114.86852951688535, 114.03029764373744, 93.07803808305403, 114.31328167650393, 70.43857992260781, 103.2767316762287, 80.06035395893144, 68.18619344603749]
],
"area": 4995.907009222967,
"bbox": [70.43857992260781, 71.48005183148128, 132.7238277837498, 54.33375472248123],
"iscrowd": 0
}, {
"id": 4,
"image_id": "cjf6gzj9v2g1h0161bwh18chv",
"category_id": 1,
"segmentation": [
[173.46162883883662, 160.28013107383993, 255.65715601241382, 148.2998138472238, 266.2728180897869, 177.11325728633884, 184.68389092165103, 182.8759506021435, 159.20627416758742, 180.1462513325615, 154.35340470313542, 170.74397441725296, 175.28142885516084, 167.7109803710303, 173.46162883883662, 160.28013107383993]
],
"area": 2509.1082874191734,
"bbox": [154.35340470313542, -0.8759506021434983, 111.91941338665146, 34.576136754919716],
"iscrowd": 0
}, {
"id": 5,
"image_id": "cjf6gzj9v2g1h0161bwh18chv",
"category_id": 1,
"segmentation": [
[37.58112185203197, 87.03332022958155, 45.16373762158412, 93.40262623857566, 94.90570169790779, 106.44448675338808, 106.73458692647054, 87.03332022958155, 46.680260775494574, 73.08155224493905, 40.31086815713212, 74.901344044691, 33.63817553604898, 74.901344044691, 27.875382923127926, 80.9673321371363, 37.58112185203197, 87.03332022958155]
],
"area": 1386.09176276128,
"bbox": [27.875382923127926, 75.55551324661192, 78.85920400334261, 33.36293450844903],
"iscrowd": 0
}, {
"id": 6,
"image_id": "cjf6gzj9v2g1h0161bwh18chv",
"category_id": 1,
"segmentation": [
[200.7590456092244, 136.92608617388885, 181.95412147624396, 120.09295996138994, 234.4258318576678, 85.2135284298296, 255.05055600697247, 103.71478748380605, 200.7590456092244, 136.92608617388885]
],
"area": 1614.301579806095,
"bbox": [181.95412147624396, 45.073913826111145, 73.09643453072852, 51.71255774405926],
"iscrowd": 0
}, {
"id": 7,
"image_id": "cjf6h0p9n55wz0178pg79lc3c",
"category_id": 1,
"segmentation": [
[17.847508506087518, 28.63952607163654, 66.60858665657888, 24.08859036914734, 77.98617155836023, 14.986669362689923, 145.27644948557162, 14.49906202292621, 147.5519565454611, 51.881911126804255, 75.0605019090974, 56.10780833710362, 64.0079859014193, 47.98110201017366, 24.3489855928197, 53.34473314609532, 17.847508506087518, 28.63952607163654]
],
"area": 4189.730491764894,
"bbox": [17.847508506087518, 110.89219166289638, 129.7044480393736, 41.60874631417741],
"iscrowd": 0
}, {
"id": 8,
"image_id": "cjf6h0p9n55wz0178pg79lc3c",
"category_id": 1,
"segmentation": [
[223.94433711573117, 23.27591973645434, 257.10186033759857, 27.82685543894354, 261.32783036444124, 48.306165303102944, 179.73427308501883, 104.86804629868364, 145.27644948557162, 113.3198159185429, 128.37261898053467, 122.42173692500033, 111.46876367433086, 108.76885541531423, 131.29826382863067, 96.09118858515549, 137.14960312715638, 77.56230808005091, 223.94433711573117, 23.27591973645434]
],
"area": 6031.236484118768,
"bbox": [111.46876367433086, 44.57826307499967, 149.85906669011038, 99.14581718854599],
"iscrowd": 0
}, {
"id": 9,
"image_id": "cjf6h0p9n55wz0178pg79lc3c",
"category_id": 1,
"segmentation": [
[26.299423758605975, 125.34733136210352, 40.60267830965016, 111.53193060632253, 117.97024076106304, 72.6862842838929, 133.57379568968702, 80.81299061082292, 132.59856420621048, 93.16559414805232, 111.46876367433086, 115.2702204768582, 64.33305479552251, 138.67514957886033, 46.128923912905776, 139.65033945764822, 23.37375410934314, 148.75226046410563, 8.095292875989244, 141.11316147693933, 26.299423758605975, 125.34733136210352]
],
"area": 3857.6591542480846,
"bbox": [8.095292875989244, 18.24773953589436, 125.47850281369777, 76.06597618021274],
"iscrowd": 0
}],
"licenses": [],
"categories": [{
"supercategory": "Bottle",
"id": 1,
"name": "Bottle"
}]
}
But when I run the script using
with open('coco_labels.json') as json_data:
label_info = json.load(json_data)
IMAGE_FOLDER = "coco_images"
with tf.python_io.TFRecordWriter("training.record") as writer:
for i,image in enumerate(label_info["images"]):
img_data = requests.get(image["file_name"]).content
image_name = "image"+str(i)+".jpg"
image_path = os.path.join(IMAGE_FOLDER,image_name)
with open(image_path, 'wb') as handler:
handler.write(img_data)
image["file_name"] = image_name
tf_example = create_coco_tf_record.create_tf_example(image,
label_info["annotations"][i],
IMAGE_FOLDER,
label_info["categories"]
)
writer.write(tf_example.SerializeToString())
I get the error
(image, annotations_list, image_dir, category_index, include_masks)
124 num_annotations_skipped = 0
125 for object_annotations in annotations_list:
--> 126 (x, y, width, height) = tuple(object_annotations['bbox'])
127 if width <= 0 or height <= 0:
128 num_annotations_skipped += 1
TypeError: string indices must be integers
What could be the problem?
Each image is supposed to receive a list of annotations, and you are providing a single one. Making it a single element list should solve your error.
Ideally, make each item of images in your json be a list itself. As a quick fix, embrace label_info["annotations"][i] in brackets:
[label_info["annotations"][i]]
I have the following table:
CREATE TABLE mytable (
id serial PRIMARY KEY
, employee text UNIQUE NOT NULL
, data jsonb
);
With the following data:
INSERT INTO mytable (employee, data)
VALUES
('Jim', '{"sales_tv": [{"value": 10, "yr": "2010", "loc": "us"}, {"value": 5, "yr": "2011", "loc": "europe"}, {"value": 40, "yr": "2012", "loc": "asia"}], "sales_radio": [{"value": 11, "yr": "2010", "loc": "us"}, {"value": 8, "yr": "2011", "loc": "china"}, {"value": 76, "yr": "2012", "loc": "us"}], "another_key": "another value"}'),
('Rob', '{"sales_radio": [{"value": 7, "yr": "2014", "loc": "japan"}, {"value": 3, "yr": "2009", "loc": "us"}, {"value": 37, "yr": "2011", "loc": "us"}], "sales_tv": [{"value": 4, "yr": "2010", "loc": "us"}, {"value": 18, "yr": "2011", "loc": "europe"}, {"value": 28, "yr": "2012", "loc": "asia"}], "another_key": "another value"}')
Notice that there are other keys in there besides just "sales_tv" and "sales_radio". For the queries below I just need to focus on "sales_tv" and "sales_radio".
I need to find all sales for Jim for 2012. Anything that starts with "sales_" and then put that in an object (just need the what the product sold is and the value). e.g.:
employee | sales_
Jim | {"sales_tv": 40, "sales_radio": 76}
I've got:
SELECT * FROM mytable,
(SELECT l.key, l.value FROM mytable, lateral jsonb_each_text(data) AS l
WHERE key LIKE 'sales_%') AS a,
jsonb_to_recordset(a.value::jsonb) AS d(yr text, value float)
WHERE mytable.employee = 'Jim'
AND d.yr = '2012'
But I can't seem to even get just Jim's data. Instead I get:
employee | key | value
-------- |------ | -----
Jim | sales_tv | [{"yr": "2010", "loc": "us", "value": 4}, {"yr": "2011", "loc": "europe", "value": 18}, {"yr": "2012", "loc": "asia", "value": 28}]
Jim | sales_tv | [{"yr": "2010", "loc": "us", "value": 10}, {"yr": "2011", "loc": "europe", "value": 5}, {"yr": "2012", "loc": "asia", "value": 40}]
Jim | sales_radio | [{"yr": "2010", "loc": "us", "value": 11}, {"yr": "2011", "loc": "china", "value": 8}, {"yr": "2012", "loc": "us", "value": 76}]
You treat the result of the first join as JSON, not as text string, so use jsonb_each() instead of jsonb_each_text():
SELECT t.employee, json_object_agg(a.k, d.value) AS sales
FROM mytable t
JOIN LATERAL jsonb_each(t.data) a(k,v) ON a.k LIKE 'sales_%'
JOIN LATERAL jsonb_to_recordset(a.v) d(yr text, value float) ON d.yr = '2012'
WHERE t.employee = 'Jim' -- works because employee is unique
GROUP BY 1;
GROUP BY 1 is shorthand for GROUP BY t.employee.
Result:
employee | sales
---------+--------
Jim | '{ "sales_tv" : 40, "sales_radio" : 76 }'
I also untangled and simplified your query.
json_object_agg() is instrumental in aggregating name/value pairs as JSON object. Optionally cast to jsonb if you need that - or use jsonb_object_agg() in Postgres 9.5 or later.
Using explicit JOIN syntax to attach conditions in their most obvious place.
The same without explicit JOIN syntax:
SELECT t.employee, json_object_agg(a.k, d.value) AS sales
FROM mytable t
, jsonb_each(t.data) a(k,v)
, jsonb_to_recordset(a.v) d(yr text, value float)
WHERE t.employee = 'Jim'
AND a.k LIKE 'sales_%'
AND d.yr = '2012'
GROUP BY 1;
Your first query can be solved like this (shooting from the hip, no access to PG 9.4 here):
SELECT employee, json_object_agg(key, sales)::jsonb AS sales_
FROM (
SELECT t.employee, j.key, sum((e->>'value')::int) AS sales
FROM mytable t,
jsonb_each(t.data) j,
jsonb_array_elements(j.value) e
WHERE t.employee = 'Jim'
AND j.key like 'sales_%'
AND e->>'yr' = '2012'
GROUP BY t.employee, j.key) sub
GROUP BY employee;
The trick here is that you use LATERAL joins to "peel away" outer layers of the jsonb object to get at data deeper down. This query is assuming that Jim may have sales in multiple locations.
(Working on your query 2)