i'm trying to display the same column three times with different values, but can't seem to get the code. right.
I have three tables
Projectdetails (projectID), projectroles (projectroleID), projectstaff (projectstaffID)
Projectdetails
+-----------+-------------+----------------+---------------+----------------+
| ProjectID | ProjectCode | ProjectName | ProjectActive | ProjectStageID |
+-----------+-------------+----------------+---------------+----------------+
| 1 | K6674 | Ironworks | TRUE | 5 |
| 2 | K6694 | Overstone Leys | TRUE | 5 |
| 3 | K6702 | Salt | TRUE | 5 |
| 9 | K6678 | Dyers | TRUE | 5 |
| 10 | K6397 | Chatham Road | TRUE | 6 |
| 11 | K6691 | Kennel Grounds | TRUE | 1 |
| 12 | K6693 | Church Farm | TRUE | 4 |
+-----------+-------------+----------------+---------------+----------------+
ProjectStaff
+----------------+-----------+------------+---------------+
| ProjectStaffID | ProjectID | EmployeeID | ProjectRoleID |
+----------------+-----------+------------+---------------+
| 1 | 1 | 31 | 6 |
| 2 | 2 | 15 | 6 |
| 3 | 3 | 61 | 6 |
| 4 | 9 | 61 | 6 |
| 5 | 10 | 72 | 6 |
| 6 | 10 | 95 | 13 |
| 7 | 10 | 70 | 10 |
| 8 | 10 | 1 | 10 |
+----------------+-----------+------------+---------------+
ProjectRoles
+---------------+---------------------------+
| ProjectRoleID | ProjectRole |
+---------------+---------------------------+
| 1 | Archaeologist |
| 2 | Assistant Supervisor |
| 3 | CAD Manager |
| 4 | CAD Technician |
| 5 | Photographer |
| 6 | Project Manager |
| 7 | Project Officer |
| 8 | Senior Project Officer |
| 9 | Specialist |
| 10 | Supervisor |
| 11 | Surveyor |
| 12 | Trainee |
| 13 | Post-Excavation Manager |
+---------------+---------------------------+
SELECT
projectdetails.ProjectID,
projectdetails.ProjectCode,
projectdetails.SiteCode,
(SELECT
employees.EmpABB
WHERE
projectstaff.ProjectRoleID = 6) AS ProjectManager,
(SELECT
employees.EmpABB
WHERE
projectstaff.ProjectRoleID = 13) AS PostExManager,
(SELECT
employees.EmpABB
WHERE
projectstaff.ProjectRoleID = 10) AS Supervisor,
projectdetails.ProjectName,
projectdetails.ProjectActive,
projectdetails.ProjectStageID,
projectdetails.AuthorityID
FROM
projectdetails
LEFT JOIN projectstaff ON projectstaff.ProjectID = projectdetails.ProjectID
INNER JOIN projectroles ON projectstaff.ProjectRoleID = projectroles.ProjectRoleID
INNER JOIN employees ON employees.ProjectRoleID = projectroles.ProjectRoleID AND projectstaff.EmployeeID =
employees.EmployeeID
GROUP By
projectdetails.projectID
This produces the following table. It's displaying each project once but not displaying data for postex manager or Supervisor.
Example below:
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| ProjectID | ProjectCode | SiteCode | ProjectManager | PostExManager | Supervisor | ProjectName | ProjectActive | ProjectStageID |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 1 | K6674 | RHN20 | AG | | | Ironworks | TRUE | 5 |
| 2 | K6694 | | PC | | | Overstone Leys | TRUE | 5 |
| 3 | K6702 | | PCM | | | Salt | TRUE | 5 |
| 9 | K6678 | | PCM | | | Dyers | TRUE | 5 |
| 10 | K6397 | KCWC19 | ZP | | | Chatham Road | TRUE | 6 |
| 11 | K6691 | KAKW20 | JW | | | Kennel Grounds | TRUE | 1 |
| 12 | K6693 | | PM | | | Church Farm | TRUE | 4 |
| | | | | | | | | |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
But if I remove GROUP BY, it does display the other project Roles but separates out the data by adding additional rows.
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| ProjectID | ProjectCode | SiteCode | ProjectManager | PostExManager | Supervisor | ProjectName | ProjectActive | ProjectStageID |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 1 | K6674 | RHN20 | AG | | | Ironworks | TRUE | 5 |
| 2 | K6694 | | PC | | | Overstone Leys | TRUE | 5 |
| 3 | K6702 | | PCM | | | Salt | TRUE | 5 |
| 9 | K6678 | | PCM | | | Dyers | TRUE | 5 |
| 10 | K6397 | KCWC19 | ZP | | | Chatham Road | TRUE | 6 |
| 10 | K6397 | KCWC19 | | JB | | Kennel Grounds | TRUE | 6 |
| 10 | K6397 | KCWC19 | | | W Pool | Church Farm | TRUE | 6 |
| 10 | K6397 | KCWC19 | | | R Books | March House | TRUE | 6 |
+-----------+-------------+----------+----------------+---------------+----------
Any ideas?
I'm going to answer my own question. Usual thing, you ask the question, relax, then the answer pops in your head. I'm not entirely sure why it works as I got the answer from another query in my list of queries which have worked before.
You use CASE
SELECT
projectdetails.ProjectID,
projectdetails.ProjectCode,
projectdetails.SiteCode,
max(case when projectstaff.ProjectRoleID = 6 then employees.EmpABB else null end) as ProjectManager,
max(case when projectstaff.ProjectRoleID = 13 then employees.EmpABB else null end) as PostExManager,
max(case when projectstaff.ProjectRoleID = 10 then employees.EmpABB else null end) as Supervisor,
projectdetails.ProjectName,
projectdetails.ProjectActive,
projectdetails.ProjectStageID,
projectdetails.AuthorityID
FROM
projectdetails
LEFT JOIN projectstaff ON projectstaff.ProjectID = projectdetails.ProjectID
INNER JOIN projectroles ON projectstaff.ProjectRoleID = projectroles.ProjectRoleID
INNER JOIN employees ON employees.ProjectRoleID = projectroles.ProjectRoleID AND projectstaff.EmployeeID =
employees.EmployeeID
GROUP BY
projectdetails.ProjectID
| ProjectID | ProjectCode | SiteCode | ProjectManager | PostExManager | Supervisor | ProjectName | ProjectActive | ProjectStageID |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 1 | K6674 | RHN20 | AG | | | Ironworks | TRUE | 5 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 2 | K6694 | | PC | | | Overstone Leys | TRUE | 5 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 3 | K6702 | | PCM | | | Salt | TRUE | 5 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 9 | K6678 | | PCM | | | Dyers | TRUE | 5 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 10 | K6397 | KCWC19 | ZP | JB | W Pool | Chatham Road | TRUE | 6 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 11 | K6691 | KAKW20 | JW | | | Kennel Grounds | TRUE | 1 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 12 | K6693 | | PM | | | Church Farm | TRUE | 4 |
+-----------+-------------+----------+----------------+---------------+------------+-----
Related
+---------+----------------+--------+
| 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.
I have a query_table Table and wants to join with match_table Table with nearest matching string. If it was vice-versa then 'like' would have worked but have no idea how to do this.
query_table
+----+------------------+
| id | string |
+----+------------------+
| 1 | fcc9e8796feb |
| 2 | fcdbd7ebcf89 |
| 3 | fccc87896feb |
| 4 | fcc7c7896fef |
| 5 | fcced777aaaf |
+----+------------------+
match_table
+----+-----------+
| id | match_code|
+----+-----------+
| 1 | fcff |
| 2 | fcccc |
| 3 | fccc8 |
| 4 | fccc9 |
| 5 | fccdb |
| 6 | fccdc |
| 7 | fccd8 |
| 8 | fcce |
| 9 | fcced |
| 10 | fccee |
| 11 | fcce6 |
| 12 | fcc7b |
| 13 | fcc7c |
| 14 | fcc8e |
| 15 | fcc87 |
| 16 | fcc88 |
| 17 | fcc9e |
| 18 | fcdbb |
| 19 | fcdbc |
| 20 | fcdbd |
+----+-----------+
I expect
result
+----+------------------+----+----------------+
| id | string | id | match_code |
+----+------------------+----+----------------
| 1 | fcc9e8796feb | 17 | fcc9e |
| 2 | fcdbd7ebcf89 | 20 | fcdbd |
| 3 | fccc87896feb | 3 | fccc8 |
| 4 | fcc7c7896fef | 13 | fcc7c |
| 5 | fcced777aaaf | 9 | fcced |
+----+------------------+----+----------------+
I want to ranking result with city based in mysql
table1:-users
| user_id | marks |
--------------------
| 1 | 10 |
| 5 | 10 |
| 5 | 50 |
| 3 | 15 |
| 4 | 10 |
| 2 | 10 |
| 6 | 10 |
| 6 | 50 |
| 4 | 15 |
| 4 | 10 |
table:-2 users details
| user_id | city |
--------------------
| 1 | newdelhi |
| 2 | kolkata |
| 3 | mumbai |
| 4 | newdelhi |
| 5
| 6 | newdelhi |
I want to result like this:
| user_id | points |
--------------------
| 6 | 60 |
| 4 | 35 |
| 1 | 10 |
Try this :
SELECT
users.user_id
,SUM(users.marks) AS points
FROM
users
INNER JOIN
users_details ON users.user_id = users_details.user_id
WHERE
users_details.city = 'newdelhi'
GROUP BY
user_id
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
)
i'm pretty new to sql and i have a problem that i coundn't really describe to google
SELECT name AS state
FROM state
WHERE id IN
(
SELECT state_id
FROM city
WHERE id=(SELECT city_id FROM zipcode)
)
;
This lists the states of all zipcodes I have in my database, I also want to list the zipcodes (zipcode.zipcode) in a additional column corresponding with the state
Thank you very much in advance for your help
my tables look like this:
city
+----+----------+-----------+---------------------+---------+---------+
| id | state_id | county_id | name | lat | lng |
+----+----------+-----------+---------------------+---------+---------+
| 1 | 1 | 1 | Prem, Oberbayern | 47.6833 | 10.8 |
| 2 | 2 | 2 | Pfullendorf (Baden) | 47.9249 | 9.25718 |
| 3 | 3 | 3 | Wissen, Sieg | 50.7833 | 7.75 |
| 4 | 1 | 4 | Miltenberg | 49.7039 | 9.26444 |
| 5 | 1 | 5 | Moosthenning | 48.6833 | 12.5 |
| 6 | 1 | 1 | Bernbeuren | 47.7333 | 10.7833 |
| 7 | 4 | 6 | Demmin, Hansestadt | 53.9 | 13.0333 |
| 8 | 2 | 7 | Konstanz, Universit | 47.6667 | 9.18333 |
| 9 | 5 | 8 | Teutschenthal | 51.45 | 11.8 |
| 10 | 6 | 9 | Vierlinden | 52.515 | 14.3141 |
+----+----------+-----------+---------------------+---------+---------+
zipcode
+----+---------+-------------+---------+
| id | city_id | district_id | zipcode |
+----+---------+-------------+---------+
| 1 | 1 | NULL | 86984 |
| 2 | 2 | NULL | 88630 |
| 3 | 3 | NULL | 57537 |
| 4 | 4 | NULL | 63897 |
| 5 | 4312 | 502 | 84164 |
| 6 | 6 | NULL | 86975 |
| 7 | 7 | 778 | 17109 |
| 8 | 8 | NULL | 78462 |
| 9 | 8 | NULL | 78464 |
| 10 | 8 | NULL | 78465 |
+----+---------+-------------+---------+
state
+----+------------------------+
| id | name |
+----+------------------------+
| 1 | Bayern |
| 2 | Baden-W?rttemberg |
| 3 | Rheinland-Pfalz |
| 4 | Mecklenburg-Vorpommern |
| 5 | Sachsen-Anhalt |
| 6 | Brandenburg |
| 7 | Niedersachsen |
| 8 | Schleswig-Holstein |
| 9 | Nordrhein-Westfalen |
| 10 | Th?ringen |
+----+------------------------+
Use explicit JOINs:
SELECT z.*, s.name AS state
FROM zipcode z JOIN
city c
ON z.city_id = c.id JOIN
state s
ON c.state_id = s.id;
If you are learning SQL, focus on learning JOIN rather than IN.