I have a query which is fetching values from multiple tables, it looks some thing close to the below query.
I have highlighted in bold the place where i need to pass the value returned from the same query.
select E.[EmployeeName] as EmployeeName
, (select City from tblEmployeeCities where C.EmployeeID = E.EmployeeID) as EmployeeCity
, (Select State from tblStates **where City = /i need to give the name of
the city returned by the above statement** ) as EmployeeState
from tblEmployees E
Use JOIN/LEFT JOIN:
SELECT E.EmployeeName, C.City, S.State
FROM tblEmployees E
LEFT JOIN tblEmployeeCities C ON C.EmployeeID=E.EmployeeID
LEFT JOIN tblStates S ON S.City=C.City
Maybe something like
select E.[EmployeeName] as EmployeeName
, c.City as EmployeeCity
, (Select State from tblStates where City = c.City ) as EmployeeState
from tblEmployees E
inner join tblEmployeeCities C on C.EmployeeID = E.EmployeeID
I think this can be achieved with a CASE WHEN ELSE END statment in the select part of your sql statment.
Look at example here for syntax or usage: https://www.w3schools.com/sql/func_mysql_case.asp
Related
I have a query which returns few emmployee id's and the query looks like below
select E.EmpID as EmployeeID
from tblEmployee E
where EmpRole in (1,2,3) and E.Test like '%PS%'
I have another query which looks like below one
Select E.EmpID and EmployeeID, B.CountryId, B.CountryName, B.StateId,
B.StateName
from tblEmployeeInfo E
inner join tblTest B
where E.StateId = B.StateId and E.CountryId = B.CountryId
My requirement is that the second query needs to return data of only those employees which are resulted in 1st query...
Both are different tables, how can i join these both ?
use a Select inside the IN clause
Select E.EmpID as EmployeeID, B.CountryId, B.CountryName, B.StateId,
B.StateName
from tblEmployeeInfo E
inner join tblTest B
where E.StateId = B.StateId and E.CountryId = B.CountryId
and E.EmpID IN (select E.EmpID
from tblEmployee E
where EmpRole in (1,2,3) and E.Test like '%PS%'
)
You could do another JOIN in tblEmployee :
Select E.EmpID and EmployeeID, B.CountryId, B.CountryName, B.StateId,
B.StateName
from tblEmployeeInfo E
JOIN tblTest B ON E.StateId = B.StateId and E.CountryId = B.CountryId
JOIN tblEmployee E2 ON E.EmpID = E2.EmpID AND E2.EmpRole in (1,2,3) AND E2.Test like '%PS%'
i need your help! I got some simple SQL skills, but this query kills me...
My Tables
Now i want the TOP5 WorkTimes on the Equipment (What Equipment got the longest WorkTime).
I want this OUTPUT:
MY Query:
SELECT
Equipment, EquipmentName, count(Equipment) as Count
FROM
Operations o
LEFT JOIN Orders ord ON ord.Id = o.[Order]
LEFT OUTER JOIN Equipments e ON ord.Equipment = e.EquipmentNumber
GROUP BY
Equipment, EquipmentName
ORDER BY Count DESC;
Another Question is how i can show o.Worktime?
i got an error with GroupBy...
please help me Thanks!
You can try this query:
select equip_nr,
(select equipmentname from table_equipments where equipmentnr = [to].equip_nr) equip_name,
sum(timeInMins) / 60.0 Worktime
from (
select (select equipmentnr from table_orders where id = [to].[order]) equip_nr,
case when workunittime = 'RH' then worktime * 60 else worktime end timeInMins
from table_operations [to]
where exists(select 1 from table_orders
where [to].[order] = id
and location = '152')
and [start] >= '2018-07-01 00:00:00.000' and [start] < '2018-08-01 00:00:00.000'
) [to] group by equip_nr
By the way, LEFT JOIN is equivalent to LEFT OUTER JOIN.
Just use SUM(worktime) as aggregate function, instead of COUNT(Equipment)
SELECT
e.[ID_Equipment]
, Name
, SUM( IIF(o.WorkUnitTime='MIN', worktime/60.0, worktime) ) as WorktimeMIN
FROM
Operations o
LEFT JOIN Orders ord ON ord.ID_Order = o.ID_Order
LEFT OUTER JOIN Equipment e ON ord.ID_Equipment = e.ID_Equipment
GROUP BY
e.[ID_Equipment]
, Name
ORDER BY
WorktimeMIN DESC
See SQL Fiddle here: http://sqlfiddle.com/#!18/5b5ed/11
Could you please help produce the output table from the below input table(screenshots are provided.Basically I would need to get the pcn from the cn based on the pid in each row.I used a case when statement but the data is huge and it's not a sustainable way,hence the self join would be fine.But I am not getting the expected output from the below self join query
Here is the self join query I tried.
select b.id, b.cn, a.pid, a.cn as pcn
from (
(select pid,cn from categories) a
left join (select id,cn from categories) b on a.pid=b.id
)
Here is the case statement I used for deriving data for some of the data
select id,cn,pid,
case
when pid is NULL then cn
when pid=1 then (select cn from categories where id=1)
when pid=13 then (select cn from categories where id=13)
END as pcn
from categories
I think this is what you want:
SELECT i.id, i.cn, i.pid, c.cn
FROM inputTable i
LEFT OUTER JOIN categories c ON (i.pid = c.id)
select
i.id,
i.cn,
i.pid,
i2.cn as pcn
from input_table i
left join input_table i2
on i.pid=i2.id
The below query could give me the exact expected results.Thanks to Jaime ,jhilden and a_horse_with_no_name SELECT
i.id, i.cn,
i.pid, case when i.pid is null then i.cn else i2.cn end as pcn
FROM inputtable i LEFT OUTER JOIN inputtable i2 ON (i.pid= i2.id)
I have a state table which has states assigned to different countries. There are cases where different countries have same state names. How can I get that. I am trying the following query. Am I right.?
SELECT Name , COUNT(*) count
FROM
[DB].[dbo].[State]
GROUP BY
Name
Having
COUNT(*) > 1
Above query gives correct result. But following query is that I am trying to fetch Country Names also. This is not working
SELECT st.Name , COUNT(*) count,co.Name
FROM [DB].[dbo].[State] st
INNER join [DB].[dbo].Country co on st.CountryID = co.ID
GROUP BY
st.Name,
co.Name
Having
COUNT(*) > 1
Yes, that's correct so I don't understand what is the question?
If you want it with the countries name and all the other information you can use EXISTS() :
SELECT * FROM [DB].[dbo].[State] t
WHERE EXISTS(SELECT 1 FROM [DB].[dbo].[State] s
WHERE t.state = s.state and t.country <> s.country)
EDIT: Try this
SELECT st.Name ,co.Name
FROM [DB].[dbo].[State] st
INNER join [DB].[dbo].Country co on st.CountryID = co.ID
WHERE EXISTS(SELECT 1 FROM [DB].[dbo].[State] st2
WHERE st.name = st2.name
HAVING COUNT(*) > 1)
Hope your Query is correct.,
SELECT st.Name,co.Name,COUNT(st.CountryID) [count]
FROM [DB].[dbo].[State] st
LEFT join [DB].[dbo].Country co on st.CountryID = co.ID
GROUP BY
st.Name,co.Name
Having
COUNT(st.CountryID) > 1
Why do I get "Incorrect syntax near the keyword 'IF'" in the following SQL?:
use AdventureWorks
CREATE FUNCTION Query2_Function(#DPT INT)
RETURNS TABLE
AS
RETURN
IF #DPT is not null
select edh.departmentid,d.name,count(*)as cnt
From HumanResources.Employee e
inner join HumanResources.EmployeeDepartmentHistory edh on e.employeeID = edh.employeeid
inner join humanresources.department d on edh.departmentid = d.departmentid
where d.Name = #dpt
group by edh.departmentid, d.name
You cannot have any flow of control statements in inline table valued functions. If #dpt is null the query will return an empty result set anyway
Edit: or at least would if the correct datatype. You have #DPT INT and are comparing against a name column. That seems doomed to failure at execution time.
Edit 2:
As a solution, you could 1) simply drop the IF #DPT is not null line and 2) either
change the #DPT parameter's type from INT to something like varchar(100), if the function was supposed to search for the department by name,
or
change the WHERE clause to something like this:
where d.departmentid = #dpt
if you meant it to search by department ID.
Try this
CREATE FUNCTION Query2_Function(#DPT INT)
RETURNS #tbl TABLE
(
departmentid int ,
[name] varchar(100),
cnt int
)
AS
begin
IF #DPT is not null
insert into #tbl (departmentid,name,cnt)
select edh.departmentid,d.name,count(*)as cnt
From HumanResources.Employee e
inner join HumanResources.EmployeeDepartmentHistory edh on e.employeeID = edh.employeeid
inner join humanresources.department d on edh.departmentid = d.departmentid
where d.DepartmentID =#DPT
group by edh.departmentid, d.name
return
end
GO