SQL issue; count + display - sql

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;

Related

Group by Column A, and Remove Duplicates from Another Column in SQL

I am trying to group column 1 by their values, and remove duplicate values of column 2 within the group.
For example,
Input
Output
I assume I need to use the function group by column 1 and use distinct to column 2, but I am not sure how to implement it.
SELECT DISTINCT Column_1,Column_2
from your_table
or
SELECT Column_1,Column_2
FROM YOUR_TABLE
GROUP BY Column_1,Column_2
You can do
SELECT distinct col2,
col1
from (Table)
GROUP BY 2

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

MSSQL: find records that has each value of column

Let's say I have some the following table schema:
year|val1|val2
I want to get all the val1 and val2 columns that has all values of year column. I suspect I need grouping here, but I can't imagine how.
Let's say we have set of years from query: SELECT DISTINCT YEAR FROM table. Let's say it returned 2000,2001. So If I have to rows as 2000|1|2 and 2001|1|2 then the query should return single row 1|2
Something like this may work
select
value1
from
table as t1 inner join
(select distinct year from table) as t2
on t1.year=t2.year
group by
val1
having count(distinct t1.year)=(count(distinct t2.year) )
Not sure about your question. But I think you need all the values of val1 and val2 which are there for all the valid values of years.If that's the case then you have to do inner join with the table
something like this
select #yearcnt = count(distinct year) from table
or select val1,count(distint year) as totalyear from table
group by val1
having count(distinct year) = #yearcnt