Simple SQL 2 Tables data grouping - mysql

I need to complete the following task in SQL:
Show the employment status in individual offices of the company in the following layout: office code, country, number of employees.
Table employees:
Table offices:
My idea was:
SELECT officeCode, (SELECT country FROM offices), COUNT(*)
FROM employees
GROUP BY officeCode
But it seems it doesn't work... Can you help?

Your main data set would be the office. Join to the employee's table by the office code. Then from your main table, get the data you need and then do a count( employee number)
Remember to add all the fields that you are displaying to the group by.

Related

How to count in a SQL list

I've made a table with the columns for a customer name and each activity they participate in. How can I can I count the activities for each name and display it?
I've done;
SELECT Activity_Name, COUNT(*) AS 'Number_of_activities'
FROM tablename
GROUP BY Activity_Name;
which gives me each a table of each activity and how many participants in each activity but not each customer and their number of activities
Apologies for anything I've done wrong, only a couple months into coding and first time posting on stack...
Considering I don't know how your schema looks exactly, this query should be a nice representation of the idea how to do it:
SELECT customer_name, COUNT(*) AS 'Number_of_activities_per_customer'
FROM tablename
GROUP BY customer_name;

How to get Number of Employees Joined or Resigned by Quarterly in SQL

How to get Number of Employees Joined or Resigned by Quarterly in SQL
I have a Table called Mst_Employee
fields are
Emp_No, Emp_JoiningDate, Emp_ResignedDate, Emp_Status
Edit: Every half a year, not quarterly.
The easiest way to do analysis like this would be to use the DATEPART function in T-SQL. Assuming all you want is to know a specific quantity for all quarters in all years on your table, an example code would be:
SELECT
DATEPART(YEAR,Emp_JoiningDate) as [Year],
DATEPART(QUARTER,Emp_JoiningDate) as [Quarter],
COUNT(1) as [Emp Count]
FROM Mst_Employee
GROUP BY DATEPART(YEAR,Emp_JoiningDate),DATEPART(QUARTER,Emp_JoiningDate)
ORDER BY 1,2
This will show all the numbers of employee's joined in the quarter. The query can easily be modified to also show resigned employee's in that quarter, or you could use a separate query to show this data.
Just an additional comment, as you're in the Employee Table, you don't need to directly state "Emp_" under all of your attributes.

Pulling different records from multiple tables as one transaction history list

I am working on an employee management/reward system and need to be able to show a single "transaction history" page that shows in chronological order the different events that the employee has experienced in one list. (Sort of like how in facebook you can goto your history/action section and see a chronological list of all the stuff that you have done and affects you, even though they are unrelated to eachother and just have you as a common user)
I have different tables for the different events, each table has an employee_id key and an "occured" timestamp, some table examples:
bonuses
customers
raise
complaints
feedback
So whenever an event occurs (ie a new customer is assigned to the employee, or the employee gets a complaint or raise) a new row is added to the appropriate table with the employee ID it affects and a timestamp of when it occured.
I need a single query to pull all records (upto 50 for example) that include the employee and return a history view of that employee. The field names are different in each table (ie the bonus includes an amount with a note, the customer includes customer info etc).
I need the output to be a summary view using column names such as:
event_type = (new customer, bonus, feedback etc)
date
title (a brief worded title of the type of event, specified in sql based on the table its referencing)
description (verbiage about the action, such as if its event_type bonus display the bonus amount here, if its a complain show the first 50 characters of the complaint message or the ID of the user that filed the complaint from the complaints table. All done in SQL using if statements and building the value of this field output based on which table it comes from. Such as if its from the customers table IF current_table=customers description='A customer was assigned to you by'.customers.assigner_id).
Ideally,
Is there any way to do this?
Another option I have considered, is I could do 5-6 different queries pulling the records each from their own table, then use a mysql command to "mesh/interleave" the results from all the queries into one list by chronological order. That would be acceptable too
You could use a UNION query to merge all the information together and use the ORDER BY clause to order the actions chronologically. Each query must have the same number of fields. Your ORDER BY clause should be last.
The examples below assume you have a field called customer_name in the customers table and bonus_amount in the bonuses table.
It would look something like this:
SELECT 'New Customer' as event_type, date,
'New customer was assigned' as title,
CONCAT('New Customer: ', customer_name, ' was assigned') as description
FROM customers
WHERE employee_id = 1
UNION
SELECT 'Bonus' as event_type, date,
'Received a bonue' as title,
CONCAT('Received a bonus of $', FORMAT(bonus_amount, 2), '.') as description
FROM bonuses
WHERE employee_id = 1
UNION
...
ORDER BY date DESC;

Comparing corresponding rows from the same column and same table

My table structure is:
Customers(customerid,first name,last name,state)
I want to print the name of the customers who belong to the same state and if they are from states where no other customer lives then that customer should be omitted..I tried inner joins but couldn't get the exact results I get one or more extra rows.
The following is from Access SQL, which should work fine for you.
Select customer_id, state, last_name, first_name
FROM Customers
WHERE (((state) In (Select state FROM Customers GROUP BY state HAVING (((Count(state))>1)))))
ORDER BY state,last_name, first_name

Help me with employee of the month query please

HowI can do a query using the design view to show me the employee with higher sales in pesos per branch if I have 3 tables like the following?
Branches
- Branch
- Id branch
Employees
- EmployeeID
- Employee Name
- Assigned_branch
Sales
- Ticket Id
- Employee Id
- Public price
How can I get the Name of the employe who has the max sum of public price in all sales one per branch in only one query. I cannot use SQL in this and I really not use access frecuently. Please Help me!
I supose that your [Assigned_branch] field is a Foreign Key to [id branch] in table [Branches].
In design editor, drop the three tables into. Create a relation between the created field [Assigned_branch] from table [Employees] and [id branch] field in table [Branches]. Create another relation between table [Sales] field [Employee ID] and table [Employees] field [Employee ID].
On grid beneath, drop the fields [Employee ID] and [Employee Name] from table [Employees]. From table [Branches], drop all fields. From table [Sales] drop only the field [Public price].
Turn the query mode to "Totals", clicking in the "E" (sum) button. Group by all the columns on grid. In the column [Public price] from table [Sales], select the "Sum" totalization option. Order the colum "descending".
Now you have the rank of the better sales of your enterprise. If you want to retrieve only the bigger saler, you should do it quickly creating another query wich uses this one. Add the fields and, on column Sum_of_Sales created, select the "Max" option in the grid. Believe me, its easier than doing all things in just one query (getting totals and retrieving the Max value).
I hope I've helped. Please respond me if you have any doubt.
Open a new query, select the option to enter/view SQL code for the query, then paste the following and study the diagram that's created:
SELECT TOP 1 employeeid, [employee name], branch, sum(price) as total_emp_sales
FROM
employees AS e
INNER JOIN branches AS b ON e.assigned_branch = b.[id branch]
INNER JOIN sales AS s on e.employeeid = s.[employee id]
GROUP BY [employeeid], [employee name], branch
ORDER BY total_emp_sales DESC
Also, for future reference, it's not practical for any of us to try and describe how to use the designer to build the exact query you want. My advice is to get an intro book on SQL, to at least get a working knowledge of the language. Then you'll have a much better idea of how to use the designer.