sql formating and extracting items (json)










0















So basically I should sum and list all products based on their name they should differ.
This would work properly if I only selected the $[]. item from the table but if I were to select all items with $[].
I will get the following



ROW:



["brand_name": "Product 1", "brand_id": "4", "quantity": 1, "brand_name": "Product 2", "brand_id": "5", "quantity": 3]


RESULT:




"brand_id": "["4", "5"]",
"brand_name": "["Product 1", "Product 2"]",
"quantity": 0



EXPECTED RESULT:




"brand_id": "4",
"brand_name": "Product 1",
"quantity": 1


"brand_id": "5",
"brand_name": "Product 2",
"quantity": 3



QUERY ELOQUENT:



public function scopegetBasicMeans($query)

$query->where('type', 1)
->join('report_asset', 'report_asset.report_id', '=', 'reports.id')->select(
DB::raw('JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].name")) as brand_name'),
DB::raw('JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].brand_id")) as brand_id'),
DB::raw('SUM(JSON_EXTRACT(resources, "$[*].quantity")) as quantity')
)->orderBy('quantity', 'DESC')
->groupBy(DB::raw('JSON_EXTRACT(resources, "$[*].name")'));



QUERY RAW:



select JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].name")) as brand_name, JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].brand_id")) as brand_id, SUM(JSON_EXTRACT(resources, "$[*].quantity")) as quantity, `reports`.`created_at`, `commercialist_id`, `type` from `reports` inner join `report_asset` on `report_asset`.`report_id` = `reports`.`id` where `reports`.`object_id` = ? and `reports`.`object_id` is not null and `type` = ? and `reports`.`deleted_at` is null group by JSON_EXTRACT(resources, "$[*].name") order by `quantity` desc


Basically I should sum the quality of all items with specific name and group them by their name which will get me the unique results.
This would work if i were to select only first item from the table for example: $[0].name[0] but not on multiple.
Any suggestions?



structure










share|improve this question
























  • you appear to have got double-encoded JSON, because you're outputting a JSON string and then putting it inside another object which is then itself encoded as JSON. instead, output all your data into an object and then encode the final object, once.

    – ADyson
    Nov 13 '18 at 9:21











  • I have trouble with indexes, arrays are multi dimensional and i dont know how to extract by index of each item

    – Nathaniel
    Nov 13 '18 at 10:27











  • you appear to be extracting from the same "resources" table each time. What's wrong with a simple SELECT statement which selects all the columns you need? Perhaps if you show the actual SQL data structure and sample of raw data it would be clearer.

    – ADyson
    Nov 13 '18 at 10:31












  • That would be cool but i need unique items for example there is multiple rows with name product 1 and for example quantity of 2 and 3, query should return only one product 1 and sum the quantity which in this case would be 5 if you understand.

    – Nathaniel
    Nov 13 '18 at 10:34











  • use SUM and GROUP BY, then. But it can still be a single SELECT.

    – ADyson
    Nov 13 '18 at 10:34
















0















So basically I should sum and list all products based on their name they should differ.
This would work properly if I only selected the $[]. item from the table but if I were to select all items with $[].
I will get the following



ROW:



["brand_name": "Product 1", "brand_id": "4", "quantity": 1, "brand_name": "Product 2", "brand_id": "5", "quantity": 3]


RESULT:




"brand_id": "["4", "5"]",
"brand_name": "["Product 1", "Product 2"]",
"quantity": 0



EXPECTED RESULT:




"brand_id": "4",
"brand_name": "Product 1",
"quantity": 1


"brand_id": "5",
"brand_name": "Product 2",
"quantity": 3



QUERY ELOQUENT:



public function scopegetBasicMeans($query)

$query->where('type', 1)
->join('report_asset', 'report_asset.report_id', '=', 'reports.id')->select(
DB::raw('JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].name")) as brand_name'),
DB::raw('JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].brand_id")) as brand_id'),
DB::raw('SUM(JSON_EXTRACT(resources, "$[*].quantity")) as quantity')
)->orderBy('quantity', 'DESC')
->groupBy(DB::raw('JSON_EXTRACT(resources, "$[*].name")'));



QUERY RAW:



select JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].name")) as brand_name, JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].brand_id")) as brand_id, SUM(JSON_EXTRACT(resources, "$[*].quantity")) as quantity, `reports`.`created_at`, `commercialist_id`, `type` from `reports` inner join `report_asset` on `report_asset`.`report_id` = `reports`.`id` where `reports`.`object_id` = ? and `reports`.`object_id` is not null and `type` = ? and `reports`.`deleted_at` is null group by JSON_EXTRACT(resources, "$[*].name") order by `quantity` desc


Basically I should sum the quality of all items with specific name and group them by their name which will get me the unique results.
This would work if i were to select only first item from the table for example: $[0].name[0] but not on multiple.
Any suggestions?



structure










share|improve this question
























  • you appear to have got double-encoded JSON, because you're outputting a JSON string and then putting it inside another object which is then itself encoded as JSON. instead, output all your data into an object and then encode the final object, once.

    – ADyson
    Nov 13 '18 at 9:21











  • I have trouble with indexes, arrays are multi dimensional and i dont know how to extract by index of each item

    – Nathaniel
    Nov 13 '18 at 10:27











  • you appear to be extracting from the same "resources" table each time. What's wrong with a simple SELECT statement which selects all the columns you need? Perhaps if you show the actual SQL data structure and sample of raw data it would be clearer.

    – ADyson
    Nov 13 '18 at 10:31












  • That would be cool but i need unique items for example there is multiple rows with name product 1 and for example quantity of 2 and 3, query should return only one product 1 and sum the quantity which in this case would be 5 if you understand.

    – Nathaniel
    Nov 13 '18 at 10:34











  • use SUM and GROUP BY, then. But it can still be a single SELECT.

    – ADyson
    Nov 13 '18 at 10:34














0












0








0








So basically I should sum and list all products based on their name they should differ.
This would work properly if I only selected the $[]. item from the table but if I were to select all items with $[].
I will get the following



ROW:



["brand_name": "Product 1", "brand_id": "4", "quantity": 1, "brand_name": "Product 2", "brand_id": "5", "quantity": 3]


RESULT:




"brand_id": "["4", "5"]",
"brand_name": "["Product 1", "Product 2"]",
"quantity": 0



EXPECTED RESULT:




"brand_id": "4",
"brand_name": "Product 1",
"quantity": 1


"brand_id": "5",
"brand_name": "Product 2",
"quantity": 3



QUERY ELOQUENT:



public function scopegetBasicMeans($query)

$query->where('type', 1)
->join('report_asset', 'report_asset.report_id', '=', 'reports.id')->select(
DB::raw('JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].name")) as brand_name'),
DB::raw('JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].brand_id")) as brand_id'),
DB::raw('SUM(JSON_EXTRACT(resources, "$[*].quantity")) as quantity')
)->orderBy('quantity', 'DESC')
->groupBy(DB::raw('JSON_EXTRACT(resources, "$[*].name")'));



QUERY RAW:



select JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].name")) as brand_name, JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].brand_id")) as brand_id, SUM(JSON_EXTRACT(resources, "$[*].quantity")) as quantity, `reports`.`created_at`, `commercialist_id`, `type` from `reports` inner join `report_asset` on `report_asset`.`report_id` = `reports`.`id` where `reports`.`object_id` = ? and `reports`.`object_id` is not null and `type` = ? and `reports`.`deleted_at` is null group by JSON_EXTRACT(resources, "$[*].name") order by `quantity` desc


Basically I should sum the quality of all items with specific name and group them by their name which will get me the unique results.
This would work if i were to select only first item from the table for example: $[0].name[0] but not on multiple.
Any suggestions?



structure










share|improve this question
















So basically I should sum and list all products based on their name they should differ.
This would work properly if I only selected the $[]. item from the table but if I were to select all items with $[].
I will get the following



ROW:



["brand_name": "Product 1", "brand_id": "4", "quantity": 1, "brand_name": "Product 2", "brand_id": "5", "quantity": 3]


RESULT:




"brand_id": "["4", "5"]",
"brand_name": "["Product 1", "Product 2"]",
"quantity": 0



EXPECTED RESULT:




"brand_id": "4",
"brand_name": "Product 1",
"quantity": 1


"brand_id": "5",
"brand_name": "Product 2",
"quantity": 3



QUERY ELOQUENT:



public function scopegetBasicMeans($query)

$query->where('type', 1)
->join('report_asset', 'report_asset.report_id', '=', 'reports.id')->select(
DB::raw('JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].name")) as brand_name'),
DB::raw('JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].brand_id")) as brand_id'),
DB::raw('SUM(JSON_EXTRACT(resources, "$[*].quantity")) as quantity')
)->orderBy('quantity', 'DESC')
->groupBy(DB::raw('JSON_EXTRACT(resources, "$[*].name")'));



QUERY RAW:



select JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].name")) as brand_name, JSON_UNQUOTE(JSON_EXTRACT(resources, "$[*].brand_id")) as brand_id, SUM(JSON_EXTRACT(resources, "$[*].quantity")) as quantity, `reports`.`created_at`, `commercialist_id`, `type` from `reports` inner join `report_asset` on `report_asset`.`report_id` = `reports`.`id` where `reports`.`object_id` = ? and `reports`.`object_id` is not null and `type` = ? and `reports`.`deleted_at` is null group by JSON_EXTRACT(resources, "$[*].name") order by `quantity` desc


