对选择值使用内部选择结构

发布于 2024-12-20 16:13:44 字数 447 浏览 0 评论 0原文

我想听听是否有人可以告诉我一个简单的语法,它可以实现与以下相同的功能(具有相同的灵活性):

SELECT C.CompanyName,
(SELECT Count(*) FROM Employees WHERE CompanyId = C.Id) as EmployeeCount
FROM Company C

现在,重要的是给出 EmployeeCount 的内部 SELECT 是:

  1. 一个独立的 SELECT 语句
    • 这意味着它应该适用于任何现有的 SELECT,即使它已经包含联接等。
  2. 可以使用父 SELECT 中的值

我知道这种情况可以通过其他方式轻松完成,但上面是一个简化的示例解释挑战。我的真实场景是一个复杂的 SELECT 语句,我不想​​通过添加更多联接来使其复杂化。性能没有问题。

I would like to hear if anyone can tell me a simple syntax that accomplishes the same as the following (with the same flexibility):

SELECT C.CompanyName,
(SELECT Count(*) FROM Employees WHERE CompanyId = C.Id) as EmployeeCount
FROM Company C

Now, what's important is that the inner SELECT giving the EmployeeCount is:

  1. An independent SELECT statement
    • This means that it should work with any existing SELECT, even if it already contains joins etc.
  2. Can use values from the parent SELECT

I know that this scenario can be easily accomplished in other ways, but the above is a simplified example to explain the challenge. My real scenario is a complex SELECT statement where I do not want to complicate it by adding more joins. Performance is no issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦境 2024-12-27 16:13:44

使用INNER JOIN

SELECT C.CompanyName, Count(E.*) as EmployeeCount
FROM Company C
INNER JOIN Employees E on E.CompanyId = C.Id 

使用NESTED JOIN

SELECT C.CompanyName, Count(E.1) as EmployeeCount
FROM Company C, Employess E
WHERE E.CompanyId = C.Id

如果你想使用相同的语法,至少这样写:

SELECT C.CompanyName,
(SELECT Count(1) FROM Employees WHERE CompanyId = C.Id) as EmployeeCount
FROM Company C

如果你需要显示所有数据,甚至是那些数据没有任何员工的公司,您可以使用LEFT OUTER JOIN

SELECT C.CompanyName, Count(E.*) as EmployeeCount
FROM Company C
LEFT OUTER JOIN Employees E on E.CompanyId = C.Id 

Using INNER JOIN:

SELECT C.CompanyName, Count(E.*) as EmployeeCount
FROM Company C
INNER JOIN Employees E on E.CompanyId = C.Id 

Using NESTED JOIN:

SELECT C.CompanyName, Count(E.1) as EmployeeCount
FROM Company C, Employess E
WHERE E.CompanyId = C.Id

If you want to use the same syntax, at least put this:

SELECT C.CompanyName,
(SELECT Count(1) FROM Employees WHERE CompanyId = C.Id) as EmployeeCount
FROM Company C

If you need all the data to be shown, even the ones the companies without any Employees, you can use a LEFT OUTER JOIN:

SELECT C.CompanyName, Count(E.*) as EmployeeCount
FROM Company C
LEFT OUTER JOIN Employees E on E.CompanyId = C.Id 
终难愈 2024-12-27 16:13:44

尝试使用派生表,它可以统计您的两个条件。

  1. 独立的 SELECT 语句。
    一个。使用派生表可以让您保留独立的 Select 语句

  2. 可以使用父 SELECT 中的值。

    a.作为内部联接,您仍然可以使用父级选择中的值。


SELECT 
  C.CompanyName, 
  EC.EmployeeCount
FROM Company C 
INNER JOIN (SELECT 
              Count(*) AS EmployeeCount 
            FROM Employees ) EC 
  ON WHERE EC.CompanyId = C.Id

Try using a derived table, which statifies both your conditions.

  1. An independent SELECT statement.
    a. Using a Derived Table allows you to keep your independent Select Statement

  2. Can use values from the parent SELECT.

    a. As an Inner join you can still use values from the parent select.


SELECT 
  C.CompanyName, 
  EC.EmployeeCount
FROM Company C 
INNER JOIN (SELECT 
              Count(*) AS EmployeeCount 
            FROM Employees ) EC 
  ON WHERE EC.CompanyId = C.Id
流心雨 2024-12-27 16:13:44

如果您的内部选择很复杂,那么为什么不对其进行查看:

CREATE VIEW EmpSelect AS
    SELECT CompanyId, whatever FROM Employees;

然后

SELECT
    C.CompanyName, Count(*) AS EmpCount
FROM
    Company C
    LEFT JOIN EmpSelect E
        ON C.Id = E.CompanyId
GROUP BY
    C.CompanyName;

If your inner select is complicated, then why not make a view of it:

CREATE VIEW EmpSelect AS
    SELECT CompanyId, whatever FROM Employees;

Then

SELECT
    C.CompanyName, Count(*) AS EmpCount
FROM
    Company C
    LEFT JOIN EmpSelect E
        ON C.Id = E.CompanyId
GROUP BY
    C.CompanyName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文