SQL Count each time row equal to column - mysql

So I have the carriesOut table:
+------------+------------+
| theatreNo | doctor |
+------------+------------+
| 1 | DD0094CYY |
| 1 | DD234CD1X |
| 2 | DD1199XYZ |
| 2 | DD1199XYZ |
| 4 | AB2323CYA |
| 4 | AB2323CYA |
+------------+------------+
And the doctor table:
+-------------+---------+
| NINumber | lname |
+-------------+---------+
| AB2323CYA | Falk |
| DD0094CYY | Gibbson |
| DD1199XYZ | Smith |
| DD234CD1X | Hammer |
+-------------+---------+
I'm trying to find how many times every doctor has been in an operation, for example, I should get the output:
+------------+---------+------------+
| doctor | lname | operations |
+------------+---------+------------+
| DD0094CYY | Gibbson | 1 |
| AB2323CYA | Falk | 2 |
| DD1199XYZ | Smith | 2 |
| DD234CD1X | Hammer | 1 |
+------------+---------+------------+
And here is what I've tried so far:
SELECT o.doctor
, d.lname
, COUNT(theatreNo) operations
FROM Hospital_CarriesOut o
JOIN Hospital_Doctor d
ON d.NINumber = o.doctor
WHERE theatreNo IN (1,2,4)
GROUP
BY statement DESC
I only get the the following output:
+------------+---------+------------+
| doctor | lname | operations |
+------------+---------+------------+
| DD0094CYY | Gibbson | 1 |
| AB2323CYA | Falk | 1 |
| AB2323CYA | Falk | 3 |
| DD234CD1X | Hammer | 1 |
+------------+---------+------------+

it should be group by Hospital_CarriesOut.doctor, Hospital_Doctor.lname
SELECT Hospital_CarriesOut.doctor, Hospital_Doctor.lname,
COUNT(theatreNo) AS operations
FROM Hospital_CarriesOut JOIN Hospital_Doctor ON Hospital_CarriesOut.doctor=Hospital_Doctor.NINumber WHERE theatreNo IN (1,2,4)
GROUP BY Hospital_CarriesOut.doctor, Hospital_Doctor.lname

Related

How can I enhance this query to use only one view?

I am learning SQL and DB, I need to make the following query, I need to make a query that finds the dates that there were more car crashes and list the names of the people who were involved in these car crashes
person
| name | id_person |
|--------|------------|
| Oliver | 000000001 |
| Harry | 000000002 |
| Jacob | 000000003 |
| Maria | 000000004 |
| Jack | 000000005 |
participated
| id_person | num_crash | cost_damage |
|------------|-------------|---------------|
| 00000001 | 11111101 | 200 |
| 00000002 | 11111102 | 120 |
| 00000003 | 11111102 | 120 |
| 00000004 | 11111103 | 400 |
| 00000005 | 11111104 | 300 |
| 00000002 | 11111105 | 280 |
| 00000005 | 11111106 | 260 |
crash
| num_crash | date_crash | crash_scene |
|-------------|--------------|-------------|
| 11111101 | 2020/04/28 | bairro 4 |
| 11111102 | 2020/05/01 | bairro 1 |
| 11111103 | 2020/05/01 | bairro 2 |
| 11111104 | 2020/05/04 | bairro 3 |
| 11111105 | 2020/05/04 | bairro 1 |
| 11111106 | 2020/05/04 | bairro 3 |
output example
| data_crash | num_crash | name |
|--------------|-------------|-------|
| 2020/05/04 | 11111104 | Jack |
| 2020/05/04 | 11111105 | Harry |
| 2020/05/04 | 11111106 | Jack |
| 2020/05/01 | 11111102 | Harry |
| 2020/05/01 | 11111102 | Jacob |
| 2020/05/01 | 11111103 | Maria |
This is my sql query
CREATE VIEW vwfrequencedatecrash AS
SELECT date_crash, num_crash, crash_scene, ROW_NUMBER() OVER (PARTITION
BY date_crash ORDER BY date_crash) AS frequence
FROM crash
ORDER BY frequence DESC;
CREATE VIEW vwmorefrequencedate AS
SELECT date_crash, num_crash, crash_scene, frequence
FROM vwfrequencedatecrash
WHERE frequence > 1;
SELECT vw.date_crash, pa.num_crash, p.name
FROM vwmorefrequencedate vw
JOIN crash c ON c.date_crash = vw.date_crash
JOIN participated pa ON c.num_crash = pa.num_crash
JOIN person p ON pa.id_person = p.id_person
ORDER BY vw.frequence DESC, c.date_crash;
how can I improve this query?

how to sort sql data for given table?

