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 NULL
Tag: sql
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
SQL many joins many to many
I have 5 tables: Table Reports Many to many table ReportsIpRel Table of InfoProviders Many to many table QueriesIpRel And table of Queries The SELECT I am trying to achieve is the following: I tried many left/inner joins like this: but all for nothing. I have to use where condition: WHERE report_Id = ‘2’ /EXAMPLE I’m trying to do this
Create aggregated SQL report where the columns are based on a table list
Not sure this is possible but I’m hoping someone can point me in the right direction. I have a database that audit logs when a record changes stage. What I need to do is consolidate this back up into a report that gives me the earliest audit record for each stage by Record ID. i.e. I end up with a
Select last N rows with multiple values in a single query
I have a table: messages: < id – title – body – city – street – building – created on > While I was able to select required messages in multiple queries, I need to do it in a single one: Here are my queries at the moment: I need one query because I can then use ORDER BY id
using ORDER BY in SQL for chunks of data
I want to know how I sort data in a SQL query but only in certain chunks. I’ll provide an example to make it easier. In the above example, I want to do an ORDER BY height DESC, BUT only the tallest person of each rank gets ordered and everyone else in the same rank is right under that person
Postgresql select distinct Column A based on certain conditions on Column B
I have a table with data: I would like my query output to be distinct on userid but with condition that between fail and success values in status column. I would like to choose success instead (if both fail as in user_3, choose fail then). The table below shows the output that I would like to have as my result:
which is better for user activity in laravel database or log file [closed]
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago. Improve this question hi im thinking about user activity in laravel application .. and i thought is it better to keep user activity in
How to avoid MySQL composite primary key permutations?
CREATE TABLE Friends_Relations( buddy_id VARCHAR(255) NOT NULL, mate_id VARCHAR(255) NOT NULL, PRIMARY KEY (buddy_id, mate_id), FOREIGN KEY (buddy_id) REFERENCES Users(id) ON DELETE …
Anyway to add a constraint when using SQL count
I want to to use count to include a criteria where patient doesn’t have diabetetes, and then using the query to show country that has at least 3 patients that doesn’t have diabetes How shoudl I be doing this? How can I include a constraint when using count? yeah I think this should work, basically my logic is not every