0

Take a look at this SQL FIDDLE: LINK

There exist a table called "clients" that contains information related to clients:

The relation is "account number" - "client name"

and i used this sentence in order to obtain the name of the client:

LEFT JOIN clients c ON ppc.customer_number = c.account_number

I do not want to join the table clients anymore, but i want to obtain the same result, do you get me?.

I mean, i need this output:

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

Somehow i would need to hard-code the values related to the account_number, i.e:

If account_number = 112121 replace it by WALMART

and if the account_number = 119435 replace it by SUPERMARKET

I tried to do it but i failed.

Can you help me?

3
  • 1
    Alright, so in order to remove the LEFT JOIN you’ll need to update your DECODE statement to use hard coded values instead of c.description. Remember to update the group by as well. Commented Apr 8, 2020 at 5:52
  • Hey dude!, how are you? I have been playing with the query but i'm making mistakes; i mean, i need to hard-code the data from table clients (i dont want to use it anymore and i dont want to create another table)but i am destroying the logic behind :(. The query should return the account number and then, it should be replaced by the hardcoded value, i.e: 112121 replaced by WALMART. Can you help me? Commented Apr 9, 2020 at 0:21
  • @Del dude, how ar you? Could you please take a look at this question: stackoverflow.com/questions/61220925/… Commented Apr 15, 2020 at 4:05

1 Answer 1

0

insert the value into the prices_per_client table directly, so you won't need this left join anymore.


or use a case statement in the select-section:

CREATE VIEW ppc_v AS 
SELECT ppc.*,
  CASE CUSTOMER_NUMBER 
    WHEN 112121 THEN 'WALMART' 
    WHEN 119435 THEN 'SUPERMARKET'
    ELSE NULL 
  END description 
FROM prices_per_client ppc

, create a view and apply it to the query:

SELECT '{"sku":"'||sub.item_code||'","PRICE":"'||sub.item_price||'",PRICES_FOR_CLIENTS:['||listagg('{"group":"'||sub.identifier||'","PRICE":"'||sub.price||'"}',',') WITHIN GROUP (ORDER BY sub.identifier)||']};' AS JSON                                              
FROM (SELECT DECODE(ppc.customer_class_code, 'E', ppc.description, ppc.customer_class_code) AS IDENTIFIER, tpp.item_code, replace(tpp.price, ',', '.') AS ITEM_PRICE, REPLACE(avg(ppc.price), ',', '.') AS PRICE, 
      tpl.request_id, max(tpl.request_id) over (partition by tpp.item_code) as max_request
      FROM table_price_list tpl
      INNER JOIN table_price_product tpp ON tpp.list_header_id = tpl.list_header_id AND tpp.request_id = tpl.request_id
      INNER JOIN ppc_v ppc ON tpp.item_code = ppc.item_code

      WHERE SYSDATE BETWEEN NVL(tpp.start_date_active, SYSDATE) AND NVL(tpp.end_date_active, SYSDATE+1)
      GROUP BY DECODE(ppc.customer_class_code, 'E', ppc.description, ppc.customer_class_code), tpp.item_code, tpp.price, tpl.request_id) sub 
WHERE sub.identifier IS NOT NULL
and sub.request_id = sub.max_request
GROUP BY sub.item_code, sub.item_price;
Sign up to request clarification or add additional context in comments.

6 Comments

Hey dude, how are you? Thank you for replying!. Yeah, i thought about the CASE but when i tried to do that in my query it did not work
The problem is that i was not able to change my query using the CASE statement
strange, what error do you get? at my desk the query is working fine.
How would you change my query? I need to modify my query.I know your code "works" but i cannot modify my code. Do you get me?
Dude, i am not supposed to create tables or views :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.