Basically I should sum the quality of all items with specific name and group them by their name which will get me the unique results.
This would work if i were to select only first item from the table for example: $[0].name[0] but not on multiple.
Any suggestions?



structure







php mysql json laravel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 11:16







Nathaniel

















asked Nov 13 '18 at 8:58









NathanielNathaniel

62




62












  • you appear to have got double-encoded JSON, because you're outputting a JSON string and then putting it inside another object which is then itself encoded as JSON. instead, output all your data into an object and then encode the final object, once.

    – ADyson
    Nov 13 '18 at 9:21











  • I have trouble with indexes, arrays are multi dimensional and i dont know how to extract by index of each item

    – Nathaniel
    Nov 13 '18 at 10:27











  • you appear to be extracting from the same "resources" table each time. What's wrong with a simple SELECT statement which selects all the columns you need? Perhaps if you show the actual SQL data structure and sample of raw data it would be clearer.

    – ADyson
    Nov 13 '18 at 10:31












  • That would be cool but i need unique items for example there is multiple rows with name product 1 and for example quantity of 2 and 3, query should return only one product 1 and sum the quantity which in this case would be 5 if you understand.

    – Nathaniel
    Nov 13 '18 at 10:34











  • use SUM and GROUP BY, then. But it can still be a single SELECT.

    – ADyson
    Nov 13 '18 at 10:34


















  • you appear to have got double-encoded JSON, because you're outputting a JSON string and then putting it inside another object which is then itself encoded as JSON. instead, output all your data into an object and then encode the final object, once.

    – ADyson
    Nov 13 '18 at 9:21











  • I have trouble with indexes, arrays are multi dimensional and i dont know how to extract by index of each item

    – Nathaniel
    Nov 13 '18 at 10:27











  • you appear to be extracting from the same "resources" table each time. What's wrong with a simple SELECT statement which selects all the columns you need? Perhaps if you show the actual SQL data structure and sample of raw data it would be clearer.

    – ADyson
    Nov 13 '18 at 10:31












  • That would be cool but i need unique items for example there is multiple rows with name product 1 and for example quantity of 2 and 3, query should return only one product 1 and sum the quantity which in this case would be 5 if you understand.

    – Nathaniel
    Nov 13 '18 at 10:34











  • use SUM and GROUP BY, then. But it can still be a single SELECT.

    – ADyson
    Nov 13 '18 at 10:34

















you appear to have got double-encoded JSON, because you're outputting a JSON string and then putting it inside another object which is then itself encoded as JSON. instead, output all your data into an object and then encode the final object, once.

– ADyson
Nov 13 '18 at 9:21





you appear to have got double-encoded JSON, because you're outputting a JSON string and then putting it inside another object which is then itself encoded as JSON. instead, output all your data into an object and then encode the final object, once.

– ADyson
Nov 13 '18 at 9:21













I have trouble with indexes, arrays are multi dimensional and i dont know how to extract by index of each item

– Nathaniel
Nov 13 '18 at 10:27





I have trouble with indexes, arrays are multi dimensional and i dont know how to extract by index of each item

– Nathaniel
Nov 13 '18 at 10:27













you appear to be extracting from the same "resources" table each time. What's wrong with a simple SELECT statement which selects all the columns you need? Perhaps if you show the actual SQL data structure and sample of raw data it would be clearer.

– ADyson
Nov 13 '18 at 10:31






you appear to be extracting from the same "resources" table each time. What's wrong with a simple SELECT statement which selects all the columns you need? Perhaps if you show the actual SQL data structure and sample of raw data it would be clearer.

– ADyson
Nov 13 '18 at 10:31














That would be cool but i need unique items for example there is multiple rows with name product 1 and for example quantity of 2 and 3, query should return only one product 1 and sum the quantity which in this case would be 5 if you understand.

– Nathaniel
Nov 13 '18 at 10:34





That would be cool but i need unique items for example there is multiple rows with name product 1 and for example quantity of 2 and 3, query should return only one product 1 and sum the quantity which in this case would be 5 if you understand.

– Nathaniel
Nov 13 '18 at 10:34













use SUM and GROUP BY, then. But it can still be a single SELECT.

– ADyson
Nov 13 '18 at 10:34






use SUM and GROUP BY, then. But it can still be a single SELECT.

– ADyson
Nov 13 '18 at 10:34













0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277203%2fsql-formating-and-extracting-items-json%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277203%2fsql-formating-and-extracting-items-json%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo