如何从多个选择语句(WITHOUT UNION)创建SQL查询结果

时间:2019-02-27 19:18:34

标签: sql sql-server

我有一些select语句,当它们单独执行时会返回我需要的内容。 我想将所有这些选择语句组合成看起来像普通表的东西。 我尝试了UNION,但收到一条错误消息,提示我需要相同数量的参数(显然是列字段),但这不是我想要的。 所以我尝试了这个(到目前为止我的代码)

SELECT Company_Code FROM Company
UNION
SELECT Founder FROM Company
UNION
SELECT CAST(COUNT(Company.id) as varchar(5)) as Number_of_LeadManagers
FROM Lead_Manager
LEFT JOIN Company
ON (Lead_Manager.Companyid = Company.id)
GROUP BY Company.id
UNION
SELECT COUNT(Company.id) as Number_of_SeniorManagers
FROM Senior_Manager
LEFT JOIN Company
ON (Senior_Manager.Companyid = Company.id)
GROUP BY Company.id
UNION
SELECT COUNT(Company.id) as Number_of_Managers
FROM Manager
LEFT JOIN Company
ON (Manager.Companyid = Company.id)
GROUP BY Company.id
UNION
SELECT COUNT(Company.id) as Number_of_Employees
FROM Employee
LEFT JOIN Company
ON (Employee.Companyid = Company.id)
GROUP BY Company.id

这整个语句在彼此之间返回我想要的所有内容。 以我的代码为例,如果每个select语句单独运行,它将返回我想要的结果(总是返回两行) 我想要的是最终结果,只需将所有这些select语句的结果并排放置,这样它将显示2行,但select语句返回了多少列。

我想要澄清的想法 如果我执行

SELECT Company_Code,Founder FROM Company

我明白了

**Result1**
row 1 :  C1 Monika
row 2 :  C2 Samantha

(仅2行) 如果我执行

   SELECT CAST(COUNT(Company.id) as varchar(5)) as Number_of_LeadManagers
    FROM Lead_Manager
    LEFT JOIN Company
    ON (Lead_Manager.Companyid = Company.id)
    GROUP BY Company.id

我明白了

**Result2**
row 1 : 1
row 2 : 1

其他select语句基本上相同,只是使用不同的表进行计数。 我想要的是一个查询,该查询使我可以将Result1和Result2彼此相邻(以及其余的选择结果) 期望的最终结果

**Result3**
row 1 : C1 Monika 1
row 2 : C2 Samantha 1

PS。上周启动的SQL仍然很挣扎,顺便说一下,这是一个家庭作业项目。 如果我的思维方式不对,请告诉我,但这就是我在大脑中看到的方式,无法完全弄清楚如何对其进行SQL处理; P

2 个答案:

答案 0 :(得分:1)

如果您有一个字符串列,那么在联合c中的每个选择都需要一个字符串列

因此,您只需要像第一次选择一样将每个计数都转换为char即可。

SELECT Company_Code FROM Company
UNION
SELECT Founder FROM Company
UNION
SELECT CAST(COUNT(Company.id) as varchar(5)) as Number_of_LeadManagers
FROM Lead_Manager
LEFT JOIN Company
ON (Lead_Manager.Companyid = Company.id)
GROUP BY Company.id
UNION
SELECT CAST(COUNT(Company.id) as varchar(5)) as Number_of_SeniorManagers
FROM Senior_Manager
LEFT JOIN Company
ON (Senior_Manager.Companyid = Company.id)
GROUP BY Company.id
UNION
SELECT CAST(COUNT(Company.id)  as varchar(5)) as Number_of_Managers
FROM Manager
LEFT JOIN Company
ON (Manager.Companyid = Company.id)
GROUP BY Company.id
UNION
SELECT CAST(COUNT(Company.id) as varchar(5)) as Number_of_Employees
FROM Employee
LEFT JOIN Company
ON (Employee.Companyid = Company.id)
GROUP BY Company.id

但是如果您需要所有结果都在同一行上,则可以在子查询上使用左连接

SELECT Company_Code , Founder
  , t1.Number_of_LeadManagers
  , t2.Number_of_SeniorManagers
  , t3.Number_of_Managers 
  , t4.Number_of_Employees
FROM Company
LEFT JOIN (
SELECT Company.id, COUNT(Company.id) as Number_of_LeadManagers
FROM Lead_Manager
LEFT JOIN Company ON Lead_Manager.Companyid = Company.id
GROUP BY Company.id
) t1 on t1.id = company.id
LEFT JOIN (
SELECT  company.id, COUNT(Company.id)  as Number_of_SeniorManagers
FROM Senior_Manager
LEFT JOIN Company
ON (Senior_Manager.Companyid = Company.id)
GROUP BY Company.id
) t2 ON t2.id = company.id
LEFT JOIN (
SELECT company.id, COUNT(Company.id) as Number_of_Managers
FROM Manager
LEFT JOIN Company
ON (Manager.Companyid = Company.id)
GROUP BY Company.id
) t3 ON t3.id = company.id
LEFT JOIN (
SELECT company.id, COUNT(Company.id) as Number_of_Employees
FROM Employee
LEFT JOIN Company
ON (Employee.Companyid = Company.id)
GROUP BY Company.id
) t4 ON t4.id = company.id

答案 1 :(得分:0)

使所有这些对象都位于同一行上的最简单方法就是关联子查询。

SELECT Company.Company_Code, Company.Founder 
  , (
        SELECT COUNT(Company.id) 
        FROM Lead_Manager
        WHERE Lead_Manager.Companyid = Company.id
    ) AS Number_of_LeadManagers
  , (
        SELECT COUNT(Company.id) 
        FROM Senior_Manager
        WHERE Senior_Manager.Companyid = Company.id
    ) AS Number_of_SeniorManagers
  , (
        SELECT COUNT(Company.id) 
        FROM Manager
        WHERE Manager.Companyid = Company.id
    ) AS Number_of_Managers
  , (
        SELECT COUNT(Company.id) 
        FROM Employee
        WHERE Employee.Companyid = Company.id
    ) AS Number_of_Employees
FROM Company

根据您的数据,您可能需要进一步消除重复项。