+---------+----------------+--------+
| aid | fn | col_no |
+---------+----------------+--------+
| 2011768 | ABDUL | 5 |
| 2011499 | ABDULLA | 4 |
| 2011198 | ADNAN | 3 |
| 2011590 | AKSHAYA PRAISY | 2 |
| 2011749 | AMIR | 1 |
| 2011213 | AMOGHA | 5 |
| 2011027 | ANU | 4 |
| 2011046 | ANUDEV D | 3 |
| 2011435 | B S SAHANA | 2 |
| 2011112 | BENAKA | 1 |
+---------+----------------+--------+
How to sort the number like col_no as 1 2 3 4 5 and again repeat as 1 2 3 4 5?
i need output like this
+---------+----------------+--------+
| aid | fn | col_no |
+---------+----------------+--------+
| 2011749 | AMIR | 1 |
| 2011590 | AKSHAYA PRAISY | 2 |
| 2011198 | ADNAN | 3 |
| 2011499 | ABDULLA | 4 |
| 2011768 | ABDUL | 5 |
| 2011112 | BENAKA | 1 |
| 2011435 | B S SAHANA | 2 |
| 2011046 | ANUDEV D | 3 |
| 2011027 | ANU | 4 |
| 2011213 | AMOGHA | 5 |
+---------+----------------+--------+
You can use row_number() partition by col_no:
select t.*
from t
order by row_number() over (partition by col_no order by fn),
col_no;
Here is a db<>fiddle.

MYSQL Complex Join one-to-many

I have the following tables:
clients:
| id | name | code | zone |
--------------------------------
| 1 | client 1 | a1b1 | zone1|
| 2 | client 2 | a2b2 | zone2|
contacts:
| id_contact | first_name | last_name |
----------------------------------------
| 11 | first1 | last1 |
| 22 | first2 | last2 |
| 33 | first3 | last3 |
| 44 | first4 | last4 |
client_contacts:
| id_client | id_contact |
--------------------------
| 1 | 11 |
| 1 | 22 |
| 1 | 33 |
| 2 | 11 |
| 2 | 44 |
offers:
| id_offer | id_client | value |
--------------------------
| 111 | 1 | 100 |
| 222 | 1 | 200 |
| 333 | 1 | 300 |
| 444 | 2 | 400 |
I would like through a optimal select to obtain:
| id_client | name | code | zone | contacts_pers | total_offer_value |
----------------------------------------------------------------------------
| 1 | client 1 | a1b1 | zone1 | first1 last1; | 600 |
first2 last2;
first3 last3;
| 2 | client 2 | a2b2 | zone2 | first1 last1; | 400 |
first4 last4;
I know how to get the desired result with "group_concat" and stored procedures for "total_offer_value". But how to get the desired result from a single efficient select?
SELECT c.id, c.name, c.code, c.zone, GROUP_CONCAT(DISTINCT CONCAT(co.first_name, " ", c.last_name) SEPARATOR ";") AS contact_pers, func_total_offer_value(c.id) AS total_offer_value
FROM clients c
LEFT OUTER JOIN (client_contacts cc, contacts co) ON ( c.id = cc.id_client AND cc.id_contact = co.id_contact )
GROUP BY c.id

How can I count the users with more than 1 Order?

I'm working on a MySQL database and I need to query the database and find out the users with more than one order. I tried using COUNT() but I cannot get it right. Can you please explain the correct way to do this?.
Here are my tables:
User
+-------------+----------+------------------+------------+
| userID | fName | email | phone |
+-------------+----------+------------------+------------+
| adele012 | Adele | aash#gmail.com | 0123948498 |
| ana022 | Anna | ashow#gmail.com | 0228374847 |
| david2012 | David | north#gmail.com | 902849302 |
| jefAlan | Jeffery | jefal#gmail.com | 0338473837 |
| josquein | Joseph | jquein#gmail,com | 0098374678 |
| jweiz | John | jwei#gmail.com | 3294783784 |
| jwick123 | John | jwik#gmail.com | 0998398390 |
| kenwipp | Kenneth | kwip#gmail.com | 0112938394 |
| mathCler | Maththew | matc#gmail.com | 0238927483 |
| natalij2012 | Natalie | nj#gmail.com | 1129093210 |
+-------------+----------+------------------+------------+
Orders
+---------+------------+-------------+-------------+
| orderID | date | User_userID | orderStatus |
+---------+------------+-------------+-------------+
| 1 | 2012-01-10 | david2012 | Delivered |
| 2 | 2012-01-15 | jweiz | Delivered |
| 3 | 2013-08-15 | david2012 | Delivered |
| 4 | 2013-03-15 | natalij2012 | Delivered |
| 5 | 2014-03-04 | josquein | Delivered |
| 6 | 2014-01-15 | jweiz | Delivered |
| 7 | 2014-02-15 | josquein | Delivered |
| 8 | 2015-10-12 | jwick123 | Delivered |
| 9 | 2015-02-20 | ana022 | Delivered |
| 10 | 2015-11-20 | kenwipp | Processed |
+---------+------------+-------------+-------------+
select user_userID, count(*) as orders_count from orders
group by user_userID having orders_count > 1
if you want additional data from your users table, you can do:
select * from user where user_id in (
select user_userID as orders_count from orders
group by user_userID having orders_count > 1
)

