-1

I need variable in parametrized view.

CREATE VIEW my_new_view AS
SELECT
CampaignName,
sentiment,    -- **CASE WHEN variable** = sentiment THEN sentiment ELSE NULL END AS sentiment_match
COUNT(CASE WHEN HOUR(StartTime) BETWEEN 23 AND 23 THEN sentiment ELSE NULL END) AS `23:00`
FROM call_details
WHERE voc.call_details.sentiment IN (
SELECT voc.call_details.sentiment
FROM voc.call_details
WHERE voc.call_details.sentiment = {sentiment:String}
)
GROUP BY CampaignName, sentiment --**in select if sentiment is there add sentiment in group also else dont add**
ORDER  BY CampaignName,sentiment  ASC;

need variable to store sentiment. is it possible in clikchouse, in mysql we can do by using stored procedure. in clickhouse how to do ?

2
  • Can you clarify what you're trying to achieve? Are you wanting the view to conditionally include the column sentiment in the query (both the SELECT and the GROUP BY) based on a variable being set to the value sentiment? Commented May 24, 2024 at 10:19
  • @gingerwizard yes Commented May 27, 2024 at 5:14

1 Answer 1

0

I'm infering you want the view to conditionally include the column sentiment in the query (both the SELECT and the GROUP BY) based on a variable being set to the value "sentiment"?

Example below probably gets you most of the way there:


create table comments (sentiment String) ENGINE = MergeTree ORDER BY ()

INSERT INTO comments VALUES ('Positive'), ('Negative'), ('Positive'), ('Negative')

CREATE VIEW comments_view
AS SELECT
    if({column:String} = 'sentiment', sentiment, 'All') AS val,
    count()
FROM comments
GROUP BY val


SELECT *
FROM comments_view(column = 'anything')


   ┌─val─┬─count()─┐
   │ All │       4 │
   └─────┴─────────┘

1 row in set. Elapsed: 0.002 sec.

SELECT *
FROM comments_view(column = 'sentiment')

   ┌─val──────┬─count()─┐
   │ Positive │       2 │
   │ Negative │       2 │
   └──────────┴─────────┘

2 rows in set. Elapsed: 0.002 sec.

Sign up to request clarification or add additional context in comments.

2 Comments

here for SELECT * FROM comments_view(column = 'sentiment'); output is coming. but for SELECT * FROM comments_view(column = 'All'); it throwing error {DB::Exception: Column sentiment is not under aggregate function and not in GROUP BY}
You need latest CH and experimental analyzer

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.