Skip to content
Advertisement

Editing the output (set brackets depending on the quantity retrieved and the semicolon)

Every time that I run my code I obtain one, or two or several rows on a JSON format. I am using Oracle 11g.

This is my CODE on SQL Fiddle, you will find all the data there.

For example, in this case I obtained something like this:

{“sku”:”99342435″,”PRICE”:”9999″,PRICES_FOR_CLIENTS:[{“group”:”A”,”PRICE”:”29223″},{“group”:”B”,”PRICE”:”33223″},{“group”:”SUPERMARKET”,”PRICE”:”48343″},{“group”:”WALMART”,”PRICE”:”40340″}]};

BUT I could get this output depending on the data stored in the tables:

{“sku”:”99342435″,”PRICE”:”9999″,PRICES_FOR_CLIENTS:[{“group”:”A”,”PRICE”:”29223″},{“group”:”B”,”PRICE”:”33223″},{“group”:”SUPERMARKET”,”PRICE”:”48343″},{“group”:”WALMART”,”PRICE”:”40340″}]};

{“sku”:”95453343″,”PRICE”:”8778″,PRICES_FOR_CLIENTS:[{“group”:”A”,”PRICE”:”29223″},{“group”:”B”,”PRICE”:”33223″},{“group”:”SUPERMARKET”,”PRICE”:”48343″},{“group”:”WALMART”,”PRICE”:”40340″}]};

I want to make my query able to put an “open bracket” in the first row, check if there exist another rows and if no new rows are shown then, insert a close bracket in the end and a semicolon; something like this:

[{“sku”:”99342435″,”PRICE”:”9999″,PRICES_FOR_CLIENTS:[{“group”:”A”,”PRICE”:”29223″},{“group”:”B”,”PRICE”:”33223″},{“group”:”SUPERMARKET”,”PRICE”:”48343″},{“group”:”WALMART”,”PRICE”:”40340″}]}];

BUT another scenario could be when there exist more than 1 row; in that case I would like to put an open bracket in the first row but not a close bracket. I only want to open the bracket in the first row and close it in the last row and within, a semicolon. The different rows MUST BE separated by a comma. Please, follow this example:

[{“sku”:”99342435″,”PRICE”:”9999″,PRICES_FOR_CLIENTS:[{“group”:”A”,”PRICE”:”29223″},{“group”:”B”,”PRICE”:”33223″},{“group”:”SUPERMARKET”,”PRICE”:”48343″},{“group”:”WALMART”,”PRICE”:”40340″}]},

{“sku”:”95453343″,”PRICE”:”8778″,PRICES_FOR_CLIENTS:[{“group”:”A”,”PRICE”:”29223″},{“group”:”B”,”PRICE”:”33223″},{“group”:”SUPERMARKET”,”PRICE”:”48343″},{“group”:”WALMART”,”PRICE”:”40340″}]}];

I tried so many times but I was not able to do it.

Can you help me?

Advertisement

Answer

This is starting to fall into the realm of just because you can make it work, doesn’t mean you should. But to answer your question, check this code out:

I updated your SQLFiddle

What this query does is expand on the previous query. It counts the total rows and this row using the analytic functions COUNT and ROW_NUMBER. I didn’t partition them at all because we want them to consider all returned rows. If Total Rows > 1 and this is the first row start it with ‘[‘. Append the JSON in the middle. And if this is the last row (Total > 1 and ROW_NUMBER = COUNT) then append ‘]’ to the end.

Edit 1: Changed to always have brackets

Here is the updated SQLFiddle (Link).

Given the simpler logic, I switched out the CASE for DECODE. IF the row is the first row it gets the opening bracket. If it is the last row, it gets the closing bracket. If it is both, it gets both.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement