How to combine the outcome of query? - mysql

I have a table about metro card of station enter and record record(cid, enter_sid, exit_sid)
I want to get the total number of enter and out each station.
For example,
cid enter_sid exit_sid
1 1 2
1 1 2
1 2 3
2 2 1
I want to get
sid count(*)
1 3
2 4
3 1
I don't know how to combine select cid, count(*) from record group by enter_sid and select cid, count(*) from record group by exit_sid
cid means id of card.
For the first row of my expected outcome, 1 is for the id of station, 3 is for sid 1 existing 2 times in enter_sid and 1 time in exit_sid.

the trick to this is your enter and exit sid are the first column so you have to union those two together to get the correct combination... from there its a simple sum of the count.
SELECT sid, cid, SUM(counting) FROM
(
SELECT cid, enter_sid as sid, COUNT(*) as counting FROM record GROUP BY enter_sid
UNION ALL
SELECT cid, exit_sid as sid, COUNT(*) as counting FROM record GROUP BY exit_sid
)t
GROUP BY sid
Working Fiddle

Related

SQL Get users with max occurrences in table

Given the following table transactions, which records the IDs of sellers and buyers who had a transaction, I would like to determine the user who was involved in the highest number of transactions and the number of transactions that user engaged in.
seller_id
buyer_id
date
1
4
2020-01-02
2
1
2020-01-03
3
2
2020-02-16
4
2
2020-02-22
4
3
2020-03-05
The desired output is this:
ID
n_trans
2
3
4
3
Because user 2 had a total of 3 transactions (1 as seller, 2 as buyer) and user 4 also had 3 transactions (2 as seller, 1 as buyer). It can be assumed that a user cannot be a buyer and seller in the same transaction, and that each buyer-seller combination is not duplicated.
What SQL query will get me this? I would not find any similar questions online. Thanks in advance!
You can unpivot, aggregate, and use window functions:
select id, cnt
from (select id, count(*) as cnt,
rank() over (order by count(*) desc) as seqnum
from ((select seller_id as id, date from t) union all
(select buyer_id, date from t)
) i
group by id
) i
where seqnum = 1;

How to neglect duplicate value in MySQL?

I have a table where I am having duplicates value also.
From that table, I want to count duplicate value as 1.
I am using below query to find count
SELECT id, team, count(*) as votes FROM vot GROUP BY team ORDER BY votes DESC;
From this query, I get the duplicates count also.
I hope I made my query clear.
I am very new to MySQL.
What I got from your question is:
Instead of --
id team votes
1 A 2
3 C 2
2 B 1
you want --
id team votes
1 A 1
2 B 1
3 C 1
For this result use the following query:
SELECT id, team, count(distinct team) as votes FROM vot GROUP BY team,id ORDER BY votes DESC;

Guidance required for sql query

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);

Select all type of data

My table is like:
id name seller product
1 bags s1 gold bag
2 shirt s1 shirts
3 shows s1 big show
4 jewellery s1 jewellery
5 Tv s2 Tv
I want to select 4 rows and in 4 rows must have seller s1 and s2 both results. Right now when I selecting 4 records so first 4 record are selecting that have seller s1
Edit
I have tried "GROUP BY" but its only returning single result.... I mean id=1
And
select distinct seller from table
getting only only 2 rows
I need
"I want to select 4 rows and in 4 rows must have seller s1 and s2 both results."
select distinct seller from table
though this will return only 2 rows
The following solution will return 4 rows which contain both sellers s1 and s2.
Code Segment 1
The code segment returns only 3 records for "s1". This is accomplished by adding the where seller = "s1" and limit 3 clause to the query.
select id, name, seller, product
from mytable
where seller = 's1'
limit 3
The union clause joins the two code segments together.
Code Segment 2
The second code segment performs a subquery similar to what was seen in the first code segment. The outer query was added to ensure the limit clause only restricted to the subquery and not to the entire result.
select *
from (
select id, name, seller, product
from mytable
where seller = 's2'
limit 1) t1
Here is the full code with link to SQL Fiddle demo.
select id, name, seller, product
from mytable
where seller = 's1'
limit 3
union
select *
from (
select id, name, seller, product
from mytable
where seller = 's2'
limit 1) t1

Not able to understand the query

I wanted to find two maximum salaries from every department in a table which had department no., salary, and various other columns. I got this answer; it surely works but I am not able to understand the logic.
select *
from emp a where 2 > (select count( distinct(sal))
from emp
where sal > a.sal and a.deptno=deptno)
order by deptno;
For each row in employee, the query within the WHERE clause counts how many rows have a higher salary in the same department. The WHERE clause itself then restricts the results to only those salaries which have 1 or 0 rows (2 >) in the same department with a greater salary - i.e. the highest two salaries.
So with this data:
EmployeeId Sal DeptNo No. of rows in the same department with higher salary
1 1 1 3 (employees 2, 3 and 4)
2 2 1 2 (employees 3 and 4)
3 3 1 1 (employee 4)
4 4 1 0
5 1 2 2 (employees 6 and 7)
6 2 2 1 (employee 7)
7 3 2 0
...the query will select employees 3, 4, 6 and 7, as they're the employees with fewer than 2 employees who have a higher salary than them.
The inner select returns the number of higher salaries within the same department for a given employee. Now if there are less than two higher salaries within the same department then the given employee must be the top earning or next-to-top earning person within the department.
Relocate the subquery to the SELECT clause without the 'top 2' restriction (will obviously get more rows back):
select a.*,
(
select count( distinct(sal))
from emp
where sal > a.sal and a.deptno=deptno
) as tally
from emp a
You can then restrict the resultset using a WHERE clause introducing a further level e.g.
select b.*
from (
select a.*,
(
select count( distinct(sal))
from emp
where sal > a.sal and a.deptno=deptno
) as tally
from emp a
) b
where b.tally < 2
order
by b.deptno, b.tally;
The above is more verbose but maybe easier to follow the logic.