I am trying to get all rows that contain a substring from a particular column in clickhouse
SELECT Column1, count(*) FROM MyTable WHERE Column1 CONTAINS 'word1'
All I need is results that include word1 in column1 and the number of occurrences of each in the entire table.
If you want to count the frequency of each superstring of 'word1', you need a GROUP BY:
SELECT Column1, count(*)
FROM MyTable
WHERE Column1 LIKE '%word1%'
GROUP BY Column1
SELECT COUNT(Column1) AS Column1Count FROM MyTable WHERE Column1 LIKE '%word1%'
I've checked on clickhouse this should work. The '%' sign will give you the contain feature - if you need to insert it using a variable just use concatenation.
Related
I have a table which have two column say Column1 and Column2.
Column1 comprises of Key and Column2 comprises of Values.
I have to display all key-value pair along with key-group sum.
Currently I am ordering the values by Column1 and calculating sum for each key in view using a local variable.
Can this be merged in a single SQL query.
Please see below image for further diagrammatic view.
Yiou can use union all ordered
select column1 , column2
from my_table
union all
select concat(column1, ' - Sub Total') as column1, sum(column2)
from my_table
group by column1
order by column1
I am trying to find the SUM of column1 of all Distinct values of column2. Is it possible?
You could try something like this:
Select SUM(ColA), ColB
from table
Group by ColB
You almost wrote the query yourself in that sentence:
SELECT Column2, SUM(Column1) FROM Table GROUP BY Column2
It's not entirely clear what you're asking...
...this adds all the values in column 1 for each distinct value in column 2 then gives a total of all values in column 1:
SELECT Column2,SUM(Column1) FROM Table GROUP BY Column2 with rollup
Note: If you want the rollup at the top of the output put distinct in it.
SELECT distinct Column2,SUM(Column1) FROM Table GROUP BY Column2 with rollup
My select-statement looks like:
Select column1, column2
From Table1
Group By column2
column1 is a CLOB and I want to receive one of the values that is part of one group. I know they are all the same so it doesn't matter which one I get. I've tried functions like MIN and MAX but they don't accept CLOB as a type.
To be clear I don't want to aggregate the CLOBs just pick one of them.
This is a simplification of the actual SELECT statement and the GROUP BY clause is necessary.
So with this data:
column1 column2
qwerty 1
qwerty 1
asdfgh 2
asdfgh 2
I want to get:
qwerty 1
asdfgh 2
Any idea how this could be done?
A CLOB value cannot be used for grouping or inside a distinct clause.
The only chance you have is to convert the CLOB to a varchar but that means you cannot compare the complete contents of the column (note: those are columns, not rows). If you are certain that all your CLOB values are smaller than 8000 bytes, you can use something like this:
select min(dbms_lob.substr(column1)), column2
from foo
group by column2;
You can use sub-queries.
Add unique column ID and then:
SELECT t1.col2,t2.col1
FROM
(SELECT max(ID) as IDM, col2 FROM Table1 GROUP BY col2) t1
LEFT JOIN
Table1 t2
ON t1.IDM=t2.ID
Try something like this:
SELECT DISTINCT row1, row2 FROM Table1;
I need to return values in a column where only the unique values are returned. I know that DISTINCT will return only unique values, however i need to completely omit any that are duplicated.
i.e.
Column 1 Column 2
----------------------
123456789 27/02/2014
123456789 25/02/2014
654789897 27/02/2014
To return only "654789897 27/02/2014" and omit the other results.
You want to use group by and having:
select column1, column2
from table t
group by column1, column2
having count(*) = 1;
EDIT: (based on comment by knkarthick24)
Depending on what the OP intends, this might also be correct:
select column1, max(column2)
from table t
group by column1
having count(*) = 1;
select column1,column2
from tbl
where column1 in(
select column1
from table
group by column1 having count(column1)=1)
Its good to have Having and GroupBy
Let me know if that works:)
My select-statement looks like:
Select column1, column2
From Table1
Group By column2
column1 is a CLOB and I want to receive one of the values that is part of one group. I know they are all the same so it doesn't matter which one I get. I've tried functions like MIN and MAX but they don't accept CLOB as a type.
To be clear I don't want to aggregate the CLOBs just pick one of them.
This is a simplification of the actual SELECT statement and the GROUP BY clause is necessary.
So with this data:
column1 column2
qwerty 1
qwerty 1
asdfgh 2
asdfgh 2
I want to get:
qwerty 1
asdfgh 2
Any idea how this could be done?
A CLOB value cannot be used for grouping or inside a distinct clause.
The only chance you have is to convert the CLOB to a varchar but that means you cannot compare the complete contents of the column (note: those are columns, not rows). If you are certain that all your CLOB values are smaller than 8000 bytes, you can use something like this:
select min(dbms_lob.substr(column1)), column2
from foo
group by column2;
You can use sub-queries.
Add unique column ID and then:
SELECT t1.col2,t2.col1
FROM
(SELECT max(ID) as IDM, col2 FROM Table1 GROUP BY col2) t1
LEFT JOIN
Table1 t2
ON t1.IDM=t2.ID
Try something like this:
SELECT DISTINCT row1, row2 FROM Table1;