SQL Server Sum of column1 of all distinct values of column2 - sql

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

Related

Removing duplicates of column2 then group them based on column1 , then sum the values of column3 in sql

The table looks like
column1 column2 column3
400196 2021-07-06 33
400196 2021-07-06 33
400196 2021-08-16 33
I want to get the sum of column3 values based on grouping of column 1 but the duplicate values of date should not be added
The desired output is:
column1 column3
400196 66
The query I wrote is
select sum(column3)
from table_name
group by column1
But this gives me result 99
You can remove duplicate values in a subquery:
select t.column1, sum(t.column3)
from (select distinct t.column1, t.column2, t.column3
from t
) t
group by t.column1;
Note: This sort of problem can arise when you are joining tables together. Removing duplicates may not always be the right solution. Often it is better to do the calculation before joining, so you don't have duplicate values to deal with.
You could use a two step process here, first remove duplicates, then aggregate and sum:
SELECT column1, SUM(column3) AS column3
FROM (SELECT DISTINCT column1, column2, column3 FROM yourTable) t
GROUP BY column1;
Demo

whereclause with substring in clickhouse

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.

SQL query to calculate subtotal on column

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

SQL issue; count + display

After searching for more than 2 hours on the internet I have not found a specific answer to my questions :
1) How to display only 3 columns of a specific table ?
2) How to count (the sum) of a column in a table.
If you don't want to display all the columns of the table you should specify the names of the columns you want to display:
SELECT column1, column2, column3
FROM table;
If you want to add all the values of a column:
SELECT SUM(column1)
FROM table;
If you wanted to display the summatory along with other columns, then you should group by those columns:
SELECT column1, column2, SUM(column3)
FROM table
GROUP BY column1, column2;
If you want to count the number of rows --it works just like the SUM function--:
SELECT COUNT(*)
FROM table;
1) How to display only 3 columns of a specific table ?
select t.column_1,
t.column_2,
t.column_3
from some_table t;
2) How to count (the sum) of a column in a table.
select sum(t.amount)
from salary t
where t.employee_id = 5;
3) How to count records in table
select count(*)
from employee;

SQL QUERY - Omit ALL duplicate results

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:)