How do I ensure that the Number column has values greater than one? - sql

I have tried adding "HAVING Number>1" However this doesn't work correctly it only outputs 1 row
SELECT
T.EmployeeID,
COUNT(*) AS "Number"
FROM (
SELECT
Skill.Title,
Skill.SkillID,
EmployeeSkill.EmployeeID
FROM Skill
RIGHT JOIN EmployeeSkill ON EmployeeSkill.SkillID = Skill.SkillID
) T
GROUP BY T.EmployeeID

I believe it should work if you will use "having count(*)>1" instead of "having number>1"

If you want from the query only the rows where Number > 1
then you need to add a HAVING clause to the statement:
HAVING COUNT(*) > 1

I suspect that you are looking to pull out employees that have at least two skills. If so, it looks like that's a simple aggregate query on table EmployeeSkill, like:
SELECT EmployeeID, COUNT(*)
FROM EmployeeSkill
GROUP BY EmployeeID
HAVING COUNT(*) > 1

Related

count(*) and having not selecting for same date

I have a small doubt on regards how to organize a group_by.
I have this report that lists different we_date for each employee_payroll. one employee payroll can have records on multiple we_dates.
I want to select only when an employee_payroll has more than 4 occurences (disregarding the date column)
I understand that I need to do it either with a having count(employee_payroll) > 4 or a sub-query,
however when I try to do the having it asks me to group by using the date column, and this doesn't return the count per employee_payroll I need. (If I add a count(employee_payroll) I receive 1 in all rows, but I cannot not group by the date field. what am I doing wrong?
want to select only when an employee_payroll has more than 4 occurences (disregarding the date column)
You would use window functions:
select e.*
from (select e.*, count(*) over (partition by employee_payroll) as cnt
from employee e
) e
where cnt >= 4;
Explicit aggregation is probably not the best way to return this result.
This is hard to tell without the actual error you are getting that asks you to group by we_date, but in theory this should be what you have to do:
SELECT employee_payroll
FROM table
GROUP BY employee_payroll
HAVING count(employee_payroll) > 4;
You can first get employees with > 4 occurances. Then , only select those employee rows.
;WITH CTE_EmployeeWithGreaterThanFourOccurance (
SELECT Employee_Payroll, COUNT(*) AS cnt
FROM Employee
GROUP BY Employee_Payroll
HAVING COUNT(*) > 4
)
SELECT *
FROM Employee AS e
INNER JOIN CTE_EmployeeWithGreaterThanFourOccurance AS c
ON c.Employee_Payroll = e.Employee_Payroll

Getting result basis on count of another SQL query

I have a table with the following columns:
bkng_date
bkng_id (varchar)
villa_id (varchar)
This query
select bkng_date,count(*) as cnt
from tab_bkng_det
group by bkng_date;
returns the no.of records for each date as count.
Now I need to find dates in the resultset of this query where cnt = 2.
I tried a couple of subqueries but I'm not getting the desired results.
The simplest, correct and safe solution is adding having count(*) = 2 clause as Gordon said.
For completeness, if you were curious how to solve it using subqueries (you didn't provide your db vendor though it's very likely your vendor supports having clause), it would be:
select x.bkng_date, x.cnt from (
select bkng_date,count(*) as cnt
from tab_bkng_det
group by bkng_date
) x
where x.cnt = 2
or
with x as (
select bkng_date,count(*) as cnt
from tab_bkng_det
group by bkng_date
)
select * from x where cnt = 2
Best Option is to use the Having Clause as follows,
select bkng_date,count(*) as cnt
from tab_bkng_det
group by bkng_date
having count(*) = 2

group by in sql

Here i dont want to group by on amount and i also dont want to remove amount from select statement
so how to modify query
select min(id),userid,calcu.amount from usertable ut
inner join calcu co on ut.userid=co.userid
group by userid,amount
how i modify above query ...
i try to achieved records which user have min id.. let say user have 1,2,3,4 id in calcu table now i want 1 id and against that there is amount column so i want that amount ..
when i remove amount i got 10 records but when i write amount column in select and group by i got 15 records
You can achieve it using the following nested query.
SELECT (SELECT MIN(ID),USERID FROM USERTABLE ut WHERE ut.USERID = co.USERID GROUP BY USERID), co.AMOUNT FROM CALCU co
Can you try this please? Your question is not very clear (pls post sample data and a table with desired output so I can test my query)
SELECT A.MINID, A.USERID, co.AMOUNT
FROM (SELECT MIN(id) AS MINID, USERID
FROM usertable ut
GROUP BY userid
) A
INNER JOIN calcu co ON A.USERID=co.userid
When you just select amount without grouping by it, a random row for each group will be selected (if your DBMS allows this at all).
In most DBMSs you have to specify which row for each group you want to display. This happens with aggregate functions. Choose the one that fits your needs
max()
min()
avg()
count()
If you want to see the row that corresponds to a row with a min()/max() value you can use one of these approaches (they are standard sql, so not limited to mysql): The Rows Holding the Group-wise Maximum of a Certain Column
Try something around these lines:
select calcu.id,ut.userid,calcu.amount
from usertable ut
inner join
(
select id,userid,amount from calc where id in
(
select min(id) from calc group by userid
)
) calcu
on ut.userid = calcu.userid

SQL Select employees with work history in multiple states

I need a query that will show only employees who have been paid in more than one state.
The query will pull three columns:
EmployeeID
WorkLocation
LastPayDate
My current, unsuccessful attempt:
Select EmployeeID
, WorkLocation
, max(LastPayDate)
from Table
group by EmployeeID, WorkLocation
having COUNT(distinct WorkLocation) > 1
This query pulls zero records. I know there are employees who have worked in multiple states, however. I am not sure where my logic breaks down.
Any instruction is much appreciated.
You need to have a count(workLocation) > 1 which indicates that they have worked in more than 1 state. Specify this in the HAVING clause. Since you're only concerned in GROUPS which contain multiple WorkLocations.
If you're trying to check for multiple work locations within a specific year, you will perform that logic in the WHERE clause.
select EmployeeId
from table xyz
//where year(LastPayDate) == 2015
group by EmployeeId
having count(distinct WorkLocation) > 1
Figured it out. I needed to use a subquery. Solution as follows:
Select t.EmployeeID
, t.WorkLocation
, t.LastPayDate
From Table t
Where t.EmployeeID in
(
Select t2.EmployeeID
From Table t2
Group by t2.EmployeeID
Having count(distinct t2.WorkLocation) > 1
)
Group by t.EmployeeID, t.WorkLocation
Order by t.EmployeeID
Thanks to everyone for helping.

Adding count in select query

I am trying to find a query that would give me a count of another table in the query. The problem is that I have no idea what to set where in the count part to. As it is now it will just give back a count of all the values in that table.
Select
ID as Num,
(select Count(*) from TASK where ID=ID(Also tried Num)) as Total
from ORDER
The goal is to have a result that reads like
Num Total
_________________
1 13
2 5
3 22
You need table aliases. So I think you want:
Select ID as Num,
(select Count(*) from TASK t where t.ID = o.ID) as Total
from ORDER o;
By the way, ORDER is a terrible name for a table because it is a reserved work in SQL.
You can do it as a sub query or a join (or an OVER statement.)
I think the join is clearest when you are first learning SQL
Select
ID as Num, count(TASK.ID) AS Total
from ORDER
left join TASK ON ORDER.ID=TASK.ID
GROUP BY ORDER.ID