Guidance required for sql query - mysql

I have a database with one table as shown below. Here I'm trying to write a query to display the names of medication manufactured by the company that manufactures the most number of medications.
By looking at the table we could say the medication names which belongs to the company id 1 and 2 - because those company manufactures the most medication according to this table, but I'm not sure how to write a query for selecting the same i said before.
ID | COMPANY_ID | MEDICATION_NAME
1 1 ASPIRIN
2 1 GLUCERNA
3 2 SIBUTRAMINE
4 1 IBUPROFEN
5 2 VENOFER
6 2 AVONEN
7 4 ACETAMINOPHEN
8 3 ACETAMINO
9 3 GLIPIZIDE
Please share your suggestions. Thanks!

Several ways to do this. Here's one which first uses a subquery to get the maximum count, then another subquery to get the companies with that count, and finally the outer query to return the results:
select *
from yourtable
where companyid in (
select companyid
from yourtable
group by companyid
having count(1) = (
select count(1) cnt
from yourtable
group by companyid
order by 1 desc
limit 1
)
)
SQL Fiddle Demo

This Query might work. I have not tested but the logic is correct
SELECT MEDICATION_NAME
FROM TABLE where
COMPANY_ID=(SELECT
MAX(counted)
FROM ( SELECT COUNT(*) AS counted FROM TABLE ) AS counts);

Related

(my)SQL: query of 2 times the same table with different data to a specific column

I am still a bit new but I would like the following result out of the query:
Input table:
ID
Name
Date 1
Date 2
1
A
1-2-2023
1-5-2023
2
B
2-2-2023
2-5-2023
3
C
3-2-2023
3-5-2023
Expected Output:
ID
Name
Date
1
A
1-2-2023
2
B
2-2-2023
3
C
3-2-2023
1
A
1-5-2023
2
B
2-5-2023
3
C
3-5-2023
I have made the database in MySQL but I'm struggling with the query.
(as specially joining 1 table with it self like this)
How can I make a query results as above?
You can union your queries - this works by "stacking" the results on top of each other, allowing us to choose the date column from each.
By adding a source column we can union them in a derived table and use it to provide for ordering the data as shown.
select Id, Name, Date
from (
select Id, Name, Date1 Date, 1 Source
from t
union all
select Id, Name, Date2, 2 Source
from t
)t
order by Source, Id;
See a demo fiddle

Can I compare different rows in the same MySQL table (SQL)

Is there a way to compare different rows in the same MySQL table? I want to count how many users have changed their names.
Here is roughly what I got in the MySQL log table...
id userid name
1 1 Joe
2 1 Joe
3 1 Joe
4 2 Fiona
5 3 Mark
6 3 Marcel
7 3 Marcel
8 4 Mary
9 4 Marie
You can see that Joe's name has not changed even though he is in the log table 3 times. "Mark" was changed to "Marcel" and "Mary" was changed to "Marie". So if this was the whole of the table, I want to know that 2 people have changed their names (out of four).
SELECT userid, GROUP_CONCAT(name) FROM tablename GROUP BY userid ORDER BY userid DESC
I can examine the output with a scripting language (PHP), I'm just wondering if there is something I can do in SQL that would be neater and only give me the names that are changed.
I'm not sure if this is possible. Haven't found a solution to this yet, if it's a duplicate please let me know. Thanks.
How about using a HAVING clause?
SELECT userid
FROM tablename
GROUP BY userid
HAVING MIN(name) <> MAX(name)
ORDER BY userid DESC;
If you want the names without duplicates:
SELECT userid, GROUP_CONCAT(DISTINCT name)
FROM tablename
GROUP BY userid
HAVING MIN(name) <> MAX(name)
ORDER BY userid DESC;

MySQL: Count occurrences of distinct values for each row

Based on an example already given, I would like to ask my further question.
MySQL: Count occurrences of distinct values
example db
id name
----- ------
1 Mark
2 Mike
3 Paul
4 Mike
5 Mike
6 John
7 Mark
expected result
name count
----- -----
Mark 2
Mike 3
Paul 1
Mike 3
Mike 3
John 1
Mark 2
In my opinion 'GROUP BY' doesn't help.
Thank you very much.
Simplest approach would be using Count() as Window Function over a partition of name; but they are available only in MySQL 8.0.2 and onwards.
However, another approach is possible using a Derived Table. In a sub-select query (Derived Table), we will identify the counts for each unique name. Now, we simply need to join this to the main table, to show counts against each name (while not doing a grouping on them):
SELECT
t1.name,
dt.total_count
FROM your_table AS t1
JOIN
(
SELECT name,
COUNT(*) AS total_count
FROM your_table
GROUP BY name
) AS dt ON dt.name = t1.name
ORDER BY t1.id
If MySQL 8.0.2+ is available, the solution would be less verbose:
SELECT
name,
COUNT(*) OVER (PARTITION BY name) AS total_count
FROM your_table

Select redundant rows only, not the original

So I'm tasked with cleaning up a system that has generated redundant orders.
Data example of the problem
ORDER ID, SERIAL, ...
1 1
2 1
3 2
4 2
5 3
6 3
7 3
The above data shows that 2 orders were generated with serial 1, 2 orders with serial 2, and 3 orders with serial 3. This is not allowed, and there should be only one order per serial.
So I need a query that can identify the REDUNDANT orders ONLY. I'd like the query to exclude the original order.
So the output from the above data should be:
REDUNDANT ORDER IDS
2
4
6
7
I can easily identify which orders have duplicates using GROUP BY and HAVING COUNT(*) > 1 but the tricky part comes with removing the original.
Is it even possible?
Any help is greatly appreciated.
As posted in the comments, here's one way to achieve this:
SELECT T1.ORDER_ID as redundant
FROM thetable T1
LEFT JOIN
(
SELECT SERIAL, MIN(ORDER_ID) AS firstorder
FROM thetable
GROUP BY SERIAL
HAVING COUNT(*) > 1
) T2 ON T1.ORDER_ID=T2.firstorder
WHERE T2.firstorder IS NULL
SQL Fiddle

SQL Grouping and counting using existing row

Lets say we have a table Requests with structure:
user_id
product_id
count
With entries like:
1 1 5
1 2 6
1 1 3
2 1 7
2 1 3
2 2 5
I want to count how much of each product each user has.
And get output like this:
1 1 8
1 2 6
2 1 10
2 2 5
Use GROUP BY with the SUM aggregate function:
SELECT user_id, product_id, SUM(count) AS total
FROM Requests
GROUP BY user_id, product_id
Your query will look something like this:
SELECT user_id, product_id, SUM(`count`)
FROM Requests
GROUP BY user_id, product_id
I wouldn't name a field "count" if I had the choice, as it's a SQL function and could cause weird naming conflicts down the road.
You can find a tutorial on how to use GROUP BY here:
SQL GROUP BY statement
You can also find specific information on the syntax and use of GROUP BY in MySQL in the MySQL Manual:
MySQL SELECT Syntax
MySQL GROUP BY Functions And Modifiers