Skip to content

count the 3 different values ​that a column can take

I’m trying to count the 3 different values ​​that a column in a table can take, in a single SELECT. What am I doing wrong? I am using a postgresql table where the column named “tipoprova” is an integer Answer You are looking for filtered aggregation: Your query is wrong, because you have the…

How to filter record with specific condition?

I have a table called post which contains WordPress posts that have this structure: each zoacres-property post have a meta stored inside the meta table: I’m trying to filter the zoacres-property post by price, and I successfully did this using this query: which returns: this result is correct but, I wou…

Count one column by considering another column

This is my Audit table: I need to write 2 different queries and I have no idea how should I write them: How many invoices do have ONLY product 2 (+ Total price)? How many invoices do have diffrent products but product 2 as well (+ Total price)? the result I want looks like this: Answer If I understand correct…

How to query multiple conditions for one column in SQL?

I have three tables, one is book, the other one is category and last one book_category which has relation between book and category. In my situation I have books and their categories. For example one book can have multiple categories. How can query the books have both categories. For example, one book have no…

How can I select one value from list

I would like to select one record if there are many status. example, if there is the same user and status A, B, C, select only record ‘A’. If there is the same user and status B, C, choose B, if there is only one status, bring that one. expected RESULT Could anyone help to suggest? Thank you Answe…

How to count number of groups?

I have a table as follow: I want to count the number of distinct g_n. (Here it’s 3.) I have tried with: But it gives me the number of items per group. How can I count the number of distinct g_n? Answer Use count(distinct): There is no need to aggregate using group by for this result. Note: This ignores …

JOIN with OR condition and use only MIN(Column)

I have two tables. Certain values from table t need to be matched with certain values of table m in order to identify a target value from table m. Table t looks as follows. Table m looks as follows. The logic should be as follows: Column A from table t should be matched with column matchA from table m and