加入四个SQL表

时间:2017-09-03 15:06:25

标签: sql oracle oracle11g oracle-sqldeveloper

我有四个表,如下所示在oracle DB中,Employee列表映射到office ID(m_office包含office ID和Office名称)。所有办事处都是分部或分部。所有细分都属于各个部门。我需要找出分工明智的员工数(分部计数应该包括属于细分的员工),如上表所示。

(因为它是一个工作数据库,因此无法改变结构)。无法找到解决方案。任何帮助都非常值得赞赏。提前谢谢。

TEST_EMPLOYEELIST

 EmpId  EmpName  Office_id
--------------------------------
1      Alex      O1
2      John      O1
3      Bob       O3
4      Max       O5
5      Nick      O5
6      Jack      O1
7      Paul      O2

TEST_M_OFFICE

  office_id   office_name
------------------------------
O1           Kamrup Division
O2           Nalbari Division
O3           Barpeta Division
O4           Nalbari RR Subdivision
O5           Barpeta SR Subdivision
O6           Barpeta RR Subdivision

TEST_DIVISION

division_id    division_name
-----------------------------------
D1             Kamrup Division
D2             Nalbari Division
D3             Barpeta Division
D4             Bongaigon Division
D5             Rangia Division

TEST_DIV_SUBDIV_MAPPING

subdivision_id    subdivision_name         division_id
------------------------------------------------------
S1                Nalbari RR Subdivision    D2
S2                Barpeta SR Subdivision    D3
S3                Barpeta RR Subdivision    D3
S4                Rangia RR Subdivision     D5
S5                Kamrup RR Subdivision     D1

必填:Division_wise记录(包括细分计数)

division_name         count
----------------------------
Kamrup Division         3
Nalbari Division        1
Barpeta Division        3
Bongaigon Division      0
Rangia Division         0

这是我到目前为止所尝试的:

select b.office_name,
       count(*) 
  from test_employeelist a,
       test_m_office b 
 where a.officeid = b.office_id 
 group by b.office_name;

2 个答案:

答案 0 :(得分:1)

试试这个。

SELECT division_name,
       count(*)
FROM
  (SELECT b.office_name division_name
   FROM test_employeelist a,
        test_m_office b,
        TEST_DIVISION c
   WHERE a.office_id=b.office_id
    AND  b.office_name=c.division_name
   UNION ALL 
  SELECT c.division_name division_name
   FROM test_employeelist a,
        test_m_office b,
        TEST_DIVISION c,
        TEST_DIV_SUBDIV_MAPPING d
   WHERE a.office_id=b.office_id
     AND b.office_name = d.subdivision_name
     AND c.division_id = d.division_id )
GROUP BY division_name;

答案 1 :(得分:0)

这是一个非常糟糕的数据模型。由于无法在名称上创建外键,因此简单的拼写错误会使记录无关。

但是,您的办公室是分部,办公室是分部的分支机构。使用UNION ALL将两者结合起来得到一个与办公室和部门相关的中间表。

try:
from urllib.request import urlopen
import smtplib
import time
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import urlopen

ufo = urlopen("https://www.tinydeal.com/mystery-floating-ufo-flying-saucer-
frisbee-magic-trick-toy-p-157394.html")
time.sleep(5)

knife = urlopen("https://www.tinydeal.com/3-in-1-paring-knife-slicer-triple-
fruit-vegetable-cooking-tool-p-163606.html")
time.sleep(5)

mosquito = urlopen("https://www.tinydeal.com/2-in-1-mosquito-killer-lamp-
led-night-light-romantic-sleeping-lamp-p-160265.html")
time.sleep(5)

lenovocase = urlopen("https://www.tinydeal.com/soft-silicone-frosted-back-
case-for-lenovo-vibe-shot-z90-7-p-159210.html")
time.sleep(5)

cereal = urlopen("https://www.tinydeal.com/cereal-double-dispenser-machine-
breakfast-food-storage-container-p-167155.html")
time.sleep(5)

onion = urlopen("https://www.tinydeal.com/onion-goggles-cutting-onion-
glasses-bbq-eye-protective-anti-spicy-no-tears-p-167156.html")

print('Have they changed price?\n')
print ('Mystery Floating UFO Flying Saucer Frisbee Magic Trick')
if ufo == '£1.07':
print ('yes')
else:
print ('no')

print ('3-in-1 knife')
if knife == '£2.31':
print ('yes')
else:
print ('no')

print ('mosquito')
if mosquito == '£3.85':
print ('yes')
else:
print ('no')

print ('lenovo case')
if lenovocase == '£0.69':
print ('yes')
else:
print ('no')

print ('cereal')
if  cereal == '£13.88':
print ('yes')
else:
print ('no')

print ('onion goggles')
if  onion == '£2.31':
print ('yes')
else:
print ('no')