简体   繁体   English

SQL 服务器 2005 - 查询帮助

[英]SQL Server 2005 - Query Help

I am trying to write a query using the following example tables:我正在尝试使用以下示例表编写查询:

Company公司

  • CompanyID公司编号
  • Name姓名
  • County

CompanyRelation公司关系

  • ParentCompanyID母公司 ID
  • ChildCompanyID子公司 ID

CompanySpecialty公司专业

  • CompanyID公司编号
  • Specialty专业

The CompanyRelation table is used because each company can have many subcompanies under it and each subcompany can have many companies over it.使用CompanyRelation表是因为每个公司可以在其下拥有许多子公司,并且每个子公司可以在其上拥有许多公司。

Ideally, what I want to be able to find is all companies by a certain county and/or by a certain specialty.理想情况下,我希望能够找到某个县和/或某个专业的所有公司。 Now, normally, I would just join Company to CompanySpecialty and filter on those two fields, but here is where it gets tricky: the subcompanies don't have counties assigned to them, so if I filter on the county, all the subcompanies will be excluded.现在,通常情况下,我只需将Company加入CompanySpecialty并过滤这两个字段,但这就是棘手的地方:子公司没有分配给他们的县,所以如果我过滤县,所有子公司都将是排除。

If I filter the companies down to a county, I would like to display all subcompanies that are associated to that company, regardless of the fact that the subcompany does not have an address.如果我将公司过滤到一个县,我想显示与该公司关联的所有子公司,不管子公司没有地址。

To get around this, I have to bring in the CompanyRelation table.为了解决这个问题,我必须引入CompanyRelation表。

Example data:示例数据:

CompanyID---Name---County
1-----------ABC----King
2-----------BCD----Pierce
3-----------DEF----NULL
4-----------EFG----NULL

ParentCompanyID---ChildCompanyID
1-----------------1
1-----------------3
2-----------------2
2-----------------4

CompanyID---Specialty
1-----------Vehicles
2-----------Vehicles
3-----------Vehicles
4-----------Vehicles

Using this data, say I want to find all companies in King county that deal with vehicles.使用这些数据,假设我想找到金县所有经营车辆的公司。 In my results, I would expect to see Company 1 and Company 3.在我的结果中,我希望看到公司 1 和公司 3。

How can I write a query to accomplish this?我怎样才能写一个查询来完成这个?

The Diagram would be like this (with Company being the primary / parent table, and all others depending on it):图表将是这样的(公司是主/父表,所有其他表都依赖于它):

Company Specialty <-- Company <-- Relationship <-- Company --> Company Specialty公司专业 <-- 公司 <-- 关系 <--公司--> 公司专业

Converting it to SQL would be like this:将其转换为 SQL 将是这样的:

SELECT
   --whatever fields you want, as long as you prefix with the table alias (p., ps., dr., c., cs.)
FROM
      Company Parent P --parent

   Left Join CompanySpecialty PS
   ON P.CompanyID = PS.CompanyID

   Left Join CompanyRelation CR
   ON P.CompanyID = CR.ParentCompanyID

   Left Join Company C
   ON CR.ChildCompanyID = C.ComapnyID

   Left Join Company Specialty CS
   ON C.CompanyID = CS.CompanyID
WHERE
   (PS.Specialty = 'Vehicle' AND P.County = 'King')
   OR
   (CS.Specialty = 'Vehicle' AND C.County = 'King')
SELECT
  d.ChildCompanyID,
  c.Name
FROM
  CompanyRelation d
INNER JOIN
  Company m
  ON d.ParentCompanyID = m.CompanyID
INNER JOIN
  CompanySpecialty s
  ON d.ChildCompanyID = s.CompanyID
INNER JOIN
  Company c
  ON d.ChildCompanyID = c.CompanyID
WHERE
  m.County = N'King'
  AND s.Specialty = N'Vehicles'

Here is the sample that works:这是有效的示例:

create table #Company (
CompanyID int,
Name nvarchar(20),
County nvarchar(20))
GO

create table #CompanyRelation (
ParentCompanyID int,
ChildCompanyID int)
GO

create table #CompanySpecialty (
CompanyID int,
Specialty nvarchar(20))
GO

insert #Company VALUES (1, 'ABC', 'King');
insert #Company VALUES (2, 'DFG', NULL);
insert #Company VALUES (3, 'HIJ', NULL);
insert #Company VALUES (4, 'KLM', NULL);
GO

insert #CompanyRelation VALUES (1, 1);
insert #CompanyRelation VALUES (1, 3);
insert #CompanyRelation VALUES (2, 2);
insert #CompanyRelation VALUES (2, 4);
GO

insert #CompanySpecialty VALUES (1, 'Vehicles');
insert #CompanySpecialty VALUES (2, 'Vehicles');
insert #CompanySpecialty VALUES (3, 'Vehicles');
insert #CompanySpecialty VALUES (4, 'Vehicles');
GO

SELECT
  d.ChildCompanyID,
  c.Name
FROM
  #CompanyRelation d
INNER JOIN
  #Company m
  ON d.ParentCompanyID = m.CompanyID
INNER JOIN
  #CompanySpecialty s
  ON d.ChildCompanyID = s.CompanyID
INNER JOIN
  #Company c
  ON d.ChildCompanyID = c.CompanyID
WHERE
  m.County = N'King'
  AND s.Specialty = N'Vehicles'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM