Get frequency of a column in SQL Server - sql

I have a column with values like 1,1,2,1,... and I would want to get the frequency of 1 and 2, I did
SELECT count(column)
FROM table
WHERE column = 1;
SELECT count(column)
FROM table
WHERE column = 2;
But, could I take the frequency with a more direct way?

Use aggregate functions
Select column, count(*)
From table
Group By column
This will return one row that contains the count, for each distinct value in column

One row each value:
select column 'value', count (column) 'Frequency'
from table
group by column
if only 2 values this give you both results in one row
select sum(case when column=1 then 1 else 0 end) as '1 Frequency',
sum(case when column=2 then 1 else 0 end) as '2 Frequency'
from table

You can try distinct function, like this
SELECT count( distinct column) FROM table;

Related

BigQuery Count Unique and Count Distinct

I am looking for SQL to count unique values in the column.
I am aware of DISTINCT - that gives me how many unique values there are. However, I am looking for - how many ONLY unique values there are.
So if my data is Letters: {A,A,A,B,B,B,C,D}. I am looking to get:
Count Distinct = 4 {A,B,C,D) and
Count Unique = 2 {C,D} <== this is what I am looking for
I am working with BigQuery.
Thank You,
Do
Below query will return only unique values in the column.
SELECT col
FROM UNNEST(SPLIT('A,A,A,B,B,B,C,D')) col
GROUP BY 1 HAVING COUNT(1) = 1;
Then, you can simply count rows.
WITH uniques AS (
SELECT col
FROM UNNEST(SPLIT('A,A,A,B,B,B,C,D')) col
GROUP BY 1 HAVING COUNT(1) = 1
)
SELECT COUNT(*) cnt FROM uniques;
Another option
select count(*) from (
select * from your_table
qualify 1 = count(*) over(partition by col)
)

Different WHERE clause depending on subquery result

I would like to SELECT WHERE column IS NULL or =value depending on result of subquery.
Here is an example incorrect solution that demonstrates the problem:
SELECT *
FROM table
WHERE column=(
SELECT (CASE WHEN COUNT(*) = COUNT(COLUMN) THEN MIN(column) END)
FROM table
)
When the subquery returns NULL the other query will return nothing because column=NULL is never true. How do I fix this?
(Subquery source: https://stackoverflow.com/a/51341498/7810882)
From your question. just add OR column IS NULL in where clause.
You will get the subquery condition or column IS NULL data.
SELECT *
FROM table
WHERE column= (
SELECT (CASE WHEN COUNT(*) = COUNT(COLUMN) THEN MIN(column) END)
FROM table
) OR column IS NULL
If you are only looking for one row, I would suggest:
select t.*
from table t
order by column nulls first
fetch first 1 row only;

SQL SELECT from one of two columns where column value is not equal to something

if I have a table with two columns, and the value for one of them will be known, is there a way to SELECT the value that is not equal to the known value and cast it as another column name?
For instance
columna columnb
1 5
3 1
4 1
1 7
I want to query both columns in the table above for all values not equal to 1, and return the list in a single column called column (or similar), i.e. the resultant table should be:
column
3
4
5
7
I think you just want:
select distinct col
from t cross join
(values (columna), (columnb)) v(col)
where col <> 1;
This will capture situations where both columns are not "1".
If your intention is something along the lines of "the other talker" in a chat, then:
select t.*, (case when columna <> 1 then columna else columnb end) as col
from t
where 1 in (columna, columnb);
You are looking for:
Field alias.
Where clause.
Your query:
SELECT
columnb as [column] --here the alias
FROM
yourTable
WHERE
columnb <> 1 or columnea <>1 --here the where clause
Notice: you can use and or or operator in where clause.
Quoting t-sql select docs:
column_ alias
Is an alternative name to replace the column name in the query result set. For example, an alias such as Quantity, or Quantity to Date, or Qty can be specified for a column named quantity.
Aliases are used also to specify names for the results of expressions, for example:
SELECT AVG(UnitPrice) AS [Average Price]
FROM Sales.SalesOrderDetail;
Edited due to OP comments:
To get values from both columns, the easiest way for you is a UNION:
SELECT
columnb as [column] --here the alias
FROM
yourTable
WHERE
columnb <> 1 --here the where clause
UNION ALL
SELECT
columna as [column] --here the alias
FROM
yourTable
WHERE
columna <> 1 --here the where clause

How to create a variable that identifies duplicate variables in SQL Server 2016?

I have the following columns A and B and would like to add an additional column C to identify duplicate values in column A as 'DUP'.
Use window function with case expression:
select *,
(case when count(*) over (partition by colA) > 1
then 'Duplicate' else 'Single' end) as Dup
from table t;
First, you create the new column c:
alter table my_table add c varchar(10);
Then, you update its value for the repeated rows only:
update my_table set c = 'DUP'
where a in (
select a from my_table group by a having count(*) > 1
);

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;