I can't figure this out, I've tried different ways but have been unsuccessful.
My database consists of empid and checkin
I want to find the lowest checkin value which is a number and get the empid for that.
select empid, MIN(checkin) FROM emp GROUP BY empid LIMIT 1
Is what I was doing.
Anyone know how I can do this?
Try this:
select empid, MIN(checkin) FROM emp GROUP BY empid ORDER BY MIN(checkin) LIMIT 1
select empid, checkin from emp order by checkin ASC LIMIT 1
Related
i have 3 table,
- Employee (idemployee, iddivision, firstname, lastname)
- Salary (idsalary, idemployee, dateadded, amount)
- division (iddivision, divisionname)
i want to display the first name that have the highest amount between january and april
so far i tried
SELECT firstname, MAX(Total) FROM (
SELECT firstname,SUM(amount) AS Total
FROM salary
JOIN employee ON employee.idemployee=salary.idemployee
WHERE dateadded BETWEEN "2019-1-1" AND "2019-4-1"
GROUP BY employee.idemployee) as t
but the employeeid sql show is wrong. why?
SELECT employee.firstname, SUM(salary.amount) AS Total
FROM salary
JOIN employee
ON employee.idemployee=salary.idemployee
WHERE salary.dateadded BETWEEN "2019-01-01" AND "2019-04-01"
GROUP BY employee.firstname
ORDER BY 2 DESC
LIMIT 1
You are filtering by the range you want, then you sum the amounts in that range, grouping by the employee. If you order by that sum and get just the first row, it must he one what you are looking for.
Even better, if your employees just have a name in the firstname attribute, you have the risk to group by the same name wrongly. So, to identify better the employee, I would add the idemployee to the group by sentence. Like this:
SELECT employee.idemployee, employee.firstname, SUM(salary.amount) AS Total
FROM salary
JOIN employee
ON employee.idemployee=salary.idemployee
WHERE salary.dateadded BETWEEN "2019-01-01" AND "2019-04-01"
GROUP BY employee.idemployee,employee.firstname
ORDER BY 3 DESC
LIMIT 1
Do you mean you want it to be ordered from greatest to least?
SELECT firstname, Total FROM (
SELECT firstname,SUM(amount) AS Total
FROM salary
JOIN employee ON employee.idemployee=salary.idemployee
WHERE dateadded BETWEEN "2019-1-1" AND "2019-4-1"
GROUP BY employee.idemployee) as t
order by desc Total
Employee table"
empno,
ename,
sal_id,
emp_id,
salary,
month
SALARY table:
sal_id,
emp_id,
salary,
month
I am trying to make query for getting all employee from employee table +
In salary table, there are multiple entry (may be not) of emp_id.
I want employee list with their latest salary( or last month salary)
My current query is :
SELECT * FROM emp LEFT JOIN salary ON emp.empno = salary.emp_id GROUP BY empno ORDER BY salary.sal_id DESC
But I am getting emp list with first salary, I want with latest salary.
Help me :(
Emploee table
Salary table
You are ordering by sal_id instead you might want to order by a field in salary which represents its month.
SELECT emp.empno, emp.name, (
SELECT sal
FROM salary
WHERE salary.empno = emp.empno
ORDER BY salary.sal_id DESC LIMIT 1
)
FROM emp
I have come with solution after a lot R&D and googling .
It is not possible in single query, we have to apply 2 query
SELECT * FROM emp LEFT JOIN salary ON salary.emp_id=emp.empno WHERE salary.sal_id IN ( SELECT MAX(salary.sal_id) FROM salary GROUP BY salary.emp_id ) GROUP BY salary.sal_id ORDER BY salary.sal_id DESC
I have table fields with student id, and date of birth i want to select oldest student from the table. I just want one student at time. I wrote this query
SELECT studid FROM student ORDER BY dob ASC LIMIT 1;
but it gives me another student id. How to fix this? How to Order table before select in one query?
I also tried ORDER BY dob DESC but the problem is same.
dob data type is datetime
SELECT studid
FROM student
HAVING date of birth = MIN(date of birth)
Or
SELECT studid, MIN(date of birth)
FROM student
Try This
select top 1 studid from student order by dob asc
I am trying to write a query which will pull me the employee id's which has stale data for a month in column1.
For ex:
I am maintaining a table where i will store power consumed by an employee per day. Now I want to pull out the emp id's who have maintained power at 30wats constantly for a month.
Try this:-
SELECT emp_id
FROM your_table
GROUP BY emp_id
HAVING ( COUNT ( DISTINCT RANK() OVER ( PARTITION BY emp_id) > 1;
This might be helpful to you.
SELECT empId,TOTAL_SUM FROM
(SELECT empId,SUM(CNT) AS TOTAL_SUM FROM
(SELECT empId,SNAPSHOT_DAY,COUNT(*) AS CNT
FROM emp_details
WHERE power_usage=300
AND SNAPSHOT_DAY>TO_DATE('20150218','yyyymmdd') - 30
AND SNAPSHOT_DAY<=TO_DATE('20150218','yyyymmdd')
GROUP BY empId,SNAPSHOT_DAY
)
GROUP BY empId
)
WHERE total_sum > 29;
How to get the sum of distinct IDs and their respective salaries?
ID Salary
1 1000
2 2000
1 1000
2 2000
In above I want to get the out like this
Total salary
3000
I tried and made the output like this
ID Salary
1 1000
2 2000
select distinct(id), sum (salary) from employee group by id
Main issue is that I am not able to total after applying distinct to the ID.
Want to remove the duplicate entries of id and sum of there distinct ID?
Check this Query is,
Query
SELECT SUM (
DISTINCT salary
) FROM employee
Result
SUM(DISTINCT SALARY)
--------------------
3000
Check Demo SQLFiddle
You can do also by this way,
SELECT SUM(salary)
FROM (SELECT DISTINCT id, salary
FROM employee
GROUP BY id,salary
) AS emp
Check this Demo SQLfiddle
Try this 100% work
SELECT SUM(salary) as "Total salary"
FROM(SELECT DISTINCT id, salary FROM
employee GROUP BY id,salary ) as e
Try Like This
select sum(salary) from( select distinct id, salary from
employee) as t
Simplest way would be, remove duplicate records and write simple query. Second way is this query, in which I have taken MAX(Highest) salary for each employee in sub query, and then applied SUM() from calculating total salary.
SELECT SUM (sal) from
(
SELECT MAX (salary) as sal
FROM employee GROUP BY id
) as tbl
Try this:
It will work definitely
It will sum of salary with same id only.
Select SUM(salary) as total from employee group by id;