Use count of unrelated table in SQL query - mysql

I have this query:
select skill.name, IFNULL(Round(((SUM(ROUND((student_skills.value/skill.value)*100,0)))/82),0),0) as successRate from skill left JOIN student_skills on skill.id = student_skills.skill_id group by skill.name
This query returns exactly what I want but I need to replace constant 82 (just for example) with number of rows in table user (something like COUNT(user.name)).
Problem is that user is not related to skill or student_skill table in any way.
How should I alter my query so that it would use current count of users?
Thanks

Use a subquery
select skill.name,
IFNULL(Round(((SUM(ROUND((student_skills.value/skill.value)*100,0)))/(select COUNT(*) from user)),0),0) as successRate
from skill
left JOIN student_skills on skill.id = student_skills.skill_id
group by skill.name

Related

SQL Temporary Table or Select

I've got a problem with MySQL select statement.
I have a table with different Department and statuses, there are 4 statuses for every department, but for each month there are not always every single status but I would like to show it in the analytics graph that there is '0'.
I have a problem with select statement that it shows only existing statuses ( of course :D ).
Is it possible to create temporary table with all of the Departments , Statuses and amount of statuses as 0, then update it by values from other select?
Select statement and screen how it looks in perfect situation, and how it looks in bad situation :
SELECT utd.Departament,uts.statusDef as statusoforder,Count(uts.statusDef) as Ilosc_Statusow
FROM ur_tasks_details utd
INNER JOIN ur_tasks_status uts on utd.StatusOfOrder = uts.statusNR
WHERE month = 'Sierpien'
GROUP BY uts.statusDef,utd.Departament
Perfect scenario, now bad scenario :
I've tried with "union" statements but i don't know if there is a possibility to take only "the highest value" for every department.
example :
I've also heard about
With CTE tables, but I don't really get how to use it. Would love to get some tips on it!
Thanks for your help.
Use a cross join to generate the rows you want. Then use a left join and aggregation to bring in the data:
select d.Departament, uts.statusDef as statusoforder,
Count(uts.statusDef) as Ilosc_Statusow
from (select distinct utd.Departament
from ur_tasks_details utd
) d cross join
ur_tasks_status uts left join
ur_tasks_details utd
on utd.Departament = d.Departament and
utd.StatusOfOrder = uts.statusNR and
utd.month = 'Sierpien'
group by uts.statusDef, d.Departament;
The first subquery should be your source of all the departments.
I also suspect that month is in the details table, so that should be part of the on clause.

MySQL Join statement to get data from two tables into a datagridview

I have two tables that I'm trying to join, 'holidays' and 'users'.
Users contains all my user info, the the column 'id' being primary and unique.
Holidays contains a column called 'userid', which corresponds to the id in the user table.
I'm struggling to get the join statement to work... what I'm looking for is the result of the select statement to give me the friendlyname (column 'fname' in user table) instead of giving me the value of userid.
Here's what I'm trying...
SELECT * FROM holidays JOIN users on users.id=holidays.userid WHERE holidays.status = 0
But i'm not getting a correct result - SQL executes without error, but my DGV is filled with tons of erroneous results.
Apologies If I have not used the correct terminology or whatever.
I'm new to the concept of joins.
Here is hopefully a better explanation of what I am after...
Thanks in advance.
You need to select the specific values you want from every table in the JOIN:
SELECT u.fname
FROM holidays h
JOIN users u
ON u.id = h.userid
WHERE h.status = 0
by the alias (FROM users u) you can select column from users table by u.fname
First try to right join to the User table. If you just want the fname then select the column name in the SELECT query, as SELECT * takes more time then SELECT column name.

Group records by UserID, select where on one column value and sum another

Using SQL in MySQLWorkbench I'd like to group records by 'UserID',
select only the records that match where 'Weeks' are in(5,6,7,8) and sum the values for each user in 'Score'.
The records are actually stored in two tables that I am joining, a lineup table and lineup history table:
INNER JOIN vi_lineup on vi_lineup.lineup_master_id = vi_lineup_master.lineup_master_id
Obviously, I am a newbie. I did search S.O. first but did not find a matching question. I'll keep looking and hope someone answers here. Thanks for any help.
A comment asked about the Weeks. The game is based on a weekly model and we number them sequentially. Here's a working query joining the tables and selecting records based on the weeks:
select * FROM vi_lineup LU
INNER JOIN vi_lineup_master LM
WHERE (LU.week > 4 and LU.week < 9)
AND LU.lineup_master_id = LM.lineup_master_id
Limit 0,148000;
What I would like to do now is Group the records by LM.UserID and sum the values in LU.Score
Thanks!
You have not told us where userid or score comes from. But assuming you can clarify that yourself, the "basic" structure of the query will be like this:
SELECT
??.UserID, SUM(??.score) AS sum_score
FROM vi_lineup lu
INNER JOIN vi_lineup_master lm ON lu.lineup_master_id = lm.lineup_master_id
WHERE lu.week between 5 and 8
GROUP BY
??.UserID
You will need to replace each ?? with the correct table alias "lu" or "lm"

mysql - how to include zeros in the count in only one single query

I only have one table to count, I am not using any join. Is this possible?
Select engagement_type as name, COUNT(engagement_type) as y
From events
group By engagement_type
order By engagement_type
But only result is 1 row with count per engagement_type. I want to show all count of accounts without any engagement_type. Like these:
Will appreciate your answers! Thanks!
If there is a lookup-table, say EngagementTypes, where all possible values of engagement types are stored, then you can query this table to get the full list of all types and do a LEFT JOIN to events table in order to get the corresponding count:
Select t1.engagement_type as name, COUNT(t2.engagement_type) as y
From EngagementTypes AS t1
left join events as t2 on t1.engagement_type = t2.engagement_type
group By t1.engagement_type
order By t1.engagement_type

How to reorder row in SELECT query in MYSQL?

A categories table which contains id, category_name,category_type etc.
I am getting all the records with SELECT query.
suppose I have a category name Shared article and I want to get it first position in my records.
of course I am using simple query
SELECT `category_name`,`category_type`,`categories`.`id`,
count(`user_bookmarks`.`id`) as counter FROM `categories`
LEFT JOIN `user_bookmarks`
ON `categories`.`id`=`user_bookmarks`.`category_id`
WHERE `categories`.`user_id`=39 GROUP By `categories`.`category_name`
but I don't know how achieve my task?
Is it possible with MySQL?
Yes you can do as
order by
`categories`.`category_name` = 'Shared article' desc