SQL query to calculate subtotal on column - sql

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

Related

Is there a function in SQL that allows me to sum specific rows based on a column value?

I want to sum City4, and the two misspellings together as one row. Any input on how to do this?
SELECT column1,
column2,
count(column3),
Sum(Column4)
FROM TABLE
AND column1 IN ('state1',
'State2',
'State3')
AND column2 IN ('City1',
'City2',
'City3',
'City4',
'City4 misspelled1',
'City4 misspelled 2')
GROUP BY column1,
column2
ORDER BY column1;

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.

Return distinct values from multiple columns in one query

i have searched but i did not find any good answer actually i got the distinct value but the problem is i am applying query on two columns it should return distinct values but it is returning these values
Au |FAA303
Au |FAA505
From my table i want to appear Au only one time as it is now associated with the FAA303 and FAA505
What i want is like this
Au |FAA303
|FAA505
This is my query in postgresql. I am kinda new to the database queries.
select distinct column1, column2
from table_name
The distinct keyword applies to the combination of all selected fields, not to the first one only.
Suppressing repeated values is something you would typically do in an application that connects to your database and performs the query.
Just to show you that it is possible in SQL, I provide you this query, but please consider doing this in the application instead:
select case row_number() over (partition by column1 order by column2)
when 1 then column1
end as column1,
column2
from (
select distinct column1,
column2
from table_name
order by column1, column2
)

SQL Server Sum of column1 of all distinct values of column2

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

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