How would I count how many values of each distinct value there are in a specific table? I have a table with a column containing different values but a varying number of different values. I would like create a table with one column just listing the value and another listing the number of each value.
Say i have a column 'Letter' with values A A A A B B C C going down
I just want to make a table with column 'Letter' and 'Number' with A B C vs 4 2 2
SELECT count(letter) occurences,
letter
FROM table
GROUP BY letter
ORDER BY letter ASC
basically you're looking for the COUNT() function. Be aware that it is an aggregate function and you must use GROUP BY at the end of your SELECT statement
if you have your letters on two columns (say col1 and col2) you should first union them in a single one and do the count afterwards, like this:
SELECT count(letter) occurences,
letter
FROM (SELECT col1 letter
FROM table
UNION
SELECT col2 letter
FROM table)
GROUP BY letter
ORDER BY letter;
the inner SELECT query appends the content of col2 to col1 and renames the resulting column to "letter". The outer select, counts the occurrences of each letter in this resulting column.
Related
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
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
I want to select two columns (A & B) from a table and only keep the distinct values of one of them (A). However single value of A can map to multiple values of B. So the following query won't work
select distinct A, B from table1
I am thinking of something like this:
select A, agg(B) from table1 group by A
I want the agg function to just randomly pick a single value from B while grouping A. How to do it in Postgres?
If you want an arbitrary value ("any old value"), then min() and max() are arbitrary values:
select a, min(b) as b
from table1
group by a;
If you want an indeterminate value ("value from any row that matches"), then:
select distinct on (a) a, b
from table1
order by a;
If you want a random value ("value from a random matching row chosen from a uniform distribution"), then:
select distinct on (a) a, b
from table1
order by a, random();
In other words, the definition of "random" is different from "arbitrary" and "indeterminate". However, distinct on is probably what you want along.
Use string_agg with comma :
select A, string_agg(distinct B,',') from table1 group by A;
I have a problem, where first field in unique/distinct and the second field is like concatenated string. i am looking for the distinct rows to be displayed in the second concatenated column not on the first column and i need in the same seq of columns, for ex:
1stcolumn 2ndcolumn(concatenated)
100 ABC-123-PQR
101 ABC-123-PQR
102 ABC-123-PQR
104 ABC-123-STU
in the above example i need to select only ABC-123-PQR AND ABC-123-STU, i don't care on the first column values.
Why not just use the DISTINCT keyword?
SELECT DISTINCT col2 FROM mytable
SELECT MAX(col1), col2 FROM mytable GROUP BY col2
In a table, I want to return duplicate rows based on three columns and a count of the duplicates found.
For example,
In a row, say I have an entry of 1 for column a, 1 for column b and 1 for column c.
I only want to return/count this row if other rows have the exact same entry for the 3 columns (1, 1 and 1).
Thanks in advance!
-N
-- You need to specify the columns you need, and the count
SELECT col1, col2, col3, COUNT(*)
FROM myTable
-- Then you have to group the tuples based on the columns you are doing the count on
GROUP BY col1, col2, col3
-- Here you specify the condition for COUNT(*)
HAVING COUNT(*) > 1;
You can find more information about this here (plus some other useful stuff).