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…
SQL DB structure for N items with M parameters varying over time
There’re the items: item_1 … item_N N is about a few hundreds. New items can be added sometimes. Each item has a set of parameters: parameter_1 … parameter_M M is about 20-30. These parameters …
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…
Match specific string before user input
I have the following strings: SDZ420-1241242, AS42-9639263, SPF3-2352353 I want to “escape” the SDZ420- part while searching and only search using the last digits, so far I’ve tried RLIKE …
Too many arguments passed to Stored Procedure storing image
I am trying to store a JPG image into a MSSQL Server 2014. The image path was being passed into a stored procedure and part of the process within the proc is to store the image into a VARBINARY(MAX) field (I cannot change schema). My first attempt was within the same proc like this: Even though the command th…
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