下面是我一直在处理此问题的查询。
查找在奥马哈互助社员工居住的每个城市中都有员工居住的公司的名称。
这意味着,如果互助社在奥马哈,林肯和丹佛这两个城市设有员工,则该公司唯一应返回的公司就是在这三个城市全部都设有员工的公司。这也应该返回Mutual。
以下查询返回在这三个城市中的任何一个都有雇员的公司。姓氏在这里,我可以手动检查正在计算的员工。
SELECT COMPANY_NAME, e1.lastname
FROM EMPLOYEE E1,WORKS W1
WHERE E1.CITY IN (SELECT CITY
FROM EMPLOYEE E2,WORKS W2
WHERE E2.firstname = W2.firstname
AND E2.lastname = W2.lastname
AND W2.COMPANY_NAME= 'Mutual of Omaha')
AND E1.firstname = W1.firstname
AND E1.lastname = W1.lastname;
我意识到我没有把桌子放下,所以在这里
员工(姓,名,中间名,性别,街道,城市)
工作(姓,名,中间名,公司名,薪水)
管理(姓氏,名字,MidInitial,ManagerLastname,MFirstName,MMidInitial,开始日期)
答案 0 :(得分:0)
这不是最精美的代码,但是请尝试使用以下代码:
ServiceAdapter = new ServiceAdapter(Home_Activity.this, voiceSearchArray);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recycler_view.setLayoutManager(linearLayoutManager);
recycler_view.setItemAnimator(new DefaultItemAnimator());
recycler_view.setAdapter(ServiceAdapter);
recycler_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) //check for scroll down
{
visibleItemCount = linearLayoutManager.getChildCount();
totalItemCount = linearLayoutManager.getItemCount();
pastVisiblesItems = linearLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
loading = false;
Log.e("Last Item Wow !", "Last Item Wow !");
OFFSET = OFFSET + 10;
CallVoiceSearchResultWebservice();
//Do pagination.. i.e. fetch new data
}
}
}
}
});
答案 1 :(得分:0)
我将从一个CTE开始,该CTE会让每家公司都经营其所在的城市。
然后有几个选项,但是通过聚合进行自联接可以实现所需的计数:
with cw as (
select distinct e.city, w.company_name
from employee e join
works w
on e.firstname = w.firstname and
e.lastname = w.lastname and
e.midinitial = w.midinitial
)
select cw.company_name
from cw join
cw cwo
on cw.city = cwo.city and
cwo.company_name = 'Mutual of Omaha'
group by cw.company_name
having count(*) = (select count(*) from cw where
cw.company_name = 'Mutual of Omaha');
答案 2 :(得分:0)
您可以列出公司拥有员工的城市并对其进行加权,因此您将获得一个基于每个公司拥有员工的位置代表每个公司的数字,一种组号。
然后,您可以检查哪些城市具有相同的组号。
;with
cities as(select ROW_NUMBER() over (order by city) city_id, city from (select distinct city from employees) c),
chk as (
select distinct company_name, city_id
from works w
join employees e on w.firstname = e.firstname and w.lastname = e.lastname
join cities c on c.city = e.city
),
cnt as (
select company_name, SUM(power(cast(2 as bigint), city_id-1)) n
from chk
group by company_name
)
select company_name
from cnt
where n = (select n from cnt where company_name = 'Mutual of Omaha')
答案 3 :(得分:0)
此选项使用bitwise运算符,因此也受它一次可以使用的城市数量的限制(由于整数大小限制,大约31个城市)
------------Assign each distinct city a value that is x2 the previous (like binary counting)
create table #cityvalues (CityName varchar(100), ValueField int)
select distinct E1.CITY
into #while
FROM EMPLOYEE E1
while (select count(*) from #while) > 0
begin
insert into #cityvalues
select top 1 CITY, coalesce((select max(ValueField) from #cityvalues)*2, 1) from #while
delete from #while w where w.CITY in (select CityName from #CityValues)
end
--------------------------------------------------------------------------------
--------------Create a list of Company/City--------------------------------
create table #companycities (Compname varchar(100),CityName varchar(100))
insert into #companycities
select distinct
W1.COMPANY_NAME
,E1.CITY
FROM EMPLOYEE E1
JOIN WORKS W1 on E1.firstname = W1.firstname AND E1.lastname = W1.lastname
----------------------------------------------------------------------------
----This SUM function then creates a "list" of all cities for the company in a single field
select cc.Compname, sum(cv.ValueField) as AllCities
into #CompanyAllCities
from #companycities cc
join #cityvalues cv on cc.CityName = cv.CityName
group by Compname
-----------------------------------------------------------------
----------This query checks if the company's "list" contains the "list" for the joined company, excluding itself
select distinct cac1.Compname
from #CompanyAllCities cac1
join #CompanyAllCities cac2 on cac2.Compname = 'Mutual'
where cac2.AllCities & cac1.AllCities = cac2.AllCities
and cac1.Compname <> 'Mutual'
-------------------------------------------------------------
----------Tidy up after yourself----------
drop table #cityvalues,#CompanyAllCities,#companycities
-----------------------------------------