Select rows with dates and article points (without relation)

Iv got following tables:
articles
+----+-------------+-----------------------------+--------------+
| ID | ID_group_AG | Title | Date_publish |
+----+-------------+-----------------------------+--------------+
| 1 | 10 | O obrotach sfer niebieskich | 2009-05-07 |
| 2 | 11 | Technologia betonu | 2011-03-21 |
| 3 | 12 | test | 2008-01-13 |
+----+-------------+-----------------------------+--------------+
employee
+----+-----------+-----------+
| ID | Name | Surname |
+----+-----------+-----------+
| 1 | Andrzej | Gacek |
| 2 | Leszek | Ksiazek |
| 3 | Krzysztof | Skibinski |
| 4 | Andrzej | Inny |
+----+-----------+-----------+
articlesGroup
+----+----------+---------------+----------------+
| ID | ID_group | ID_employee | Points |
+----+----------+---------------+----------------+
| 1 | 10 | 1 | 3 |
| 2 | 10 | 3 | 3 |
| 3 | 11 | 1 | 2 |
| 4 | 11 | 2 | 2 |
| 5 | 11 | 4 | 2 |
| 6 | 12 | 4 | 6 |
+----+----------+---------------+----------------+
And following relations:
articles.ID_group_AG => articlesGroup.ID_group
articlesGroup.ID_employee => employee.ID
What I need to do is to print all article points related to a employee, article and publish date, so I use folowing query:
SELECT
p.Name,
p.Surname,
a.Date_publish,
ag.Points
FROM
employee p,
articles a,
articlesGroup ag
WHERE
(ag.ID_group = a.ID_group_AG) AND
(ag.ID_employee = p.ID)
and I get:
+-----------+-----------+--------------+----------------+
| Name | Surname | Date_publish | Points |
+-----------+-----------+--------------+----------------+
| Andrzej | Gacek | 2009-05-07 | 3 |
| Andrzej | Gacek | 2011-03-21 | 2 |
| Leszek | Ksiazek | 2011-03-21 | 2 |
| Krzysztof | Skibinski | 2009-05-07 | 3 |
| Andrzej | Inny | 2011-03-21 | 2 |
| Andrzej | Inny | 2008-01-13 | 6 |
+-----------+-----------+--------------+----------------+
Now lets get to the problem :)
Im using pChart library to make charts.
I want to put on Y axis Points, on X axis all Dates regarding each employee.
So Points array for employee "Andrzej Gacek" will be: [3,2]
for employee "Krzysztof Skibinski" will be: [3]
and Date array (sorted): ["2008-01-13","2009-05-07","2011-03-21"]
I need to add zero points to employee Points array for ex. for "Andrzej Gacek" array should look like: [0,3,2]
so Point will correlate with Dates.
How to form a query to add zeroes to points so the output of the query would look like this:
+-----------+-----------+--------------+----------------+
| Name | Surname | Date_publish | Points |
+-----------+-----------+--------------+----------------+
| Andrzej | Gacek | 2009-05-07 | 3 |
| Andrzej | Gacek | 2011-03-21 | 2 |
| Andrzej | Gacek | 2008-01-13 | 0 |
| Leszek | Ksiazek | 2011-03-21 | 2 |
| Leszek | Ksiazek | 2009-05-07 | 0 |
| Leszek | Ksiazek | 2008-01-13 | 0 |
| Krzysztof | Skibinski | 2009-05-07 | 3 |
| Krzysztof | Skibinski | 2011-03-21 | 0 |
| Krzysztof | Skibinski | 2008-01-13 | 0 |
| Andrzej | Inny | 2011-03-21 | 2 |
| Andrzej | Inny | 2008-01-13 | 6 |
| Andrzej | Inny | 2009-05-07 | 0 |
+-----------+-----------+--------------+----------------+
You have to create a Cartesian Product to get the results you are looking for. I've used a CROSS JOIN to get the dates for each employee.
Give this a try:
SELECT DISTINCT E.Name,
E.SurName,
a.Date_Publish,
IFNULL(AG.Points,0) Points
FROM Articles A CROSS JOIN
Employee E LEFT JOIN
ArticlesGroup AG ON ag.ID_group = a.ID_group_AG
AND E.Id = ag.ID_employee
And here is the SQL Fiddle.