Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL中连接多个select语句并在不同列中显示_Sql - Fatal编程技术网

如何在SQL中连接多个select语句并在不同列中显示

如何在SQL中连接多个select语句并在不同列中显示,sql,Sql,我编写了一个查询,使用union关键字将两个表连接起来,这两个表显示约会的总数和Lead,我希望此查询的结果显示在两个不同的列中,我的描述很难 SELECT COUNT(FilteredAppointment.createdbyname) AS Appointment FROM FilteredBusinessUnit INNER JOIN FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSys

我编写了一个查询,使用
union
关键字将两个表连接起来,这两个表显示约会的总数
Lead
,我希望此查询的结果显示在两个不同的列中,我的描述很难

SELECT COUNT(FilteredAppointment.createdbyname) AS Appointment
FROM  FilteredBusinessUnit 
INNER JOIN FilteredSystemUser 
    ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid 
INNER JOIN FilteredAppointment 
    ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
UNION
SELECT COUNT(FilteredLead.fullname) AS Lead
FROM  FilteredBusinessUnit 
INNER JOIN FilteredSystemUser 
    ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid 
INNER JOIN FilteredLead 
    ON FilteredSystemUser.systemuserid = FilteredLead.createdby
WHERE (FilteredBusinessUnit.name IN (@Branch))
我期望的结果是:

CRITERIA | Appointment | Leads
Total    | 200         | 123

联合不是正确的方法。Union组合或合并到具有相同列数和数据类型的结果集。我会做这样的事

SELECT 
    (   SELECT COUNT(FilteredAppointment.createdbyname)
        FROM  FilteredBusinessUnit INNER JOIN
              FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
              FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
        WHERE (FilteredBusinessUnit.name IN (@Branch))
    ) AS Appointment ,
    (   SELECT COUNT(FilteredLead.fullname)
        FROM  FilteredBusinessUnit INNER JOIN
              FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
              FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
        WHERE (FilteredBusinessUnit.name IN (@Branch))
    ) AS Lead
为了回应您的评论,以便能够将这些数字与其他分支进行比较,我将尝试类似的方法

SELECT  FilteredBusinessUnit.name , 
        SUM(CASE WHEN FilteredAppointment.createdbyname IS NOT NULL THEN 1 ELSE 0 END) AS Appointments,
        SUM(CASE WHEN FilteredAppointment.fullname IS NOT NULL THEN 1 ELSE 0 END) AS Leads
FROM    FilteredBusinessUnit 
        INNER JOIN FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid 
        LEFT OUTER JOIN FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
        LEFT OUTER JOIN FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
WHERE   (FilteredBusinessUnit.name IN (@Branch1, @Branch2)) 
GROUP BY FilteredBusinessUnit.name

如果要查看所有分支,可以省略where子句。

使用
交叉连接而不是
联合来执行此操作:

select 'Total', Appointment, Leads
from (SELECT COUNT(FilteredAppointment.createdbyname) AS Appointment
      FROM  FilteredBusinessUnit INNER JOIN
            FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
            FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
     ) c1 cross join
     (SELECT COUNT(FilteredLead.fullname) AS Leads
      FROM  FilteredBusinessUnit INNER JOIN
            FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
            FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
      WHERE (FilteredBusinessUnit.name IN (@Branch))
     ) t

where子句是否为两个SELECT都添加了?对不起,我对SQL有点陌生,我还有另一个困难。我还想将两个或更多分支机构的潜在客户总数和任命与此结果进行比较。条件|约会| Leads Lagos | 200 | 123是否要按分支机构名称分组?我不是按分支机构名称分组,我是按分支机构名称筛选,我想在筛选中选择多个分支机构,我的结果应该显示各自的分支机构及其相应的条件(约会和Leads)我不是按分支机构名称分组,我按分行名称筛选,我想在我的筛选中选择多个分行,我的结果应该显示各自的分行及其相应的标准(任命和领导),我同意下面的Gordon。看起来你真的想按分支进行筛选和分组。对不起,我对SQL有点陌生,我还有另一个困难。我还想将两个或更多分支机构的潜在客户总数和任命与此结果进行比较。标准|任命| Leads Lagos | 200 | 123 Abuja | 210 | 323 New York | 200 | 123我不是按分支机构名称分组,我是按分支机构名称筛选的,我想在筛选中选择多个分支机构,我的结果应该显示各自的分支机构及其相应的标准(任命和领导)我不是按分支机构名称分组,我是按分支机构名称筛选,我想在筛选中选择多个分支机构,我的结果应该显示各自的分支机构及其相应的条件(约会和潜在客户)@Tope。如果这里的解决方案不能回答您的问题,请提出另一个问题。关注输入数据的外观以及您希望看到的结果。